Download - Module 05 - GNU and Unix Commands
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Module 05
GNU and Unix Commands
Objectives
Work on the Command Line The Interactive Shell Command History and Editing Manpages
Process Text Streams Using Filters Perform Basic File Management
Filesystem Objects File-Naming Wildcards (File
Globbing) Use Streams, Pipes, and Redirects
Standard I/O and Default File Descriptors
Pipes Redirection Using the tee Command The xargs Command
Create, Monitor, and Kill Processes Processes Process Monitoring Signaling Active Processes Terminating Processes Shell Job Control
Modify Process Execution Priorities Search Text Files Using Regular
Expressions Regular Expression Syntax Using grep Using sed Examples
Perform Basic File Editing Operations Using vi Invoking vi vi Basics
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Work on the Command Line
For Linux system administration, a text interface is typically used, called the command line
It is the job of a program called a shell to provide the command prompt and to interpret commands
The shell provides an interface layer between the Linux kernel and the end user
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Work on the Command Line
The Interactive Shell The shell is a powerful programming environment, capable of
automating nearly anything you can imagine on your Linux system Shells can run standalone, as on a physical terminal, or within a
window in a GUI environment
Shell variable basics PS1
o This special variable holds the contents of the command prompt that are displayed when bash is ready to accept commands
o Display the contents of PS1:
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Work on the Command Line
Entering commands at the command prompt Consist of four components:
o A valid commando Command options, usually preceded by a dash (-)o Argumentso Line acceptance (i.e., pressing the Enter key)
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Work on the Command Line
Entering multiple-line commands interactively Looping constructs, including for, until, and while, are often used
this way. Exp: if you wanted to repetitively execute a series of commands
each time with a different argument from a known series, you could enter the following:
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Work on the Command Line
Entering command sequences There may be times when it is convenient to place multiple
commands on a single line
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Work on the Command Line
Command History and Editing The history list is controlled by the HISTSIZE shell variable By default, HISTSIZE is set to 1,000 lines, but you can control that
number by simply adjusting HISTSIZE’s value history command
Command substitution This feature allows you to replace the result of a command with a
script
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Work on the Command Line
Man pages Although the books were convenient, many users didn’t always
want to dig through printed documentation or carry it around The man (manual) command was created to put the books on the
system, giving users immediate access to the information they needed
There is a manpage for most commands on your system
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Work on the Command Line
Man pages
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
Cat command: Concatenate files and print on the standard output
Cut command: Cut out (that is, print) selected columns or fields from one or more
files The source file is not changed
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
expand command: Convert Tabs to spaces. This command eliminates Tabs and replaces them with the
equivalent number of spaces. By default, Tabs are assumed to be eight spaces apart
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
fmt command: It breaks long lines and joins short lines as required, but doesn't
remove empty lines. Set line width. The default is 75 characters
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
head command: Print the first few lines of one or more files When more than one file is specified, a header is printed at the
beginning of each file
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
join command: join two files based on a common field -1 N : Nth field in 1st file -2 N : Nth field in 2nd file
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
nl command: Number the lines of files
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
od command: This program prints a listing of a file’s contents in a variety of
formats
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
paste command: Paste together corresponding lines of one or more files into vertical
columns. Similar in function to the join command, but simpler in scope.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
pr command: Convert a text file into a paginated, columnar version, with headers
and page fills The header will consist of the date and time, the filename, and a
page number.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
sort command: Write input to stdout (standard out), sorted alphabetically
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
split command: Split infile into a specified number of line groups, with output going
into a succession of files, outfileaa, outfileab, and so on
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
tac command: This command is named as an opposite for the cat command
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
tail command: Print the last few lines of one or more files The default is 10 last lines printed
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
tr command: Translate characters.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
unexpand command: Convert spaces to Tabs. This command performs the opposite action of expand command
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
uniq command: Writes input (or stdin) to output (or stdout), eliminating duplicate
lines.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Process Text Streams Using Filters
wc command: Print counts of characters, words, and lines for files
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
Directories and files A directory is a container intended to hold objects such as files and
other directories. A directory’s purpose is primarily for organization A file exists within the directory, and its purpose is to store raw
data. At the top of all Linux filesystem hierarchies is a directory depicted
simply by /; this is known as the root directory
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
Inodes Inodes carry information about objects, such as where they are located
on disk, their modification time, security settings, and so forth
File and directory management commands bzip2 cp cpio dd file find gzip and gunzip mkdir mv rm rmdir touch
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands cp
o Used to Copy
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands cpio
o cpio is used to create and extract archives, or copy files from one place to another.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands cpio
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands dd
o dd converts and copies fileso This is especially useful when performing backups of block
devices, such as hard drive partitions, CD-ROMs, or floppy disks
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands dd
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands file
o file is designed to determine the kind of file being queriedo Linux (and other Unix-likesystems) don’t require filename
extensions to determine the type of a file
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands find
o find searches recursively through directory trees for files or directories that match certain characteristics
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands gzip and gunzip
o Compress or uncompress fileso gzip is one of the most common compression formats found on
Linux systems, although it is starting to be replaced by the more efficient bzip2
o Files compressed with gzip usually have the extension .gz
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands gzip and gunzip
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands mkdir
o Create one or more directorieso You must have write permission in the directory where
directories are to be created
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands mkdir
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands mv
o Move or rename files and directories
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands rm
o Delete one or more files from the filesystemo The rm command also removes directories when the -d, -r, or -
R option is used.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands rmdir
o Delete directories, which must be empty.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File and directory management commands touch
o Change the access and/or modification times of files
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File-Naming Wildcards (File Globbing)
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Management
File-Naming Wildcards (File Globbing)
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Use Streams, Pipes, and Redirects
Among the many beauties of Linux and Unix systems is the notion that everything is a file
Things such as disk drives and their partitions, tape drives, terminals, serial ports, the mouse, and even audio are mapped into the filesystem.
This mapping allows programs to interact with many different devices and files in the same way
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Use Streams, Pipes, and Redirects
Standard I/O and Default File Descriptors a file descriptor is an abstract indicator for accessing a file
When a program is launched, it is automatically provided with three file descriptors
Use Streams, Pipes, and Redirects
Pipes From a program’s point of
view there is no difference between reading text data from a file and reading it from your keyboard.
Similarly, writing text to a file and writing text to a display are equivalent operation
As an extension of this idea, it is also possible to tie the output of one program to the input of another that called pipes (|)
Use Streams, Pipes, and Redirects
Redirection Each pipe symbol in the
previous pipeline example instructs the shell to feed output from one command into the input of another.
This action is a special form of redirection, which allows you to manage the origin of input streams and the destination of output streams.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Create, Monitor, and Kill Processes
Process The management and control of processes is also essential for
smooth system operation Every program, whether it’s a command, application, or script, that
runs on your system is a process Your shell is a process, and every command you execute from the
shell starts one or more processes of its own (referred to as child processes).
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Create, Monitor, and Kill Processes
Process Attributes and concepts associated with these processes include:
o Lifetime: length of time it takes to execute (while it “lives”)o Process ID (PID): Every process has a number assigned to it
when it startso User ID (UID) and Group ID (GID): process’s UID and GID are
associated with the user who started the processo Parent processo Parent process ID (PPID)o Environment: Each process holds a list of variables and their
associated values. Collectively, this list is known as the environment of the process
o Current working directory: The default directory associated with each process
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Create, Monitor, and Kill Processes
Process Monitoring Monitoring these processes is done using three convenient utilities:
ps, pstree, and top. ps command:
o This command generates a one-time snapshot of the current processes on standard output
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Create, Monitor, and Kill Processes
Process Monitoring pstree command:
o Displays a hierarchical list of processes in a tree format
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Create, Monitor, and Kill Processes
Process Monitoring top command:
o The top command offers output similar to ps, but in a continuously updated display
o This is useful for situations in which you need to watch the status of one or more processes or to see how they are using your system.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Create, Monitor, and Kill Processes
Process Monitoring free command:
o Display amount of free and used memory in the system.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Create, Monitor, and Kill Processes
Process Monitoring uptime command:
o One-line display of the following information: the current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes.
o Load average on a Linux system is – defined as the number of blocking processes in the run queue
averaged over a certain time period.
– A blocking process is a process that is waiting on a resource to continue, usually the CPU, disk I/O, or network
– This is usually indicative of a busy system
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Create, Monitor, and Kill Processes
Signaling Active Processes Each process running on your system listens for signals, simple
messages sent to the process either by the kernel or by a user For example, if you are executing a program from the command
line that appears to hang, you may elect to type Ctrl-C to abort the program. This action actually sends an SIGINT (interrupt signal) to the process, telling it to stop running.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Create, Monitor, and Kill Processes
Signaling Active Processes
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Create, Monitor, and Kill Processes
Signaling Active Processes Kill process
o The command kill sends the specified signal to the specified process or process group.
o If no signal is specified, the TERM signal is sent.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Create, Monitor, and Kill Processes
Signaling Active Processes Terminating Processes
o Occasionally, you’ll find a system showing symptoms of high CPU load or one that runs out of memory for no obvious reason. This often means an application has gone out of control on your system. You can use ps or top to identify processes that may be having a problem. Once you know the PID for the process, you can use the kill command to stop the process nicely with SIGTERM (kill -15 PID), escalating the signal to higher strengths if necessary until the process terminates
o Killing a process will also kill all of its child processes. For example, killing a shell will kill all the processes initiated from that shell, including other shells.
Create, Monitor, and Kill Processes
Shell Job Control When a process is put in the
background, you create a job Each job is assigned a job
number, starting at 1 and numbering sequentially
programs can be placed in the back-ground by adding a & character to the command
For example, if you start firefox from the command line, you don’t want the shell to sit and wait for it to terminate
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Create, Monitor, and Kill Processes
Shell Job Control bg command
o This command places the specified job in the back-groundo Using this command on a job that is stopped will allow it to run in
the background fg command
o This command places the specified job in the foreground jobs command
o List the active jobs
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Modify Process Execution Priorities
Part of Linux’s flexibility is to let users and administrators prioritize process execution
This feature is handy when you have a high-load machine and want to make sure special processes get more rights to use system resources than others
The priority of a process can be determined by examining the PRI column in the results produced from issuing either the top or ps -l commands
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Modify Process Execution Priorities
Nice One of the parameters used by the kernel to assign process priority
is supplied by the user and is called a nice number Nice numbers range from –20 to +19. Any user can start a process with a positive nice number, but only
the superuser (root) can lower a process’s nice number and thus raise its priority.
The lower the nice number, the higher the priority to the CPU.
The nice command works to change the nice number for new processes only at the time that they’re started. To modify a running program, use the renice command.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Modify Process Execution Priorities
Renice Alter the nicenumber to set the scheduling priority of one or more
running target processes By default, renice assumes that the targets are numeric PIDs.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Search Text Files Using Regular Expressions
Regular Expression Syntax
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Search Text Files Using Regular Expressions
Regular Expression Syntax
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Search Text Files Using Regular Expressions
Regular Expression Syntax
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Search Text Files Using Regular Expressions
Regular Expression Syntax
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Search Text Files Using Regular Expressions
Using grep
Search files or standard input for lines containing a match to regular expression regex
For example, to find all lines in file1 that contain either Linux or linux, you could use grep like this:
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Search Text Files Using Regular Expressions
Using Anchors Anchors are used to describe position information.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Search Text Files Using Regular Expressions
Using Groups and ranges Characters can be placed into groups and ranges to make regular
expressions more efficient.
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Search Text Files Using Regular Expressions
Using Modifiers Modifiers change the meaning of other characters in a regular
expression
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Editing Operations Using vi
vi Basics The vi editor has two modes of operation: command or insert In command mode, vi allows you to navigate around your file and
enter commands To enter new text,put vi into insert mode To switch from command to insert mode, press the “i” key To terminate insert mode, press the Escape key (Esc), which puts
you back in command mode
LPI Lin
ux Cer
tifica
tion
http://
www.bkacad
.com
Perform Basic File Editing Operations Using vi
vi Basics
<full key command: page 142>