BASH Course Part 1 Student Edition2
BASH Course Part 1 Student Edition2
part 1
by Ivan Radic
1 / 142
What is it? Bash is one of many Unix shells.
A Unix shell is a commandline interpreter, first written in early
seventies
Bash was written in 1989. by Brian Fox for the GNU Project as a
free software replacement for the Bourne shell (sh)
The GNU Project is a free software started in 1983 by Richard
Stallman at MIT.
2 / 142
Bash Some info from Wikipedia.
filename wildcarding,
piping,
here documents,
command substitution,
variables and control structures for conditiontesting and iteration.
the keywords, syntax and other basic features of the language were
all copied from sh (Bourne shell).
3 / 142
Configuration
4 / 142
Configuration Configuration files are kept is user HOME directory.
All configuration files are hidden files.
files 1. .bash_history
2. .bash
_logo
ut
A list of auto run commands executed when user exits the shell.
3. .bash
_prof
ile
4. .bash
rc
5 / 142
Configuration $bash
files NOTE: Since BASH is not a default shell on our cloud system, when
you open a new shell, first command you type in is always
$ba
sh
$ba
sh
$ba
sh
6 / 142
Environment Wikipedia:
In plain English:
$en
v #printenvi
ronm
ent
#someinter
esti
ngvar
iabl
es(neverm
indgrepfornow)
$en
v|gre
pS HE
LL #pat
hofs hel
le xe
cutablefile
$en
v|gre
p" ^H
OME=" #use
r'shomedirec
tory
$en
v|gre
pP AT
H #theexe
cutab
lessearchpath
7 / 142
Environment To change environment variable use the command:
$VA
RNAME=v
alue #N
OTE:t
herearenosp
acesaround=
$ex
portVA
RNAME
$VA
RNAME=v
alue;expo
rtV
ARNAM
E;
Save this to your .bashrc file, and every shell will see your
variable VARNAME
$so
urce$H
OME/.
bashr
c
8 / 142
Environment TIP: to output variable to screen, use echo command
$ec
ho$VAR
NAME(ore
cho"
$VAR
NAME"
)
9 / 142
Environment 1. Create environment variable IVAN and give it a value of "teacher"
10 / 142
Environment 1. $I
$e
VA
N=
ch
o$
tea
ch
IV
AN
er
teach
er
Solution 2. $I VA
N="collea
gue"
$e ch
o" $IVAN"
colle
ague
#
#T IP
:b yde fa
ulta l
lval
uesinBASHar
est
rings,
#t he
reforequotatio
nisopti
onal.
3. $ged
it$H
OME/
.bash
rc
$echo$IVAN
coll
eagu
e
11 / 142
Basic commands
12 / 142
Basic BASH uses many standard commands. This tutorial will only cover
what I think are the most common ones. Help for each command can be
commands accessed by man command, e.g.:
$ma
nls
$w h
ichl s
/bin
/ls
$w h
ichb ash
/bin
/bash #so,B
ASHi
sju
stan
otherUNIXprogram
13 / 142
Redirecting There are always three default files
Each open file gets assigned a file descriptor. The file descriptors for
default files
stdin (0)
stdout (1)
stderr (2)
14 / 142
Redirecting COMMAND_OUTPUT > file
1>fi
lename #Redir
ects
tdou
ttofile"file
name."
1>>f
ilename #Redir
ecta
nda
ppendstdo
uttofile"filename."
2>fi
lename #Redir
ects
tder
rtofile"file
name."
2>>f
ilename #Redir
ecta
nda
ppendstde
rrtofile"filename."
&>fi
lename #Redir
ectb
oths
tdou
tandstde
rrtofile"filename."
15 / 142
Redirecting COMMAND_INPUT < file
16 / 142
alias Any long command can be shortened by using alias command:
alia
sshort
='lon
gcom
mand
'
define or display
aliases Example (trivial but useful):
teac
h='IVAN
=teac
her;echo$IVA
N'
alia
s
17 / 142
alias To make alias available in current shell use source command:
sour
ce$HOM
E/.ba
shrc
define or display
aliases In fact, let's create our first alias.
Open .bashrc file in text editor:
gedi
t$HOME
/.bas
hrc
alia
sss='s
ource$HOM
E/.b
ashrc
'
18 / 142
echo Only three options
n
display a line of do not output the trailing newline
text e
enable interpretation of backslash escapes
E
disable interpretation of backslash escapes (default)
echoH ello
Hello
echo" Helloworl
d"
Hellow orld
echo' Helloworl
d'
Hellow orld
19 / 142
echo Double quotes can be escaped:
echo"Hello\"wo
rld\"
"
display a line of Hello"worl
d"
text
Single quotes can't be escaped:
echo'Hello\'
Hello\
echo'Hello''wo
rld'
Helloworld
20 / 142
echo Double quotes expand variables:
echo" hello
,$ US
ER,curre
ntdi
ris:$PWD
"
display a line of hello,ei va
rad,curre
ntdiris
:/ho
me/ei
varad
/bash_course
text
Single quotes don't expand variables:
echo' hello
,$US
ER,curren
tdiris:$PWD
'
hello,$U SE
R,cu
rrentdiris:$
PWD
$X=
ABC
$ec
ho"$Xa
bc"
$X =
ABC
$e c
ho"${X
}abc"
ABCa
bc
$e c
ho-e" Firstline
\nse
condl
ine"
Firs
tline
seco
ndline
21 / 142
echo 1.
2.
print "Hello world!"
print "Hello world!" with quotes included
3. print "Hello\nworld!" with newline expanded
display a line of 4. print your current working directory
text
Excersize
22 / 142
Student edition, no solution provided
echo
display a line of
text
Solution
23 / 142
ls List information about the files (in the current directory by default).
Sort entries alphabetically by default.
list content of There are many (maaaany!) options available for this command, here
directory are the most common ones:
$ls/home/
eivar
ad
$ls-a/ho
me/ei
varad #sh
owal
lfil
es
$ls-l/ho
me/ei
varad #lo
ngli
sting
,onefileperline
$ls-r/ho
me/ei
varad #re
verseorde
r
$ls-R/ho
me/ei
varad #re
cursi
ve,i
nclud
esubdirectories
$ls-t/ho
me/ei
varad #so
rtbymodi
ficat
iontime
help: man ls
24 / 142
ls ls command supports wildcard search functionality:
#l is
ta llfil
esindire
ctory
$l s*
a.shtest.
shtest.p
y
#l is
ta lls he
llsc
riptsind
irect
ory
$l s*.sh
a.shtest.sh
#l is
ta llpyt
honfilewi
th"
test"int
heirname
$l s*test
*.py
test1
.pyuser_
test2
.py
$l s?.sh #l
ista
llsh
ellscript
sindirectory
a.sh #w
ithn
amee
xactl
yo necha
racterlong
$l stest.
?? #l i
sta
llfi
lewithna
me"test"
test.
shtest.p
y #a
ndt
woch
aract
erex
tens
ion
help: man ls
25 / 142
ls List content of your home folder (all files) by:
1. File name
list content of 2. Long file name
directory 3. Long file name in reversed order
4. Long file name including all subdirectories
5. Long file name, but sort by modification date
Excersize 6. Long file name, but sort by modification date in reversed order
$ls~
TIP2: another shortcut for your home folder is HOME env variable:
$ls$HOME
help: man ls
26 / 142
Student edition, no solution provided
ls
list content of
directory
Solution
help: man ls
27 / 142
ls alia
sl
#s
l=
om
'l
s-
elik
la
'
etous
eju
stl,orl
aforthisalias
#evenshort
erve
rsio
n
help: man ls
28 / 142
mkdir Create the directory(ies), if they do not already exist.
$mk
dir~ /b
ash_c
ourse
make directories $mk
dir~ /b
ash_c
ourse
#t howsanerror,dir
ector
yalr
eadyexist
s
$mk
dir-p~/bas
h_cou
rse
#n oer
rorwith-pop t
ion
$mk
dir- p~/bas
h_cou
rse/
subdi
r1/su
bdir2
#c reat
em ul
tiplesub
direc
torie
sw it
h-poption
29 / 142
cd Change the shell working directory.
$cdDIR
change directory
Change the current directory to DIR. The default DIR is the value of
the HOME shell variable.
$cd
#j umptous er'sHO MEfolde
r
$cd~
#j umptous er'sHO MEfolde
r
$cd/ home/$USER
#j umptous er'sHO MEfolde
r
$cd~ /bash_cour
se/subdir1
/subd
ir2
#jumpt os ub
dir2
$cd. .
#j umptopa rentfo lde
r,s u
bdir1
$cd. ./..
#j umptopa rent'spa r
entfolder,/h
ome/$
USER
$cd/
#j umptoro otdi recto
ry
$cd..
#a ner rorwithoutas pace!!
help: man cd
30 / 142
cd 1. Create "bash_course" directory in your homedir
2. Create subdirectory "subdir" in "bash_course"
Move to newly created "subdir"
change directory 3. Create two nested subdirectories "subdir_a/subdir_b" in
"bash_course"
TIP: you can access parent dirrectory with '..'
Excersize Move to subdir_b
4. Move to root directory (/)
5. Move to HOME directory
help: man cd
31 / 142
Student edition, no solution provided
cd
change directory
Solution
help: man cd
32 / 142
cd a
a
l
l
i
i
a
s.
a
s.
.=
..
'c
d.
='
.'
cd../..'
alia
s....=
'cd../../..'
change directory alia
s.....
='cd../../../.
./..
'
alia
scd..=
'cd..'
Alias
help: man cd
33 / 142
touch touc
hf
#i
ile_
ffi
name
ledo esno
tex
ist,c
reat
eanemptyfile
change file TIP: touch command is used to change file access and modification
timestamps time, but it can also be used to quickly create a file.
34 / 142
cp $cps
#c
r
o
cds
t
pyf
ilea
tpat
h's
rc't
opat
h'ds
t'
$cp~/bash
_cour
se/te
st1.
txt~/
bash
_cour
se/te
st2.txt
$cp~/bash
_cour
se/te
st1.
txt~/
bash
_cour
se/su
bdir_a/
$cp~/bash
_cour
se/te
st1.
txt~/
bash
_cour
se/te
st_dir/test2.txt
#throwsanerror,mis
singt
est_
dir
$cp~/bash
_cour
se/su
bdir
1~ /ba
sh_c
ourse
/subd
ir_a
#throw
sa nerror
,byde fau
ltcpd oe
sn'tcopydirectories
$cp-r~ /b
ash_c
ourse
/sub
dir_a~/ba
sh_co
urse/
subdir_c
#withrecur
siveopti
on-r ,eve
nd ir
ector
iesarecopied
-f,--force
, #over
writedest
inati
onfi le
-i,--inter
activ
e,#prom
ptbe f
oreoverw
rite
help: man cp
35 / 142
cp 1. Create a new file test.txt in directory ~/bash_course
help: man cp
36 / 142
Student edition, no solution provided
cp
copy files and
directories
Solution
help: man cp
37 / 142
mv $mvsr
#m
cds
ov
t
efile(ord
ir)a
tpa
th's
rc't
opat
h'dst'
move (rename) mv works exactly like cp, with one major difference:
files
mv doesn't need r option to move directories
help: man mv
38 / 142
mv 1. Create a new files test1.txt, test2.txt, test3.txt, in directory
~/bash_course
Excersize 3. Move all files with extension .txt to new backup directory
help: man mv
39 / 142
Student edition, no solution provided
mv
move (rename)
files
Solution
help: man mv
40 / 142
pwd $p
/ho
w
d
m
e/eivar
ad
print working
directory
41 / 142
rm $r
$r
ms
m-
rc
rs rc
#re
mo
#re
mo
ve(d
ve(d
el
e
el
e
te
te
)fi
)di
l
r
e's
ec
to
r
r
c'
y'src'
help: man rm
42 / 142
rmdir $rm
dirsrc #re
moveempt
ydir'src
'
Example:
$'r
mdir-pa/b/
c' #i
ssi
milarto
$'r
mdira/
b/ca/ba'
43 / 142
rmdir 1.
2.
in ~/bash_course create directory delete_me
remove directory delete_me
3. in ~/bash_course create nested directories delete_me1/delete_me2/
remove empty 4. remove directory nested directories delete_me1/delete_me2/
directories
Excersize
44 / 142
Student edition, no solution provided
rmdir
remove empty
directories
Solution
45 / 142
find $f i
n
#
d.
findallfilesanddirecto
riesu nde
rc ur
rentdirectory
$f i
nd/
search for files in #findallfilesanddirecto
rieso nharddr
ive
$f i
nd.-n ame"file_
name
"
a directory #r egexissuppor
tedforf i
le_name
hierarchy $f i
ndDo cu
ments
/- na
me"New"
#f indeveryfilenam
edNe w
$f i
ndDo cu
ments
/- na
me"*New*"
#N ewiso nl
yp ar
toffi len
ame
Docu
ments/N
ewSpreads
heet
.ots
$fi
nd.-n
ame"
file_
name
"-pr
int-
exec"comm
and_name"{}\;
$f i
ndDo cu
ments
/- na
me"*New*"-pr
int-
execls-la{}\;
Docu
ments/N
ewSpreads
heet
.ots
-rw-
r--r--1e iv
aradrnd6391Jul 9 20
13Documents/NewSpreadsheet.ots
$f i
ndDo cu
ments
/- na
me"*New*
"-ex
ecmd5sum {}\;
808b
1073f3d
7f852
0201d
5722
2bb72
d2 D
ocume
nts/N
ewSpreadsheet.ots
46 / 142
find 1.
2.
goto your HOME folder
find all files and directories
3. find all files and directories with word "bash" in their name
search for files in 4. repeat previous search, but execute command "file" on each result
a directory
hierarchy
Excersize
47 / 142
Student edition, no solution provided
find
search for files in
a directory
hierarchy
Solution
48 / 142
date #b
$d
a
s
a
t
i
e
cc onfigur
ation
MonDec 812 :40
:50CET20 14
print or set the $d a
te+% Y
2014
system date and $d a
te+% y
time 14
$d a
te+% M
04
$d a
te+% d
28
$d a
te+% H
18
$d a
te+% m
11
$d a
te+% S
35
#a ndm anymore,see mandat
e
$d a
te+% Y-
%M-%d
-%H:%
m:%S
2014
-15-28-
18:11
:56
49 / 142
date 1. Echo a date of format: yy/mm/dd-hh:mm:ss
2. Echo current ISO week number (01..53)
Excersize
50 / 142
Student edition, no solution provided
date
print or set the
system date and
time
Solution
51 / 142
top $t
to
o
p
p-1 5:20
:35up6 0days
, 7: 0
8,32u se
rs, loadaverage:0.00,0.00,0
Task
s:32 7total
, 2r un
ning,325sleep
ing, 0stopped, 0zombie
display Linux Cpu(
s): 0.7%us, 2.5
%sy, 0.0%
ni,95.4%
id, 0.0%wa, 1.0%hi, 0.5%si,
Mem: 81 90
972ktotal
, 7801916
kused, 3 890
56kfree, 848324kbuffers
tasks Swap
: 34 42
680ktotal
, 1268
kused, 34414
12kfree, 5026288kcached
PI
DUSER PR N
I V
IRT R
ES SHRS%CPU%MEM TIME+ COMMAND
1
5root 20 0 0 0 0R 4 0.0377:17.63events/0
725
6cudbuser 20 067
028 1
2m3796S 4 0.2 0:03.43python
725
0cudbuser 20 067
028 1
2m3796S 1 0.2 0:03.44python
732
0cudbuser 20 063
380 1
0m3772S 1 0.1 0:00.45python
749
9eivarad 20 019
51615
681024R 1 0.0 0:00.16top
732
6cudbuser 20 063
380 1
0m3772S 1 0.1 0:00.31python
750
0cudbuser 20 016
97613
481116S 1 0.0 0:00.06telnet
1
7root 20 0 0 0 0S 0 0.0 89:48.17events/2
748
1cudbuser 20 016
97613
481112S 0 0.0 0:00.03telnet
748
2cudbuser 20 016
97613
481112S 0 0.0 0:00.03telnet
750
1cudbuser 20 016
97613
481116S 0 0.0 0:00.03telnet
3135
0michaelk 20 076
92053
921008S 0 0.1 12:38.22sshd
3140
5michaelk 20 042
14m 6
9m9188S 0 0.9 33:24.55java
3155
61211 20 038
22m 7
3m9616S 0 0.9 17:36.39java
3159
4ufw_auto 20 036
22m 6
1m8664S 0 0.8 17:47.16java
1root 20 0 8
668 6
88 636S 0 0.0 1:58.11init
52 / 142
ps #T
$p
os
s-
e
e
eeveryproces
sonthes
yste
mu si
ngstandardsyntax:
$ps-ef
report a $ps-eF
$ps-ely
snapshot of the $pskstart
_time-ef #so
rtbypro
cessstarttime
current processes
Long commands can be run in the background.
&
Run job in background.
A command followed by an & will run in the background.
sleep20
echo$PWD
sleep20&
echo$PWD
help: man ps
53 / 142
kill kill command is typically used to end a process.
Examples:
send a signal to a
process $ki
ll-9-1
#Killallp
roces
sesy
ouc
anki
ll.
$ki
ll-l11
#Trans
latenumbe
r11intoasi
gnalname.
$ki
ll-L
#Listthea
vaila
bles
igna
lcho
icesinanicetable.
$ki
ll1235432
3413453
#Sendthed
efaul
tsi g
nal,SIGT
ERM,toal
lthoseprocesses.
$ki
ll-93453
#Killproce
sseswithPID3
453.
54 / 142
kill 1. execute command sleep 200&
2. find PID of sleep process
3. kill sleep process
send a signal to a
process
Excersize
55 / 142
Student edition, no solution provided
kill
send a signal to a
process
Solution
56 / 142
history $h i
st
#p
ory
rintalistofl ast600(configur
abledefault)commands
#f orexampl
e:
manipulate the 464 find.
465 pwd
history list 466 ls
467 findDocume
nts/
468 findDocume
nts/-n ame"New
"
469 findDocume
nts/-n ame"*Ne
w*"
470 find.- nam
e" *New*"-exec"ls- la"{}\;
471 findDocume
nts/-n ame"*Ne
w*"-e xec"ls-la"{}\;
472 findDocume
nts/-n ame"*Ne
w*"-e xec"ls"{}\;
473 findDocume
nts/-n ame"*Ne
w*"-e xec"/bi
n/ls"{}\;
#toexecteaco
mmandfro
mthi
slis
t,us
eban
gsyntax
$!!
#W illexecu
tethela s
tco m
mand
.
#S ameas!-1o r"up-a
rrow+return"
$!n
#W illexecu
tetheli n
eno ftheh is
toryrecord.
$!-
n
#W illexecu
tetheco m
mandnlinesback.
$!g
zip
#W illre-ex
ecutethemostrece
ntgzipco mmand
#( hist
oryiss ea
rched inreve
rseorder)
#s trin
gs pe
cifie
disco nsi
dere
dt obepr efixofthe
# n ec
essar
yc om
mand.
57 / 142
history 1.
2.
list command history
execute pwdcommand
3. reexecute pwdcommand using history bang syntax
manipulate the 4. reexecute last lscommand using history bang syntax
history list 5. list entire history and reexecute any command using !X
XXsyntax
Excersize
58 / 142
Student edition, no solution provided
history
manipulate the
history list
Solution
59 / 142
history alia
shi='h
istor
y'
manipulate the
history list
Alias
60 / 142
piping Passes the output (stdout) of a previous command to the input (stdin) of
the next one, or to the shell. This is a method of chaining commands
together.
interprocess General format is:
communication
$pr
ocess1|pre
cess2
Example:
$ec
ho"/ho
me/ei
varad
"|l s-la
#pipe(|)sendsoutp
utoffirs
tcom
mand(echo)
#asinputtos ec
ondcomma
nd(l
s-la
)
61 / 142
grep #e
#t
x
a
h
i
ctd
stu
et
ai
to
ri
l
a
sdepen
liswr
dve
it
r
te
si
nf
onan
or
dimple
menta
tion
$g r
ep--ve
rsion
print lines GNUgrep2.
5.2
matching a -i,--ignor
e-cas
e
pattern -v,--inver
t-mat
ch
#S elec
tn on
-matc
hinglines
.
-c,--count
#P rintacountofma t
chinglin
esfore achinputfile.
#W iththe-v,countnon-ma
tchi
nglines.
-l,--files
-with
-matc
hes
#P rintthe nameof each inp
utfile f rom which
#o utpu
tw ou
ldnormall
yha v
ebeenprinted.
-q,--quiet
,- -s
ilent
#D onot write anyth
ing to stand
ard output.
#E xitimmed
iatel
ywi t
hze r
ostatusifan ymatchisfound.
-R,-r,- -r
ecurs
ive
#R ead all file
s u n
der each dir
ectory, recursively;
-CNUM,- NU
M,--conte
xt=NU
M
#P rintNUMlinesofoutputcon
text.
-E,--exten
ded-r
egexp
#I nter
pretPATTE
RNa sanexten
dedregularexpression
-P,--perl-
regex
p
#I nter
pretPATTE
RNa saP e
rlregula
re xpression.
#( Notfullyimpl
ement
ed)
62 / 142
grep --co
lo
#D
r[=
is
WH
EN
pl
],--
aymatc
c
h
ol
ou
esi
r[
=
nc
WH
ol
EN]
oronthe terminal.
-s,--no-me
ssage
s
print lines #S uppr
esserrormess
agesaboutnone
xistent
#o runreada
blefiles
.
matching a -n,--line-
numbe
r
pattern #P refi
xe ac
hl in
eofou tpu
twiththe1 -based
#l inenumbe
rw it
hinitsi n
putfile.
-w,--word-
regex
p
#S elec
t only those line
s contai
ning
#m atch
esthatformwholewords
.
andmanymo
re...seem
angr
ep
63 / 142
grep grep reads input stream line by line, looking for a match
$l s|gr eptest
print lines test
matching a test.py
test.sh
pattern test.sh~
grep can search through file line by line, looking for a match
$g repec ho<te
st.sh
echo$ lists
echo
$g repec ho./te
st.sh
echo$ lists
echo
$g repec hotest
.sh
echo$ lists
echo
$c attest.
sh|grepecho
echo$lists
echo
64 / 142
grep grep can also search through file content of many files at once
$g r
eppr in
t*
print lines test
.py:pri
nt"D
one"
matching a
pattern
$g r
epgi t*.sh
cher
ry.sh:e
cho" Hello
,Ia myourgitc he
rry-p
ickscript"
cher
ry.sh:g
itch erry-
pick$com
mit
cher
ry.sh:e
cho" gitpushorigi
nH EA
D:ref
s/for
/cotsfailed!"
cher
ry.sh:e
cho" gitcherry
-pic
kf ai
led!!"
test
.sh:ori
g="Tos sh:
//eiv
arad
@gerr
it.ip
:port
/project.git"
$g r
ep-Rt est*
sour
ces/Mon
itorT
hread
.hpp
: i
nttest()
;
sour
ces/Mon
itorT
hread
.hpp
: v
irtu
alintt es
tConn()=0;
sour
ces/Mon
itorT
hread
.hpp
: i
nttestCon
n();
sour
ces/Mon
itorT
hread
.hpp
: i
nttestCo
nn();
65 / 142
grep $c
Fir
a
s
te
tl
xa
in
mp
e
le.tx
t
UNIXo peratingsystem
print lines ThirdL ine
UNIXa ndLi nux1opera
tingsyst
ems
matching a FifthL ine
pattern Linux2op eratingsyst
em
Seventhl ine
$g repUN IXexam
ple.t
xt
UNIXo perat
ingsystem
UNIXa ndLinux1opera
tingsyst
em
$g repUN IXexam
ple.t
xt|grep-vLinux
UNIXo perat
ingsystem
$c ate xamp
le.tx
t|g repU
NIX|g re
p- vLinux
UNIXo perat
ingsystem
$gr
ep-cU
NIXe
xampl
e.tx
t
2
$g r
ep-- context
=1Linux2exam
ple.t
xt
Fift
hl ine
Linu
x2op eratingsyst
em
Seve
nthl ine
help: man grep
66 / 142
grep #e
$g
x
r
t
e
ende
p-E"
dgr
UN
e
I
pmo
X|
Li
d
n
e
ux"ex a
mple
.txt
UNIXo perat
ingsystem
print lines UNIXa ndLinux1operatingsyst
ems
Linux2op er
atingsystem
matching a
pattern
NOTE:
$g r
ep-iL inee
xampl
e.tx
t
Firs
tl ine
Thir
dL ine
Fift
hL ine
Seve
nthl ine
$g rep-ws ystemexam
ple.
txt
UNIXo perat
ingsystem
Linux2op er
atingsyst
em
67 / 142
grep $g
che
r
e
r
r
p-lg
y.sh
it*.
sh
test
.sh
print lines
matching a $g r
ep-ng it*.sh
pattern cher
ry.sh:6
:echo"Hel
lo,Ia myourgitcherry
-pickscript"
cher
ry.sh:1
6:gitcher
ry-p
ick$commi
t
cher
ry.sh:2
2:ech
o" gi
tpushoriginHEAD:
refs/
for/cotsfailed!"
cher
ry.sh:3
4:ech
o" gi
tcherry-
pickfaile
d! !"
test
.sh:4:o
rig="
Tossh://
eivar
ad@ge
rrit.
ip:po
rt/project.git"
$grep-E" UN IX|
Linux"example.txt--col
or
UNI
Xo pe
rat ingsystem
UNI
Xa nd Linux1op er
atings ystems
Lin
ux2oper atingsystem
68 / 142
grep 1.
2.
copy ~eivarad/bash_course/test_files to your working directory
go to test_files directory
3. grep all files that have "test" in their name
print lines 4. grep all files that have "test" in their name and extension ".txt"
matching a 5. repeat last two steps recursively
pattern 6. grep recursively for files that contain word "This" (print just file
name)
7. grep all files that contain words UNIX and Linux
Excersize 8.
9.
grep all files that contain only word UNIX, but not Linux
grep all files that contain word "Line", in both lower and UPPER
case
10. grep all files that contain word "distribution"
11. grep all files that contain exact word "distribution" (search string
must not be a part of larger string)
12. grep count of word "Linux" in files with extension ".txt"
13. grep all files that contain word "Linux", but print also a context of
one line around matching lines.
14. repeat previous search, but print results colored
15. repeat previous search, but print also the line number
69 / 142
Student edition, no solution provided
grep
print lines
matching a
pattern
Solution
70 / 142
sed sed works as a filter
sed is line oriented
simplest commands are a pattern and a command
stream editor for if no pattern is given, the command is applied to all lines
filtering and
transforming Most typical use of sed if for text substitution
text
$se
d's/ol
d/new
/fil
e'
Example:
$c a
ts ed_t
est_f
ile.t
xt
sedcanb egreatifnotoverus
ed
sedstandsforStreamEDi
tor
Ih a
vealo ve/ha
terelati
onshi
pw it
hsed
$s e
d' s/se
d/SED
/'sed_te
st_fi
le.tx
t
SEDcanb egreatifnotoverus
ed
SEDstandsforStreamEDi
tor
help: man sed Ih a
vealo ve/ha
terelati
onshi
pw it
hSED
71 / 142
sed Use of forward slash as a separator means things can get really ugly
really fast, if you try to use sed on path strings:
$se
d's:/h
ome/e
ivara
d/te
st/:/
temp/
cours
e/:'file
$s e
d' s:se
d:SED
:'sed_te
st_fi
le.tx
t
SEDcanb egreatifnotoverus
ed
SEDstandsforStreamEDi
tor
Ih a
vealo ve/ha
terelati
onshi
pw it
hSED
72 / 142
sed In sed special character & represents matched string.
$sed' s/^[
0-9]/
&0/'sed_
test_
file.
txt
stream editor for 10line1 :sedcanbeg re
atifno toveru
sed
filtering and 20sedst an
dsforS tr
eamEDito
r
30Ih aveal ove
/haterel
ation
shipwithsed
transforming
text
Or you could use standard regex syntax:
$sed' s:\(
^[0-9
]\):\
10:'sed_
test_
file.
txt
10line1 :sedcanbeg rea
tifno toveru
sed
20sedst an
dsforS tr
eamEDito
r
30Ih aveal ove
/haterela
tion
shipwithsed
Another example:
$se
d' s:\(
sedcan.*\
)\(i
f\)\(
.*ove
rused
\):\1when\3:'sed_test_file.txt
1li
ne1:s edcanb egrea
tw he
nn otover
used
2se
ds tand
sf orStre
amEDitor
3Ihavealo ve/
haterela
tions
hipwithsed
73 / 142
sed By default sed will only find the first occurrence in a line. If you wish
to change every occurrence, use /g or global replacement.
$se
d' s/se
d/SED
/g'sed_t
est_fi
le.t
xt
1li
ne1:S EDcanb egrea
ti fnotoveruS
ED
2SE
Ds tand
sf orStre
amEDitor(SED
)
3Ihavealo ve/
haterela
tionsh
ipwithSED
$se
d' s/ed
itor/
EDITO
R/I'sed_t
est_
file.
txt
1li
ne1:s edcanb egreatifnotoverus
ed
2se
ds tand
sf orStre
amE D
ITOR(sed
)
3Ihavealo ve/
haterelat
ionsh
ipwithsed
Multiple command:
$se
d- e's/sed/
SED/g
'-e's /ed
itor
/EDIT
OR/I'sed_test_file.txt
1li
ne1:S EDcanb egrea
ti fnotoveruS
ED
2SE
Ds tand
sf orStre
amEDITOR(SED
)
3Ihavealo ve/
haterela
tionsh
ipwithSED
74 / 142
sed 1. goto ~/bash_course directory
2. create a text file test_sed.txt with this content: "sed stands for
Stream EDitor (sed)" "sed runs on UNIX" "sed runs on Linux too"
stream editor for "linux is similar to unix"
filtering and 3. read test_sed.txt and convert word runs to upper case
transforming 4. read test_sed.txt and convert word sed to upper case
text 5. read test_sed.txt and convert every word unix to minix, ignore case
when searching
6. read test_sed.txt and convert every word unix and linux to upper
Excersize case
75 / 142
Student edition, no solution provided
sed
stream editor for
filtering and
transforming
text
Solution
76 / 142
wc -c,--
#p
byt
ri
es
ntthebytecount
s
-m,--chars
print newline, #p rintthechara
ctercoun
ts
-l,--lines
word, and byte #p rintthenewli
nec o
unts
counts for each -L,--max-l
ine-l
ength
file #p rintthelengt
hofth elonge
stli
ne
-w,--words
#p rintthewordcount
s
This simple but useful command can help you quickly count files.
$l s|wc- l
4
$l s
grep_test_f
ile1.
txt grep
_test
_file
2.txt sed
_test_file.txt
subfolder
help: man wc
77 / 142
wc $f
6
i
nd.|wc-l
$f i
nd.
print newline, .
./su
bfolder
word, and byte ./su
bfolder
/grep
_test
_fil
e3.lo
g
counts for each ./gr
ep_test
_file
1.txt
file ./gr
ep_test
_file
2.txt
./se
d_test_
file.
txt
$c atg rep_
test_
file1.txt|wc-w
62
$w c- ws ed
_test
_file.txt
25s ed_test
_file
.txt
help: man wc
78 / 142
wc 1. count files and directories in your HOME folder
2. count words in file ~/bash_course/test_sed.txt
3. count characters in file ~/bash_course/test_sed.txt
print newline,
word, and byte
counts for each
file
Excersize
help: man wc
79 / 142
Student edition, no solution provided
wc
print newline,
word, and byte
counts for each
file
Solution
help: man wc
80 / 142
head -c,--
#p
byt
ri
es
=[
ntt
-
h
]N
efirstNby
tesofeachfi
le;
#w iththeleadin
g`-'
,pr i
ntallbu
t
output the first #t helastNb yte
sofeachfile
part of files -n,--lines
=[-]N
#p rintthefirstNli
nesinste
adofthefirst10;
#w iththeleadin
g`-'
,pr i
ntallbu
t
#t helastNl ine
sofeachfile
$h ead-n2g rep
_test
_file
1.tx
t
Hellof romas im
plegreptestfile.
Thisi st heseco
ndline.
$h e
ad-c1 0gre
p_tes
t_fi
le1.t
xt
Hell
ofrom
81 / 142
head 1.
2.
save output of manheadto file head.txt
print first 10 lines of file head.txt
3. print first 5 lines of file head.txt
output the first 4. print first 50 characters of file head.txt
part of files
Excersize
82 / 142
Student edition, no solution provided
head
output the first
part of files
Solution
83 / 142
tail -c,--
#o
byt
ut
es
=[
pu
tt
-]N
helastNb
ytes;alte
rnati
vely,
#u se+Nt ooutpu
tby
tess t
arti
ngwiththeNthofeachfile
output the last
-n,--lines
=[-]N
part of files #o utpu
tt helastNl i
nes,inst
eadoft helast10;
#o ruse+ Ntooutputlinessta
rtingwiththeNth
$t ail-n2g rep
_test_fil
e2.tx
t
Justt woli
neslong.
$ta
il-c10gre
p_tes
t_fi
le2.t
xt
esl
ong.
84 / 142
tail 1.
2.
save output of mantailto file tail.txt_
print last 10 lines of file tail.txt_
3. print last 5 lines of file tail.txt_
output the last 4. print last 50 characters of file tail.txt_
part of files
Excersize
85 / 142
Student edition, no solution provided
tail
output the last
part of files
Solution
86 / 142
more more has several options, but we won't use any in this tutorial.
We will simply use defaults.
87 / 142
more 1. Find recursively all files in your home folder.
Use more to scroll line by line
Use more to scroll screenful by screenful.
filter for paging
through text one
screenful at a
time
Excersize
88 / 142
Student edition, no solution provided
more
filter for paging
through text one
screenful at a
time
Solution
89 / 142
less less is a program similar to more, but which allows backward
movement in the file as well as forward movement.
opposite of more Also, less does not have to read the entire input file before starting, so
:) with large input files it starts up faster than text editors like vi.
Example:
$f i
nd~|le ss
/hom
e/eivarad
/hom
e/eivarad/.login
/hom
e/eivarad/.cshrc
/hom
e/eivarad/.logout
/hom
e/eivarad/.profile
/hom
e/eivarad/.rhosts
line
s1 -31
But also:
Use up, down arrows to scroll line by line.
Use pageup, pagedown buttons to scroll screenful of text.
help: man less
90 / 142
less 1. Find recursively all files in your home folder.
Use less to scroll line by line up and down
Use less to scroll screenful by screenful up and down
opposite of more
:)
Excersize
91 / 142
Student edition, no solution provided
less
opposite of more
:)
Solution
92 / 142
tar tar saves many files together into a single tape or disk archive, and can
restore individual files from the archive.
$ta
rcvzfarchi
ve.ta
r.gzdire
ctory
/
$ta
rcvfjarchi
ve.ta
r.gzdire
ctory
/
93 / 142
tar Extracting uncompressed archive:
$ta
rxvfa
rchiv
e.tar
tar archiving
utility x extract files from archive
$ta
rxvfzarchi
ve.ta
r.gz
$ta
rxvfjarchi
ve.ta
r.bz
2
$ta
rxvfarchiv
e.tar/pa
th/to
/file
$ta
rxvfzarchi
ve.ta
r/path/t
o/fil
e
$ta
rxvfjarchi
ve.ta
r/path/t
o/fil
e
94 / 142
tar Extracting multiple files:
$ta
rxvf(z
j)ar
chive
.tar/pat
h/to/
file1/pat
h/to/file2
tar archiving
utility Extracting a single directory:
$ta
r(zj)x
vfar
chive
.tar/pat
h/to/
dir
$ta
rxvf(z
j)ar
chive
.tar/pat
h/to/
dir1/path
/to/dir2
$ta
rxvf(z
j)ar
chive
_fil
e--w
ildca
rds'
*.pl'
$ta
rrvfa
rchiv
e.tarfil
e
$ta
rrvfa
rchiv
e.tardir
/
95 / 142
tar Listing uncompressed archive:
$ta
rtvfa
rchiv
e.tar
tar archiving
utility x extract files from archive
$ta
rtvfzarchi
ve.ta
r.gz
$ta
rtvfjarchi
ve.ta
r.bz
2
$le
ssarch
ive.t
ar
$le
sstvfzarch
ive.t
ar.g
z
$le
sstvfjarch
ive.t
ar.b
z2
96 / 142
tar Verify extracted archive file using the option W:
$ta
rcvfWfile_
name.
tard
ir/
tar archiving
utility For daily backups use N option:
-N,--newer
=DATE
-OR-F
ILE,--af
ter-d
ate=D
ATE-O
R-FILE
#o nlystorefile
sne w
erthanDATE-O
R-FIL
E
$ta
rcjfb
ack-$
(date+%Y
%m%d)
.tbz2dir/
$t a
rc jfback-$
(date+%Y
%m%d)
.tbz2dir/-N
./ba
ck-$(da
te-dy est
erda
y+ %Y
%m%d)
97 / 142
tar Quick reference:
98 / 142
tar 1.
2.
goto your HOME directory
create uncompressed tar archive of directory ~/bash_course
3. create compressed gzip archive of directory ~/bash_course
tar archiving 4. create compressed bzip2 archive of directory ~/bash_course
utility 5. list all archive file, compare sizes
6. list content of each archive
7. verify archive.tar
Excersize 8. extract all .txt files from archive.tar.gzip
99 / 142
Student edition, no solution provided
tar
tar archiving
utility
Solution
100 / 142
id Print information for USERNAME, or the current user.
$i d
print user uid=
110038(
eivar
ad)gid=1
115(r
nd)g roups
=1115
(rnd),
identity 2153
4(madri
dall)
,2154
7(ma
dridc
udb)
help: man id
101 / 142
chmod $l
tot
s-
al1
la
88
drwxr-xr-x27ei
varadeiv
arad 4096Pro 716
:02.
change file mode drwxr-xr-x 3ei
varadeiv
arad 4096Kol2322
:48..
drwx------ 3ei
varadeiv
arad 4096Kol2819
:22.adobe
bits -rw------- 1ei
varadeiv
arad10815Pro 618
:07.bash_history
-rw-r--r-- 1ei
varadeiv
arad 2 20Kol2322
:48.bash_logout
-rw-r--r-- 1ei
varadeiv
arad 1 06Ruj 217
:15.bashrc
drwxr-xr-x 2ei
varadeiv
arad 4096Ruj 413
:23Desktop
drwxr-xr-x 7ei
varadeiv
arad 4096Pro 209
:46Documents
...
Lets break down the first column: (e.g. d rwx rx rx )
NOTE: there are three groups of permissions, for user, group and others
102 / 142
chmod General format:
$ch
modwho
=perm
issio
nsf
ile
change file mode
bits who can be:
103 / 142
chmod Examples in descriptive notation:
$ch
moda=rfile #allo
wany
onet
orea
dthefile
change file mode
bits
$ch
modo=file #remo
veal
lper
missi
onsf
orothers
$ch
modu=r
wxfi
le#setread,writ
ea ndexec
ute
#perm
ission
stou ser
$ch
moda+xfile #setexecu
tepermiss
ionf
oreveryone
#with
outchangi
ngreadorwritepermissions
$ch
moda-xfile #remo
veex
ecuteperm
issio
nforeveryone
#with
outc
hangi
ngreadorwritepermissions
104 / 142
chmod Octal notation:
4=r
change file mode 2=w
bits 1=x
Triplets:
$ch
mod764file #rwxf
orus
er
#rw-f
oragrou
p
#r--f
orot
her#
$ch
mod777file #rwxf
ora
ll
$ch
mod700file #rwxf
oruser
#nope
rmis
sionsforgroupandothers
105 / 142
chmod 1.
2.
create a new file test_permission.txt
print file's permissions
3. change file's permissions to (rw)(r)(r) use octal notation
change file mode 4. change file's permissions to (rwx)(rx)(rx) use descriptive notation
bits 5. change file's permissions to (rwx)(rwx)(rwx) use octal notation
Excersize
106 / 142
Student edition, no solution provided
chmod
change file mode
bits
Solution
107 / 142
ifconfig Not to be confused with similar MS Windows command ipconfig !
In this course we will use ifconfig to quickly determine ip address of
our Linux cloud server:
configure a
network $i f
config
interface eth0 Linkencap:
Ethe
rnet HWad
dr96:36:9
7:AF:9C:4D
inetaddr:1
47.2
14.13.
89 Bcast
:147.
214.13.255 Mask:255.255.25
inet6addr:fe8
0::943
6:97
ff:fe
af:9c
4d/64Scope:Link
UPB RO
ADCAS
TRUNNINGMULT
ICAST MTU
:1500 Metric:1
RXp ac
kets:
2494
533762err
ors:0drop
ped:0overruns:0frame:0
TXp ac
kets:
1974
508985err
ors:0drop
ped:0overruns:0carrier:0
collis
ions:
0txqueuel
en:1
000
RXb yt
es:15
5607
791691
0(148399
1.5Mb) TXbytes:2017421675842
lo L
inkencap:
Loca
lL oop
back
i
netaddr:1
27.0
.0.1 Mask
:255.
0.0.0
i
net6addr:::1
/128Scope
:Host
U
PL OO
PBACKRUN
NING MTU:
16436 Met
ric:1
R
Xp ac
kets:
1357
37010erro
rs:0dropp
ed:0overruns:0frame:0
T
Xp ac
kets:
1357
37010erro
rs:0dropp
ed:0overruns:0carrier:0
c
ollis
ions:
0txqueuel
en:0
R
Xb yt
es:63
1176
03645(601
93.6Mb) TXbytes:63117603645(60193
108 / 142
ifconfig 1. determine ip address of your server
configure a
network
interface
Excersize
109 / 142
Student edition, no solution provided
ifconfig
configure a
network
interface
Solution
110 / 142
Variables You can use variables as in any programming languages.
There are no data types.
A variable in bash can contain a number, a character, a string of
wonderful world characters.
of bash variables By default all variables in bash are text strings, therefore these are
:) equivalent:
$VA
R=testi
ng
$VA
R="test
ing"
$VA
R=1
$VA
R="1"
$V A
R="Hell
oWor
ld!"
$e c
ho$V AR
Hell
oW orld
!
111 / 142
Variables If you wish to deal with numbers, you need to use special syntax:
$C OU
NT=6
$e ch
o$ COUNT
6
$l etCOUNT=COU
NT-1
echo$COUNT
5
#
#i fyoud efini
telyneedspac
es,u
sequ
otes
:
let"COUNT=C O
UNT+ 1"
$COU
NT=exp
r$CO
UNT+3
112 / 142
Variables 1.
2.
create variable IVAN and assign it an output from m
print variable IVAN to screen
anc p
Excersize
113 / 142
Student edition, no solution provided
Variables
wonderful world
of bash variables
:)
Solution
114 / 142
Putty bash can be installed on MS Windows too.
http://winbash.sourceforge.net/
what about poor http://cygwin.com/
windows users? https://msysgit.github.io/
But we are simply going to install a ssh and scp clients ad connect to
Linux server:
http://www.putty.org/
https://code.google.com/p/superputty/
http://winscp.net/eng/index.php
http://www.notepadplusplus.org/
a good MS Windows text editor
115 / 142
Putty open superputty and connect to our cloud server use ifconfig
command to determine server ip address
what about poor open Winscp and also connect to cloud server
windows users?
integrate Putty
(Options/Preferences/Integration/Applications)
optional: setup Ntepad++ to be default editor in Winscp
(Options/Preferences/Editor)
116 / 142
Putty 1. list content of your cloud HOME folder from putty console
2. copy a file to your cloud HOME folder using Winscp
Excersize
117 / 142
Student edition, no solution provided
Putty
what about poor
windows users?
Solution
118 / 142
ssh ssh (SSH client) is a program for logging into a remote machine and
for executing commands on a remote machine. It is intended to replace
rlogin and rsh, and provide secure encrypted communications between
OpenSSH SSH two untrusted hosts over an insecure network.
client (remote
login program) Typical usage:
$s s
hu ser@
addre
ss:po
rt
--pr
omptforp as
sword
--
$s s
ha ddre
ss:po
rt-ll og
in_na
me
--pr
omptforp as
sword
--
$ss
huser@
addre
ss:po
rt'
comma
ndst
ring'
$ss
h-nus
er@ad
dress
:por
t'co
mmandstri
ng'
119 / 142
ssh $s
pas
s
hl
s
wor
oc
d:
alhost'ls-la~'
tota
l2 624
OpenSSH SSH drwx
--x--x50ei varadrnd 8192D
ec 817:3
8.
drwx
r-xr-x73ro ot r oo
t 0D
ec 817:2
0..
client (remote -rw-
------ 1ei varadrnd 10
681N
ov2618:1
7.bash_history
login program) -rw-
r--r-- 1ei varadrnd 1530F
eb17 201
4.bashrc
...
$ex
it
#c loseconn
ectio
nwi
thex
itco
mmand
120 / 142
ssh 1. start MSGIT console (right click on any directory, and select "git
bash")
2. use ssh command to connect to company's cloud server
OpenSSH SSH TIP: obtain ip address with command ifconfig
client (remote TIP: use format ssh user@address:22
login program) 3. print working directory
4. list content of your HOME directory
5. close ssh connection
Excersize 6. use ssh command to connect to company's cloud server and list
recursively content of your ~/bash_course directory
TIP: use format ssh user@address 'command'
121 / 142
Student edition, no solution provided
ssh
OpenSSH SSH
client (remote
login program)
Solution
122 / 142
scripts First we need to locate bash.
$w h
ichb as
h
creating simple /bin
/bash
bash scripts
Now create a new file hello.sh
$ge
dithel
lo.sh
&
#!/bi
n/bash
VAR="
HelloW orld"
echo$VAR
COURS
E=$(find~|g repcou
rse)
echo$COURSE
$c h
mod+ xhello
.sh
$. /
hello.s
h
Hell
oW orld
--li
stoff iles-
-
123 / 142
scripts If you wish to execute a command in your script, use tickle `` or $()
syntax:
$MA
N=$(manmkdi
r)
#Ip re
ferthisf
orma
tbec
auseofbe
tterreadability
#!/bi
n/bash
echo-e"H i,e nteroneword
:"
read word
echo"Thewo rdis :$wo
rd"
echo-e"E ntertw owor
ds?"
readword1w ord2
echo"Youty ped:\"$wo
rd1\"\"$
word2
\""
$. /
read.sh
Hi,enteroneword:one
Thewordis:o ne
Ente
rt wowords?twothree
Youtyped:"two""thr
ee"
124 / 142
scripts Exiting and return value. You can exit at any point from your script by
calling command exit:
$d a
te
MonDec 820 :43
:30CET2
014
$e c
ho$?
0
$a
a:Commandnotfound.
$e c
ho$?
1
125 / 142
scripts 1. create a script that asks use for ssh server ip address,
connects to that address and
recursively lists content of user's HOME folder
creating simple
bash scripts
Excersize
126 / 142
Student edition, no solution provided
scripts
creating simple
bash scripts
Solution
127 / 142
expect expect is a tool for creating interactive scipts.
#!/us
r/bin/expect
expec
t" hi"
send"hellowo rld
\n"
$c h
mod+ xtest.
exp
$. /
test.ex
p
abc
Hi
hi
hell
ow orld
When you execute this, the script will wait for the string "hi" to be
entered. When this string is received, script will immediately print
"hello world" and exit. If you enter anything other than "hi", script will
continue waiting
128 / 142
expect Useful expect options:
set timeout 60
programmed limit execution of the script,
dialogue with 1 wait forever, 0 exits immediately
interactive
programs set allow patternaction pairs:
expec
t"hi
" {send"Yousaidhi\n
"}\
"hell
o" {sen
d"H e
lloyours
elf\n
"}\
"bye" {sen
d"G o
od-by
ecruelworld\
n"}
To spawn a command:
#!/u
sr/bin/
expec
t
settimeout60
setpass[lindex$arg
v0]
spawns shlocalh
ost'ls'
expect"* pa
sswor
d:"
send" $pass
\r"
interact
$t e
st.extsecre
t
--li
stoff iles-
-
129 / 142
expect 1. try to copy and execute example from the previous page, where
expect scripts excutes spawn ssh localhost 'ls'
programmed
dialogue with
interactive
programs
Excersize
130 / 142
Student edition, no solution provided
expect
programmed
dialogue with
interactive
programs
Solution
131 / 142
vi editor vi is a text editor.
It can be used to edit all kinds of plain text.
It is especially useful for editing programs.
a programmers
text editor vi has two main modes of operation: command mode, and insert mode.
running in When you first load the editor, you will be placed into command mode.
command line To switch into insert mode, simply press the 'i' key.
When you have finished typing, you may return to command mode.
This is done by pressing your 'Esc' key. In command mode, key presses
do not appear on the screen, but instead are used to indicate various
commands to vi.
If you are unsure which mode you are in, press 'Esc'. If you were in
insert mode, you will be returned to command mode. If you were
already in command mode, you will be left in command mode.
132 / 142
vi editor Using vi to create and write to file:
$vi
a programmers
text editor Switch to insert mode, by pressing the 'i' key.
running in
command line
Hell
oworld
~
~
:wvi_file.
txt
:q
133 / 142
vi editor Inserting and appending text :
i--insertstexttot
hel e
ftof cursor
a programmers I--insertsinthebe
ginni
ngofl in
e
a--appendstexttor
ightofcursor
text editor A--appendstotheen
dofli ne
running in
command line
Adding new line:
o--addanewli
nebe
lowthecu
rren
tlin
e
O--addsanewl
inea
bovethec
urre
ntli
ne.
134 / 142
vi editor Replacing a character & word:
r--replacethechara
cterabov
et hecurs
or.
a programmers R--replceschar
acter
sun t
ilEscisp res
sed.
cw--replac
esthew or
dfr o
mcursortothee ndindicatedby$sign.
text editor C--replace
st il
le ndofline.
running in
command line
Repeating last command:
.--repeatsthelasttext
.
yy- -c opiesthecurre
ntl i
neintobuffer
.
5yy- -c opie
s5l inesfromthecurre
ntline.
p- -p astesthecurren
tbu f
fer.
135 / 142
vi editor Searching:
:/nam
e- -&re tur
ns ea
rche
sf orthewordnameinthefile
a programmers n- -continu
essearchforw
ard.
N- -searche
sb ac
kward
s.
text editor
running in
command line
Substitution:
:s/<s
earch-s
tring
>/<re
plac
e-str
ing>/
g
Saving:
:w--savesthetextdoesnotquit.
:wq--saves&quittheeditor.
ZZ--save&q uittheedit
or.
:q!--Quitwitho
utsaving
.
136 / 142
regex Regular expression or regex for short is a way to find text patterns
within a body of text.
We are going to cover only the basics of it.
searching for
text patterns Character literals
any letter or number matches itself
dot (.) matches any character
if you want to match a dot, escape it .
asterisk () means zero or more repetition:
. is any text (even no text, or empty string)
plus (+) means one or more repetition:
.+ is any text (but not an empty string)
question mark (?) means zero or one time:
.? is one character (even no text)
caret (^) means "at the beginning of line"
dollar (&) means "at the end of line"
^ivan$ means a line with only word ivan in it
Character classes
[azAD] means any lower letter, and only capital letters ABCD
[^09] means anything without digits in it, ^ is negation in
character classes
Alternation
unix|linux means either "unix" or "linux"
137 / 142
regex Using vi to create and write to file:
$vi
searching for
text patterns Switch to insert mode, by pressing the 'i' key.
Hell
oworld
~
~
:wvi_file.
txt
:q
138 / 142
regex Inserting and appending text :
i--insertstexttot
hel e
ftof cursor
searching for I--insertsinthebe
ginni
ngofl in
e
a--appendstexttor
ightofcursor
text patterns A--appendstotheen
dofli ne
o--addanewli
nebe
lowthecu
rren
tlin
e
O--addsanewl
inea
bovethec
urre
ntli
ne.
139 / 142
regex Replacing a character & word:
r--replacethechara
cterabov
et hecurs
or.
searching for R--replceschar
acter
sun t
ilEscisp res
sed.
cw--replac
esthew or
dfr o
mcursortothee ndindicatedby$sign.
text patterns C--replace
st il
le ndofline.
.--repeatsthelasttext
.
yy- -c opiesthecurre
ntl i
neintobuffer
.
5yy- -c opie
s5l inesfromthecurre
ntline.
p- -p astesthecurren
tbu f
fer.
140 / 142
regex Searching:
:/nam
e- -&re tur
ns ea
rche
sf orthewordnameinthefile
searching for n- -continu
essearchforw
ard.
N- -searche
sb ac
kward
s.
text patterns
Substitution:
:s/<s
earch-s
tring
>/<re
plac
e-str
ing>/
g
Saving:
:w--savesthetextdoesnotquit.
:wq--saves&quittheeditor.
ZZ--save&q uittheedit
or.
:q!--Quitwitho
utsaving
.
141 / 142
the end Now that you now the basics of bash, continue exploring it on your
own:
:)
142 / 142