Unix Commands&Bash CODECDEMY
Unix Commands&Bash CODECDEMY
List l
The shell command ls is used to list the contents of directories If no arguments are given
it will list the contents of the current working director $ De k
e e. df
h . g
cd Change Directory
The shell command cd can be used to move throughout the les stem of a computer It
accepts a variet of arguments $ cd e-di ec
$ cd ..
Full le paths
Helper Commands
Helper commands for the command line include
C a dO i
O ion can be used to modify the behavior of shell commands. Shell command o ion are
commonly represented by a single letter preceded by a - . For example -l -a and
-d could all be options that follow a shell command.
m M e
The shell command m is used to move a le into a directory. Use m with the source le
as the rst argument and the destination directory as the second argument. $ i de .h eb i e/
m Re e
The shell command rm is used to delete les and directories. The -r ag deletes a
directory and all of its les and directories rm -r . $ - bad_ e fie
l Li C a dO i
The shell command ls is used to list the contents in a directory. It can be combined with
the following command options $ -a
$ -
-a lists all contents including hidden les and directories. $ -
-t lists all contents by the time they were last modi ed.
Cheatsheets Lea he C a dL e
Red ec gI a dO
A e d Red ec he c a d
The >> shell command is used to redirect the standard output of the command on the left
and append add it to the end of the le on the right # Thi command ill append "Hello Wo ld!" o g ee ing .
echo "Hello Wo ld!" >> g ee ing .
P e he c a d
The | command is called a i e It is used to i e or transfer the standard output from
the command on its left into the standard input of the command on its right # Fi , echo "Hello Wo ld" ill end Hello Wo ld o he
anda d o p .
# Ne , pipe ill an fe he anda d o p o he ne
command' anda d inp .
# Finall , c - ill co n he n mbe of o d f om i
anda d inp , hich i 2.
echo "Hello Wo ld" c -
Red ec gO
The > symbol is used to redirect output by taking the output from the command on the
left and passing as input to the le on the right echo "Hello" > hello.
ca D a
The shell command cat displays the contents of one or more les to the terminal
$ ca poem.
$ ca poem. ki ie .
g e Sea ch
The shell command grep is used to search les for lines that match a pattern and returns
the results Various options can be speci ed along with the grep command to specify the g ep ' onn ' name .
search
In the provided example the lines in the le a e which contain sonny will be
returned
Ca e e e ea ch
The shell grep command searches les for a particular pattern The grep command
with the -i option can be used to search les for lines that match a pattern case
insensitive and returns the results
ge R he c a d
The shell command grep has a -R option grep -R that searches all les in a
directory including its subdirectories and outputs lenames and lines containing matched
results
C a d L e Red ec
On a command line redirection is the process of using the input output of a le or
command to use it as an input for another le It is similar but di erent from pipes as it l > di ec o ie _li .
allows reading writing from les instead of only commands l >> di ec o ie _li .
Redirection can be done by using the operators > and >>
Cheatsheets Learn the Command Line
Alias
The shell command alia is sed to assign commonl sed commands to shortc ts or
aliases The assigned commonl sed command sho ld be rapped in do ble q otes # The follo ing command c ea e an alia ` d` fo he
command ` d`
Environment Variables
Variables that can be sed across terminal commands are called en i nmen a iable The
also hold information abo t the shell s en ironment
Export command
The e command makes a gi en ariable a ailable to all child sessions initiated from
the c rrent session # Thi command ill make he en i onmen a iable USER
a ailable
# o all child e ion i h he al e "Jane Doe".
e o USER="Jane Doe"
echo $HOME
Cheatsheets Lea Ba h Sc i i g
Ba h Sc i i g
Ba h Sc i A g me
Arg ments can be added to a bash script after the script s name Once pro ided the can be
accessed b sing $( e a e ) For e ample the #!/bi /ba h
rst arg ment can be accessed ith $1 the second ith $2 the third ith $3 etc # F a c i i ked b a c ed g ee b e
# ech e ed
ech $1
# ech e g ee
ech $2
# ech e b e
ech $3
Ba h c i a iable
Variables in a bash script can be set sing the = sign and accessed sing $
g ee i g="He "
ech $g ee i g
ba h c i read ke d
The ead command can be sed to prompt the ser for inp t It ill contin e to read
ser inp t ntil the Enter ke is pressed #!bi /ba h
Some prompt te t can also be speci ed sing - ith the ead command ech "P e E e c i e"
ead
ead - "E e a e: " a e
Ba h Sheba g
Bash script les start ith #!/b /ba This special line tells the comp ter to se
ba h as the intepreter
Ba h Alia e
Aliases can be created sing the ke ord a a The are sed to create shorter
commands for calling bash scripts The can also be sed to call bash scripts ith certain # F e a e, c ea e a a ia ha i ke he a c
arg ments # c i i h he a g e "g ee ", he f i g a i
ed:
a ia a g ee ='./ a c . h "g ee "'
Ba h Sc i
Re s able sets of ba h terminal commands can be created sing ba h c ip Ba h c ip
can r n an command that can be r n in a terminal
Ba h c i c m a i ea
In bash scripting strings are compared sing the == Eq al and != Not eq al
operators #!bi /ba h
d1="He "
d2="He "
d3="he "
if [ $ d1 == $ d2 ]
he
ech "S i g a e e a "
fi
if [ $ d1 != $ d3 ]
he
ech "S i g a e e a "
fi