Linux CH 5
Linux CH 5
5.1
NTRODUCTION
123
ncider two facts: First,
more than 90%
System. S Second, the most
Widely used search
of today' s 500
fastest
Doesn't this sound
interesting? engine, Google, uses supercomputers use Lnus
Linux
Low cost and
as its
Operating System to
superlor reliability have
tablets,
c
and personal
computers to gained space for
Linux in a wide
mainframes and variety of devices, Iron
[n the previous chapters, the supercomputers.
rticular operating system has beenoperating system is described in general. Except for some
particu
considered during the
description of various concepts of theexampics
y
This chapter is dedicated to a operating y em.
Linux was first developed by Linus Torvalds, a student in Finland, in 1991. But now, Linux is not
owned by anyone. No one company or individual "owns" Linux, Iike Windows is owned by a single company
Microsoft.
Linux is the most famous free and open source Operating System. Open source Operating Systems are
available in source-code format rather than as compiled binary code, Free software means not from a money
point of view. But, software that is free. like Linux, is distributed along with its source code. So, anyone who
receives it is free to make changes and redistributeit. You can change the source code of Linux as per your
equirements. Not only this, but you can also distribute this modified OS. You need to be concerned that you
an operating now
onwards,
g System. But from
Linux Operating fiystem
124
are given.
1o VISit a
readers are rcquested website
To get about LinuN installation and upgrade,
detailed information
So, rClders are requested
installation procedures.
Such as www.linux.org. Different flavors of Linux have different
to go through detailed user manuals of particular Linux distributions.
In such situation, a user has to decide which Operation System to boot into during the boot
process, i.e., when the computer system starts.
3. Virtual Installation
Linux can also be run as a virtual machine inside
another Operating System. For this, a
has to install some virtual server application such
usc
as
VMWare and then install Linux under
host software. u
Here. Linux runs above the host
Operating System, providing all
Linux functionalities.
4 Fresh Installation
This is the most popular installation. It installs Linux as the
Here, the hard disk is formatted, nd if there is
only Operating System
em of the computer
it is removed.
any existing Operating System in the compulet
The following sub-sections describe this kind of
installation
Linux Basics
on different partitions will not get affected it some problems arise With Operating System.
em
apt-get install:
ory
ion Syntax : 127
es
SS This section describes various
cribes what the shell 1s. The next
descrit
concepts related to a shell. It starts
of the shell. Next are Wild
topic covers the working of the with the basics about the snell, w
n characters, a set of special shelI, known as the interpretive
redirection, filtering and pipes provide a characters used for cycl
pattern-matching. The next three topies
Redirection provides a way to re-direct the wayandto combine simple commands to perform complex tasks.
e
that use both standard input as well as
input output of processes to various files. Filters are commands
standard output to
the output of one process to another perform their tasks. Pipes provide a way to feed
next. The last topic covers shell
directly. Shell special characters, called
metacharacters, are summarized
variables.
5,4.1 Basics
Shell- As an interface
Shell is an interface between the user and the kermel.
Users directly interact with the shell. Shell provides a prompt (command prompt) to the user where
the user can enter their commands.
executes these commands.
Shell interacts with the kernel and
available in Linux. These are the Bourne shell, C shell and Korn
There are mainly three different shells
shell.
which makes it difficult
The Bourne shell very compact syntax,
(sh) is the oldest among all shells. It has a
Read
Comnand
Display Interpret
prompt comand
Executee
conand
129
The shell performs the
tollowing activities in its
The shell displays the
interpretive cycle:
prompt. Then,
a user to enter a it waits for
command.
When a command 1S
entered, the shell
2 reads the command. It scans the
Tf it finds any such character, it command line for meta characters.
recreates the command in
It then passes the command to the
simplified form.
kernel for execution. During the execution of a
simply waits. It normaly does not do command, the she
any work during this time.
After the command execution is
complete, the prompt reappears. Shell is ready to start a new cycle
Users can enter another command now. now
This cycle starts when a user
logs in to the system and
terminates when the user logs out of the system.
Files
.4.4 Redirection: The Three Standard
from the sinell opens Dtndard input, 2. Standard Outnut
Every program that a user executes means
means of
of communication pro
c o m m u n i c a t i o n between the programs and exist
described below:
These three standard files
are
ne process can be told to read input from another source, such as a file or other process, instead of
a keyboard. Similarly, the process can be told to write output to another destination, such as a file or other
1. Re-directing Input
Here, a process is told to read input from some file stored on a disk instead of a keyboard.
wc command will read input from a file named test.txt rather than a keyboard.
2. Re-directing Output:
Here, a process is told to write output to some file
kept on disk instead of a monitor.
For this purpose >symbol is used with a file name.
General syntax: command > filename
Example:
WC test.txt
output.txt
we command will write output to a file named
output will not be output.txt rather than monitor. S0, the
displayed on the monitor screen but
a
will be stored in
3.
Re-directing Error a file dre
Here, a
process is told to write errOr
messages encountered
on disk instead of a monitor. during its execution to some file
For this purpose
2> symbol is used with a file name.
General syntax: Command 2>
filenamee
Example:
Wc test.txt
2> error.txt
(Here, 2 is file
descriptor number.)
Linux Basics
5.4.5 Filters
1. Basic file-handling commands like cp, mv and rm, and directory-oriented commands like mkdir, rmdir
and cd use neither standard input nor standard output.
2. Commands like ls, who, and pwd don't take any standard input, but they send their output to the
standard output.
3. Ip uses standard input but produces no standard output.
4. Commands like cat, we, od, emp, and gzip use both standard input and standard output.
Example
Consider the.following command:
command is a text-based calculator.)
result.txt (bc
be< input.txt >
and produces output to the screen of a monitor
firom a keyboard
takes input
By default, the bc command both are
re-directed.
standard input, as
well as output
C T e , its perTorms required operations, and stores
ores
from a file named
pu.t ,
this example, bc reads its input display
in nor monitor is used to output.
used to take input
result.txt. Neither
keyboard is
put tothe file
called filters.
Such commands are
This it possible to
Standard inpu another
process.
with
each
other. This
O to
connect
blocks.
USing simple prOcesses as building
Linux Operating System
132
Command2
Syntax: Commandl Iile. Instead, it is d.
Ouput ot Command1 is transferred to neither
monitor nor
anotner
reads neither
from the keyboard Instead
fed to the input of Command2. Similarly, Command2
1.
it directly accepts the output produced by Command
Example
mechanism.
Consider the following example to understand the piping
We-1
who
1his output is directly fed to the we.
Fiere, who command produces a list of users, one userper line.
it gives the number of users who
Command. It counts the number of lines in an input. So, in this example,
have logged in currently to the system.
5.4.7 Metacharacters
Metacharacters are a special set of characters. They contain special meaning to the Linux shell.
When a user enters a command, the shell reads the command and scans it to find metacharacters. If it
finds any such character, it recreates the command in the simplified form before presenting it to the kernel
for execution.
Metacharacters are nothing for commands, but they are important to the shell.
Various metacharacters have been described in previous sections. The following table given in Figure 5.3
summarizes the metacharacters used in Linux.
Suppose that the user has a file named prog, and he wants to remove
that file. So, he
like this - gives a command
rm
prog (Tm command removes the file.)
But, here, the shell interpretsas a metacharacter. It will be matched with any other characters.
any file starting with the name prog will be removed. So, what is Consequen
the solution
If a user wants to treat any of the
metacharacters as a normal
to this problem?
of shell, then there are two solutions to solve this
character, i.e., he doesn't want interrere
Both of these problem. These two solutions are
two remove the
special meaning of metacharacters. These two Escaping and Quoll
Escaping: ways are described as follow
Provide a \
(backslash) before
the wild card to
remove
So, in our
example, a command can be (escape) its special meaning.
given as:
rm progl*
Quoting
Enclose the metacharacter, or even the
entire
So, in pattern, within
our
example, a command can be given as:
quotes.
rm
progt
Nothing within these quotes 1S
special meaning. interpreted by the shell.
Shell lefts it S
alone without conside
LinuxBasics
133
am Metacharacter
Description
Linux interprets a
space as a
ctly separator, not as a character.
ead, A wild card character that matches any number of characters: For example, aDe all
the files that start with 'abc
A wild card character that matches any single character, Thus 1 s 222 lists files having names
3 characters long.
VC
A set of characters that can be matched. Thus ls [a-c]+ lists all files that begm wi
who
b, or C.
1s
whose value
Indicates that the following text is the name of a shell (environment) variable
$
to be used.
to current contents. ee
standard error to append
mand Re-direct the
name.
Introduces a job
ently, process
into the
background.
Place a
command. d
executed as a single
commands to be
of
rence 0 Encloses a sequence
oting. substitution.
sequences
of
Separates two
character, as in *
fails. treated
as
a plain
it 1s
so
m e t a c h a r a c t e r ,
the
Use
Sed to "quote" Figure 5 . 3 : Metacharacters
ing its
134 Linux Operating System
5.4.8 Shell Variables
LKe In other high-level programming languages, the shell also supports variables. They are
re used to used to
store
values. Here, variables divided into
are two categories:
User defined variables, and
Linux defined variables or System variables.
Case sensitivity:
Variable names are case sensitive, 1.e., Tenp and temp refer to two different variables.
Variable assignment:
A variable
assignment is of the form:
Variable=value
(no space around =)
For example, count=5
Variable evaluation:
An evaluation of a variable requires the $ as
For example,
a
prefix to the variable name.
echo $count, or
total=$count.
2. Linux defined variables or
System variables
These variables are defined by Linux itself.
They are used to customize the working
environment.
Generally, they contain some default values.
Users But, the user
can also read the values already set in can set its
these variables.
own values in these variaobles.
L i n u xB a s i c s
5.5 COMMANDS
and examples.
options,
usage,
syntax,
mmand description
includes
Lng Sy
136
Syntax: pwd
2. cd:
Change Directory
Usage: Change working directory.
Syntar: cd Tdirectory
Examples:
cd.change working directory to home directory
cd .change working directory to parent directory
cd /usr/lib ..change working directory to absolute path /usr/lib
cd doc/letters ...change working directory to relative path doc/letters.
3. mkdir:
e
Make irectory
Usage: Creates new directory.
Syntax: mkdir directory
Examples:
mkdir Test ..creates a new
directory named Test.
4. rmdir:
Remove Directory
Directory Name
Isage: Displays parent directory name or the
last
Syntax: dirname filepath component in the file
path.
Examples:
dirname
home/india/sample
ple.txt..displays home/india that is
directory
ory of sample.txt
L i n u xB a s i c s s
2. Is
List files
T long listing showing protections, number of links, owner, size, and time of last
modification
S size in kilobytes
3. cp
Copy command
file or group or
est 1000o o e
Usage: Copies a
file2...) target
Syntax: cp [ -irp 1 filel
Options: overwriting a destination file.)
i: Interactive copying
(Asks before
directory structure.)
r: Recursive copying
(It copies
mv
file
Move / Rename or group
of files.
a tile
moves
file or
Renames a
Usage: targer
1file2 ...]
mv I -if1 filel
Syntax:
filel, to file2
Examples ..Renames "
mv old
Linux oper.
Sys./ 2022/ 18
Linux Operating System
138 m
5. rm.
Remove file
Removes files (Deletes files)
Usage
Syntax: n1i 1-ill-r] file
Options:
i: Interactive removal (Asks before each removal.)
Options:
Counts number of lines
W Counts number of words
Counts number of characters
7. chmod:
Syntax:
chmod [ugoa][+ =][rwx file (Relative permissions)
chmod ### file (Absolute permissions)
Usage:
Changes file permissions
In Relative mode, the first
options indicate whose access is
to be changed u (user,
g (group), o (other), or a (al). The second you
options indicate the type of action
(delete), or =
(set). The last options
specify the access to be
+ad ). ite
delete), or x (execute). set (read), W
r
(W
In Absolute mode, three numbers
Each number is represent the protections for
a sum of I (for execute the user,
user, group and others
group an
read access). access), 2 (for write/delete access) an
ror write/delete access) and 4 (TOr
Examples:
chmod 0-rWX
test.out
.denies others from to
file test.out having any a
cess
cmp:
139
Compare
Usage:
Compares two files and
Syntax: gives the location of the first
cmp filel file2 misma
diff:
9.
Usage Compares two files, gives the
to make two files location of mismatch, as well
identical. as
suggests es
Options:
1: suppress lines unique to file1.
2: suppress lines unique to file2.
11. split:
contain n bytes.
new file will
b n: each
man
passwd, who,
tty,
5.5.4 General date, echo,
Purpose: cal,
. cal:
Calendar
Usage: Displays calendar.
current month
calendar of the
Syntax: ..displays
calendar of the particular year
cal ..displays
calendar of the
particular month and year
yeart
cal .displays
montht
yeart
cal
140
Linux Operating System
2. date:
Syntax: date
Usage: Displays current date and time
3. echo:
Examples:
echo "Hello world...!" ..displays a message
Syntax: passwd
Syntax:
who ..displays all users
who am i ...displays user of the current shell only
Usage:
Displays information about all users who have
logged-in to the system currentiy,
Information includes username, device name, date and time of logging in, and machine
name from where a has
user
logged in.
6. tty:
Syntax: tty
Usage:
Displays filename associated with terminal
Remember that all devices are treated as files in Linux
7. man:
cem Filters: head, tail, cut, paste, sort, tr, grep 141
head:
head test.txt
.displays first 10 lines of a file test. txt
head -20 test.txt
.displays first 20 lines of a file test.txt
2 tail:
Options:
n: indicates the number of lines to be displayed from the end of a file.
Examples:
tly. tail test.txt ... displays last 10 lines of a file test.txt
ine
tail -20 test.txt ..displays last 20 lines of a file test.txt
3. cut:
test.txt
cut f2,3
5. Sort:
Options:
c: check that input is in sorted order
case
f: sort upper and lower case letters together, i.e., 1gnore
m list: merges sorted files in the list
k m,n: start sort on the m field and end sort on the nth field
k m.n: start sort on the n column of the mth field
6. tr:
Translating Characters
Syntax: tr -cds] [string1] string2]
input
Usage: Translate characters in a file. It is used to
replace characters, delete characters, chang
case of a text as well as compress
multiple consecutive characters.
Options:
c: translate character not in
stringl
d: delete a character in stringl from the input
s: truncate repeated characters given in
stringl from the input.
strings: any pattern can be given as
strings
input: input can be standard input or
re-directed file,
Examples:
or piped process
1r
1 < test.txt
replace all : from the
tr a-z]' TA-Z]'
<test. Lxt test.txt file wi
.translates lower
tr -d T
<bdate. Ixt case letters to uppe case
.delete / from
tr -S
<test. txt bdate.txt file
...Compresses multiple
consecutive spac
i n u xB a s i c s
H r e p
to 143
Syntax: grep
loptions) patlern
Usage Displays that match the filenamel8)
lines from
files
programming. (EXam point of given pattern, Very
view also... ) useful command for she
Options:
c:
display the number of
matching lines
e: match multiple patterns
i: ignore case of letters during
: display
comparisons
only filenames of files
containing a
pattern
n:
display output with line numbers
V: reverSe
matching; display
lines that do not contain
pattern
0: show pattern cach time it found
in a file
Examples:
grep "hello" 1est.1xt . Simple pattern matching
grep "manager" W,1xt ..
displays filenames having patterns
grep V "Mr." Customer.txt . reverse matching
Process Status
Syntax: ps loptions
showing /dev/console)
1: long listing the
terminal
lerm, (say,
on
running
processes
t term:
Linux Operating System
144
2. time:
time Command 7 program
Syntax:
Usage: on the terminal.
time usage
command or program
and displays
EXecutes
three different time values:
in the form of
Here, time usage is displayed
command till its terminatin
invocation of the
) real time: total time from the
in the execution of itself,
actual time spent by a program
ii) user time:
the kernel doing work
on behalf of the user procese
ii) system time: time spent by
services to requests such as reading data from a
means providing
file stored on disk.
Examples:
time date
3. kill:
Usage:
Sends a signal to a process.
top 10
..displays output till 10
top -
root iterations
...displays all
processes invoked by user root
Linux Basics
,7
do,
thers: su, sude
logname, hostname, 145
Su:
exit, expr, tee, set
Switch User
Syntax: Su
Super User DO
Syntax: Sudo Command
Usage : Runs command with higher privileges like a super user. Used to perform restricted
operations.
Examples:
sudo reboot ...asks a user to enter a password and then allows reboot to execute,
which may not be possible without sudo.
3. logname:
Syntax: logname
Usage:
Displays the name of the current user.
that of who am i
The output will be the Same as
4 hostname:
Syntax: hostnamne
Usage: 201
hostname of the computer.
Displays the attached to the network.
name given tO the computer and
A hostname is a
5. exit:
Syntax: exit n)
Usage: closes terminal.
current shell und
Terminates
current code block like a loop or from
from the
useful to exit
If used in a shell script,
a program.
error.
exit with some
Options: lt zero,
normal exit. It non-zer0,
status.
n specifies exit