Lesson2 - Presentation
Lesson2 - Presentation
Objectives
Comman commands
Basic Terminology
Understanding the man pages
Redirecting Output
Chaining and Piping
2
Basic Terminology
#! is called a Shebang
~ is called a tilde
| is called a pipe
- is called a tack or minus
` is called a backtick (the lower case of the ~ beside 1)
3
CLI Help Syntax
The following special characters are used to define the command
syntax
Char Description
Identifies an optional argument. Arguments not enclosed in brackets are
[]
required.
... Indicates that you can specify multiple values for the previous argument.
Indicates mutually exclusive information. You can use the argument to the
| left of the separator or the argument to the right of the separator. You
cannot use both arguments in a single use of the command.
Delimits a set of mutually exclusive arguments when one of the arguments is
{}
required. If the arguments are optional, they are enclosed in brackets ([ ]). 4
What are the man pages?
The man pages short for manual pages are the help pages for the
Linux CLI.
6
Common Basic Commands
Take a look at the list available for GNU Core Utilities
https://en.wikipedia.org/wiki/List_of_GNU_Core_Utilities_commands
7
Command: pwd and cd
Format: pwd [options]
Format: cd directory
Command Description
pwd Display the current working directory
Change directory to dir, located in current
cd dir
working directory
cd ../ Change directory to parent directory
cd /home/franco Change directory to /home/franco
8
Command: ls
Format: ls [options] [directory]
Command Description
ls List all contents of the directory in alphabetical order
List all contents of the directory in list format with additional details in
ls -l
alphabetical order
List all contents of the directory including hidden files in list format
ls -la
with additional details in alphabetical order
List all contents of the directory in list format with additional details
ls -lh
with file size in a human readable format
9
Command: cat
Format: cat [options] [file]
Command Description
cat file.txt Display the text contents of file.txt
cat file.txt Display the text contents of file.txt and
file2.txt concatenate the contents of file2.txt
Display and concatenate the text contents
cat * of all files in the current working
directory
10
Command: echo
Format: echo [options] [string]
Command Description
echo hello Display the word 'hello' to the terminal
Display the variable $HOME to the
echo $HOME
terminal
Display the word 'hello' without a new line
echo -n hello
to the terminal
11
Command: cp
Format: cp [options] [dir]source [dir]destination
Command Description
cp file.txt file2.txt Copies file.txt to file2.txt
cp file.txt /home/franco Copies file.txt to /home/franco
Copies the directory dir to /home/franco/ creating the
cp -r dir /home/franco/
directory /home/franco/dir
If /home/franco/dir2 exists copy dir to
cp -r dir /home/franco/dir2/dir.
/home/franco/dir2 If /home/franco/dir2 does not exist then copy dir to
/home/franco/dir2
12
Command: mv
Format: mv [options] [dir]source [dir]destination
Command Description
mv file.txt file2.txt Moves (or renames) file.txt to file2.txt
mv file.txt /home/franco/ Moves file.txt to /home/franco/file.txt
mv file.txt Moves (and renames) file.txt to
/home/franco/file2.txt /home/franco/file2.txt
If /home/franco exists moves dir to
/home/franco/dir.
mv dir /home/franco/
If /home/franco does not exist then moves dir to
/home/franco
13
Command: rm
Format: rm [options] [file]
Command Description
rm file.txt Delete file.txt
Delete the directory /home/franco and all
of its contents.
rm -rf /home/franco
r= recursively
f=forcefully
Delete the contents of /home/franco but
rm -rf /home/franco/*
not the directory itself.
14
Command: rmdir
Format: rmdir [options] directory
Command Description
Delete dir directory in the current
rmdir dir
working directory, dir must be empty
rmdir Delete /home/franco/dir,
/home/franco/dir /home/franco/dir must be empty.
15
Command: grep
Format: grep [options] pattern [file]
Command Description
Display lines with the string '80' in
grep 80 httpd.conf
httpd.conf
Display lines without the string '80' in
grep -v 80 httpd.conf
httpd.conf
Display lines with the string '80' and
grep -n 80 httpd.conf
include the line number from httpd.conf
16
Command: head
Format: head [options] [file]
Command Description
head file.txt Display the first 10 lines of file.txt
head -n 2 file.txt Display the first 2 lines of file.txt
head -2 file.txt Display the first 2 lines of file.txt
head -n -2 file.txt Display all except the last 2 lines of file.txt
17
Command: tail
Format: head [options] [file]
Command Description
tail file.txt Display the last 10 lines of file.txt
tail -n 2 file.txt Display the last 2 lines of file.txt
tail -2 file.txt Display the last 2 lines of file.txt
tail -n +2 file.txt Display all lines including line 2 and forward of file.txt
Listen to file changes and display an on going feed of
tail -f file.txt
file.txt. This is very useful for displaying live log files
18
Command: sort
Format: sort [options] [file]
Command Description
sort file.txt Output file.txt in alphabetical order
sort -n file.txt Output file.txt in numerical order
Output file.txt in alphabetical order
sort -f file.txt
ignoring case
Output file.txt in numerical order sorting
sort -h file.txt by human readable size values like 2G or
5K 19
Command: cut
Format: cut [options] [file]
Command Description
Get columns 1 and 2 from a comma
cut -d, -f1,2 f.csv
separated file
cut -d'^' f3 f2.csv Get column 3 from a caret delimited file
20
Command: more and less
Format: more [options] [file]
Format: less [options] [file]
Command Description
Display the contents of file.txt and scroll
more file.txt
forward with spacebar (can't scroll backward)
Display the contents of file.txt and scroll with
less file.txt spacebar or arrow keys (scrolling forward and
backward are possible).
less -R file.txt Allows for colour formatting in less
21
STDIN
By default is attached to the keyboard
Data (usually text) supplied to a program from either the keyboard
or through a file
A command in linux wants some kind of input
cat [file] (file is optional and input can be taken from
STDIN)
Not all commands require input like ls
Standard input can be redirected through the use of arguments and
redirection operators so that it becomes a source other than the
keyboard 22
STDOUT
By default is attached to the terminal which is diplayed to your
monitor
Standard output is the stream where a program writes its output
data.
Most programs do not give you output based on successful
completion
Ex. mv
A majority of programs can have the option for verboseness
turned on
mv -v [source] [destination]
23
STDERR
Standard error is another output stream typically used by
programs to output error messages or diagnostics.
It is a stream independent of standard output and can be
redirected separately.
This comes in handy when chaining commands together.
By default is sent to STDOUT to be displayed to the terminal
24
Redirection
A Linux shell, such as BASH, receives input and sends output as
sequences or streams of characters.
You may wish to redirect stdin, stdout or stderr to something other
than their defaults.
By using special operators {< | > | >> | 2>} we can manipulate the
defaults of stdin stdout & stderr.
Common use example: One can take the stdout and redirect it to
stdin on the next program.
You can redirect output to /dev/null to have output ignored
25
Redirection Examples
Command Description
Write hello to the file ex.txt, if ex.txt
echo hello > ex.txt
exists overwrite ex.txt
Write hello to the file ex.txt, if ex.txt
echo hello >> ex.txt
exists append to ex.txt
Have STDIN for cat be read from
cat < ex.txt
ex.txt
ping google.ca 2> Redirect STDERR (error messages) to
error.log error.log
26
Chaining Commands
What if you wanted to run a command and when it finished, run
another?
What if you wanted to connect the output of one command to the
input of another?
What if you wanted to run commands depending on success or
failure of a previous command?
27
Chaining Commands (Continued)
Linux provides many ways to chain commands together. The ones we will discuss: