Module Linux essentials - Module 8 introduce pipes, redirection and REGEX. After studying this chapter you should be able to understand pipes, redirection and partial POSIX; understand how to search and extrac data from files. Inviting you to refer.
Trang 1This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Module 8 Pipes, Redirection and
REGEX
Trang 2This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Exam Objective 3.2 Searching and Extracting
Data from Files
Objective Summary
Trang 3This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Command Line and
Redirection
Trang 4This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Command Line Pipes
• The pipe character ( | ) can be used
between two commands to send the
output of the first as input to the second:
• The output of ls /etc is sent to head
as input.
Trang 5This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Command Line Pipelines
pipelines The order in which commands are
added to the pipeline can affect the output:
Trang 6This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
– Standard Error (STERR) is the is the output produced
by the command when an error has occurred
STDOUT normally appears in the same window as
where command executed.
Trang 7This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
I/O Redirection Symbols
shell:
– < /path/to/file (Redirect STDIN from file)
– > /path/to/file (Redirect STDOUT overwriting file)
– >> /path/to/file (Redirect STDOUT appending file)
– 2> /path/to/file (Redirect STDERR overwriting file)
– 2>> /path/to/file (Redirect STDERR appending file)
– &> /path/to/file (Redirect STDERR and STDOUT
overwriting file)
– &>> /path/to/file (Redirect STDERR and STDOUT
appending file)
Trang 8This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
The null device
/dev/null file (Otherwise known as the “Bit Bucket”)
• This file is very useful in redirection of input and output
– any output redirected to /dev/null is discarded.
– /dev/null can be used for input to provide a stream
of null values.
Trang 9This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
STDIN, STDOUT, and
STDERR
Trang 10This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
STDIN or 0
the keyboard but can be redirected with the < symbol
them to process
providing data by the keyboard via STDIN, type CTRL-D
translates from one set of characters to another
• If you were the user typing the data to be
translated by the tr command, you would type CTRL-D when finished
Trang 11This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
STDIN from keyboard
translates from lowercase to uppercase after the
user typed the command and pressed Enter
• Then, "alpha" was typed and Enter pressed
Finally, the user typed CTRL-D
Trang 12This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Redirecting STDIN from file
uppercase with STDIN being redirected from the /etc/hosts file:
Trang 13This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
STDOUT or 1
command when operating correctly
the command is executed
to STDOUT
be redirected, as shown on the following slide
Trang 14This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Redirecting STDOUT
command is executed and the output appears
on STDOUT
redirects the output to the file a.txt
contents to STDOUT, so the output is shown
Trang 15This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Appending STDOUT redirection
will clobber, or overwrite, the file specified
• Using the double arrow >> for STDOUT
redirection will either create a new file or append
an existing one:
Trang 16This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
STDERR or 2
command after an error has occurred
• It is normally sent to the console/terminal where the command is executed
to be output to STDERR because the /fake file does not exist
Trang 17This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Redirecting STDERR
that would cause an error to be sent to STDERR which is then redirected to the /tmp/err.msg file
contents of the file to STDOUT to display the file:
Trang 18This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Disposing of STDERR
would cause STDERR to be redirected to the /dev/null file, in effect disposing of the error message
output
Trang 19This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Working with STDERR and
STDOUT
filesystem
locates a file that matches your criteria
access a directory
redirecting both STDOUT and STDERR on the following slides
later in this chapter
Trang 20This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
STDERR and STDOUT
Example
command searching recursively the /etc/pki directory for any files matching "*.crt"
messages appear:
Trang 21This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Isolating STDERR
redirected to the /dev/null file, so the
STDERR output alone is sent to the terminal window:
Trang 22This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Isolating STDOUT
redirected to /dev/null file, so the STDOUT output alone is sent to the terminal window:
Trang 23This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Redirecting Multiple Streams
Separately
to the crt.err file and the STDOUT output is sent
to the crt.txt file:
Trang 24This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Redirecting Multiple Streams
Combined
redirected into the same file, crt.all:
Trang 25This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
find Command
Trang 26This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Searching with find command
thousands of files making finding files
challenging
to search for files in different ways including:
– ownership
Trang 27This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Syntax of find command
• If the starting directory (start_dir) is not
specified, then the current directory is assumed
search will be done For example, use the
-name option to search by name
Trang 28This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Syntax of find command (cont'd)
• The search criteria (criteria) is the data to be
used with the search option So, if the search
option was -name, then the search criteria would
be the name of the file to find
which will output the names of the files that are found Other result options can perform actions
on the files that are found
Trang 29This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Searching by file name
find /etc/pki -name "*.crt"
/etc/pki directory
• Output any file names that match "*.crt" (anything that ends in ".crt“)
Trang 30This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Displaying file detail
• The option -ls will create output similar to the
ls -l command (show both)
permissions, link count, user owner, group
owner, size, date/time, and file name
Trang 31This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Searching by file size
by its size
Trang 32This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Useful options for find command
directory and its immediate subdirectories
the payroll group
by filename
the last ten minutes or less
the user bob
Trang 33This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
less Command
Trang 34This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Viewing files with less command
designed to display only one page of data at a time
that has less features than the less command
and forth with movement commands to view one page at a time
Trang 35This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
The help screen in less
• Once in the less program, pressing the "h" key will display the help screen:
Trang 36This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
less movement commands
has many movement commands The most
common commands are:
Trang 37This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
less searching commands
• Type / to search from cursor to end of file.
• Type ? to search from cursor to beginning of file.
next match or N to go to previous match.
Trang 38This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
head or tail
Trang 39This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Filtering with head
a file by default
be displayed to be specified
Trang 40This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
head with negative lines
number of lines specified from the top of the file
many lines from the bottom to not show
/etc/passwd except the last thirty-two
Trang 41This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Filtering with tail
a file by default
be displayed to be specified:
Trang 42This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
tail with positive lines
• If the -n option specifies the number of lines to
be displayed with a plus ( + ) prefix, then the tail command interprets that to mean to
display from that line number to the end of the file:
Trang 43This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Following with tail
a file and print them out as they occur by using -f option
• System administrators frequently follow log files
in order to troubleshoot system problems
when following with the –f option by using
CTRL-C
Trang 44This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
sort Command
Trang 45This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Sorting files or input
lines according to one or more fields you specify for sorting
with the –t option, you can specify the delimiter
can use the -r option to reverse the sorting of a field
• The default sort is a dictionary sort, but you can use the -n option to make it a numeric sort
Trang 46This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Example of sort
is sorted using a : character as a delimiter, by the fourth field numerically and then the third
field numerically in reverse:
Trang 47This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
File Statistics
Trang 48This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
File statistics with wc command
for each file it is given as an argument
bytes contained in each file
• If provided more than one file, then it also
calculates the totals of all files
• To view individual statistics, specify -l for lines, -w for words or -c for bytes
Trang 49This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Example of wc command
in the /etc/passwd and /etc/passwd- files, the following wc command could be executed:
Trang 50This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Using wc with pipes
that the output of a command can be analyzed
will count how many lines of output was
produced
directories are in the /etc directory, you could execute: ls /etc | wc -l
Trang 51This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
cut Command
Trang 52This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Filtering with cut command
• If you want to extract columns of text, then the cut command provides two simple techniques:
– By delimiter, where whitespace is the default The -d option can let you specify other delimiters and -f is used to indicate which fields to extract.
– By character position, using the -c option with the
range of the column to extract.
Trang 53This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Example of cut command
Trang 54This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
grep Command
Trang 55This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Filtering with grep command
standard input or the contents of a file for lines matching a specified pattern
word, appears within a file, then the grep
command is useful for that purpose
Trang 56This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Common grep options
Trang 57This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
Basic Regular Expressions
Trang 58This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses
©Copyright Network Development Group 2013
Basic Regular Expressions
used with the grep command without requiring
an option to use them (unlike Extended Regular Expression show later)
alphabetic or numeric characters that match
themselves
meaning of regular expression meta-characters, including the backslash itself