Linux OS Day 1 & 2
Linux OS Day 1 & 2
2
Agenda
Let me chk with the IT ppl
Introduction to Linux OS
Commands- basic and advanced
vi editor
Shell Scripting
DB Connection to MySQL
Summary
3
Introduction to UNIX OS Architecture
An Operating System (OS) is a software component of a
computer system, which:
Shares the limited resources of the computer
Coordinates and manages the various activities
Offers a number of services to application programs and users
Acts as a host for Applications that are run on the machine
Monitor Applications
Operating
Printer System
Disk
Drive
Keyboard
4
General Functions of O/S
Opening files
Reading files
Closing files
Creating files and/or directories
Copying or deleting files and directories
Command Interpretation
Device Management
Memory management
Network communication
Reading and writing to devices
Allowing or limiting access to system resources
Monitoring/Killing processes
5
Types of Operating System
Based on O/S Performance Based on User Performance
6
Features of UNIX OS
7
UNIX Architecture
Everything below the It provides file
system call interface system, CPU
and above the physical scheduling, memory
hardware is the management, and
KERNEL. other OS functions
using system calls.
8
User Application and Kernel Interaction
9
UNIX Architecture (Contd.,)
Major components of UNIX are:
Kernel
– Monitors and controls the resources of a computer
and allocates them among its users in an optimal
manner
Shell
– Provides a processing environment for the user
programs and acts like a command translator
Utilities
– Programs which are used for development purposes
User Applications
– Programs written by the user
10
UNIX File System
What is a file?
A collection of data items stored on a disk
11
History of UNIX
Year UNIX Variant Features
1957 BESYS • At Bell Labs
• To run batch jobs
1965 MULTICS (Multiplexed Information • Adopted third generation computer equipments
and Computing Service)
1969 UNICS (UNiplexed Information and • To play space travel on another smaller machine (DEC PDP-7)
Computing Service)
12
UNIX Flavors
13
What is Linux ?
Comviva Internal
14
LINUX
Developed by Linus Torvalds
Placed under General Public License
Variants
Caldera Linux
Debian Linux
Kondara Linux
Red Hat Linux
Mandrake Linux
SuSE Linux
Fedora
Ubuntu
15
Basic Commands
18
Linux Boot Process (Startup Sequence)
19
Boot Process - BIOS
BIOS stands for Basic Input/Output System
Performs some system integrity checks
Searches, loads, and executes the boot loader program.
It looks for boot loader in floppy, cd-rom, or hard drive. You can press
a key (typically F12 of F2, but it depends on your system) during the
BIOS startup to change the boot sequence.
Once the boot loader program is detected and loaded into the
memory, BIOS gives the control to it.
So, in simple terms BIOS loads and executes the MBR boot loader.
20
Boot Process - MBR
MBR stands for Master Boot Record.
It is located in the 1st sector of the bootable disk. Typically /dev/hda,
or /dev/sda
MBR is less than 512 bytes in size. This has three components 1)
primary boot loader info in 1st 446 bytes 2) partition table info in next
64 bytes 3) mbr validation check in last 2 bytes.
It contains information about GRUB (or LILO in old systems).
So, in simple terms MBR loads and executes the GRUB boot loader.
21
Boot Process - GRUB
RUB stands for Grand Unified Bootloader.
If you have multiple kernel images installed on your system, you can
choose which one to be executed.
GRUB displays a splash screen, waits for few seconds, if you don’t enter
anything, it loads the default kernel image as specified in the grub
configuration file.
GRUB has the knowledge of the filesystem (the older Linux loader LILO
didn’t understand filesystem).
Grub configuration file is /boot/grub/grub.conf (/etc/grub.conf is a link to
this)
In simple terms GRUB just loads and executes Kernel and initrd images.
22
Boot Process - Kernel
Mounts the root file system as specified in the “root=” in grub.conf
Kernel executes the /sbin/init program
Since init was the 1st program to be executed by Linux Kernel, it has the
process id (PID) of 1. Do a ‘ps -ef | grep init’ and check the pid.
initrd stands for Initial RAM Disk.
initrd is used by kernel as temporary root file system until kernel is
booted and the real root file system is mounted. It also contains
necessary drivers compiled inside, which helps it to access the hard
drive partitions, and other hardware.
23
Boot Process - Init
Looks at the /etc/inittab file to decide the Linux run level.
Following are the available run levels
0 – halt
1 – Single user mode
2 – Multiuser, without NFS
3 – Full multiuser mode
4 – unused
5 – X11
6 – reboot
Init identifies the default initlevel from /etc/inittab and uses that to load all
appropriate program.
Execute ‘grep initdefault /etc/inittab’ on your system to identify the default
run level
If you want to get into trouble, you can set the default run level to 0 or 6.
Since you know what 0 and 6 means, probably you might not do that.
Typically you would set the default run level to either 3 or 5. 24
Boot Process - Init
When the Linux system is booting up, you might see various services getting started. For
example, it might say “starting sendmail …. OK”.
Those are the runlevel programs, executed from the run level directory as defined by your run
level.
Depending on your default init level setting, the system will execute the programs from one of
the following directories.
Run level 0 – /etc/rc.d/rc0.d/
Run level 1 – /etc/rc.d/rc1.d/
Run level 2 – /etc/rc.d/rc2.d/
Run level 3 – /etc/rc.d/rc3.d/
Run level 4 – /etc/rc.d/rc4.d/
Run level 5 – /etc/rc.d/rc5.d/
Run level 6 – /etc/rc.d/rc6.d/
Please note that there are also symbolic links available for these directory under /etc directly.
So, /etc/rc0.d is linked to /etc/rc.d/rc0.d.
Under the /etc/rc.d/rc*.d/ directories, you would see programs that start with S and K.
Programs starts with S are used during startup. S for startup.
Programs starts with K are used during shutdown. K for kill. 25
Types of Commands
Internal
Built-in into the shell
No separate process invoked
External
Executable files
Stored in known locations in the file system
Shell scripts
Interpreted by the shell
26
Some Simple Commands
ls = list the files and directory
date = display date and time
cal = display calendar
clear = clear the screen
pwd = prints the name of current working directory
mkdir = creates the directory
rmdir = removes the directory
cd = changes the directory
cat = display the contents of file
rm = removes the files
cp = copy the files and directory
mv = move and rename ( files and directories )
touch = updates the file/directory time-stamps
less = pager command
more = pager command
Comviva Internal
27
UNIX Command Syntax
General form
Commandname [argument]… [<inputfile] [>outputfile]
Commandname
The name of the command comes first
Argument
If there are any arguments to the command, they follow the
command name
Inputfile
If there is an input redirection symbol “<“, input comes form
inputfile, otherwise it comes form the keyboard
Outputfile
If there is an output redirection symbol “>“, output is sent to
outputfile, otherwise to the screen 28
File Metacharacters
ls -l; ls -l .bash*
ls -la or ls -l -a
ls -la --ignore=.b*
It is also possible to give more than one command:
Coammdnd1 | command2 | command3 | … | commandn
– ls -l ; date ; echo "Hello“
31
Basic File Manipulation Commands
ls – list contents of directory
Commonly used switches
l – Long listing
a – All
d – List directory contents
R – Recursive
touch – update file modification time
Create file if it does not exist
cat – display contents of a file
touch - change file timestamps
rm – Remove file
Switch –i or interactive highly recommended
cp – Copy file
mv – Move file
Try mv .b* .c* 32
Basic Commands
which
$ which ls
ls whereis
$ ls -l $ whereis ls
$ ls -a
$ ls -la locate
$ locate stdio.h
$ ls -l --sort=time $ locate iostream
$ ls -l --sort=size -r
cd rpm
$ cd /usr/bin $ rpm -q bash
$ rpm -qa
pwd $ rpm -qa | sort | less
$ pwd
~ find
$ find / | grep stdio.h
$ cd ~ $ find /usr/include | grep stdio.h
~user
$ cd ~weesan
What will “cd ~/weesan” do?
Comviva Internal
33
Basic Commands (cont)
rm
$ rm foo
echo $ rm -rf foo
$ echo “Hello World”
$ echo -n “Hello World” $ rm -i foo
chgrp
cat $ chgrp bar /home/foo
$ cat /etc/motd chgrp im /root/out
$ cat /proc/cpuinfo
chsh
cp $ chsh foo
$ cp foo bar chfn
mv $ chfn foo
$ mv foo bar chsh im
mkdir chown
$ mkdir foo $ chown -R foo:bar /home/foo
Comviva Internal
34
Basic Commands (cont)
Pipe
tar $ cal > foo
$ tar cvfp lab1.tar lab1 $ cat /dev/zero > foo
gzip $ cat < /etc/passwd
$ gzip -9 lab1.tar $ who
untar & ungzip backtick
$ gzip -cd lab1.tar.gz | tar xvf – $ echo “The date is `date`”
$ tar xvfz lab1.tar.gz $ echo `seq 1 10`
touch Hard, soft (symbolic) link
$ touch foo ln vmlinuz-2.6.24.4 vmlinuz
$ cat /dev/null > foo ln -s firefox-2.0.0.3 firefox
Comviva Internal
35
Alias Command
Alias Using this command we can create alternate name of a exists
unix command
Syntax:
alias Altenatename=‘Unix Command’
Ex:
alias MLA=‘cat’
MLA>file123
UnaliasTo Cancel the Alternate name of the Unix Command
Syntax:
unalias alternatename
ex:
unalias MLA
36
Structure of a UNIX File System(UFS)
3 blocks
Boot block
Super block
Inode list (ilist)
Data blocks
Boot Block
The first block of every file system (block 0)
Contains bootstrap code for system
Boot block empty for all except root file system
37
Super Block
Contains data about file system
Fields are
– The size of the file system
– Sanity flag – Indicates whether the super block has been modified since the
last disk flush
http://www.angelfire.com/myband/binusoman/Unix.html
38
Disk Drive
39
Detailed File system model
40
Inode List
Disk array of inodes
Fields in individual inode (entries)
uid of owner
gid of “group” owner
Type of file
Number of links
Permissions
Access time
Modified time
Inode change time
Size
Disk addresses
– Direct and indirect addresses
41
I-Node
42
Links
Alternate access paths to a file
Two types
Hard Links (ln src dst)
Symbolic Links (ln –s src dst)
Hard Links
Two directory entries with the same inode number
Can be across directories
Must be within the same file system
Link count field incremented when new link created
File deleted only when link count reaches zero
Directory also have a link with parent
Symbolic Links
Use the ln -s command
Creates another inode
Contents of file are link to source
When source deleted, access through link fails
43
Hard Link v/s Soft Link
Hard Link is a mirror copy of the original file. Hard links share the same
inode. Disk usage is same.
Any changes made to the original or Hard linked file will reflect the other.
Even if you delete any one of the files, nothing will happen to the other.
Hard links can't cross file systems.
Soft Link is a symbolic link to the original file. Soft Links will have a different
inode value. Disk is increased.
A soft link points to the original file. If you delete the original file, the soft link
fails.
If you delete the soft link, nothing will happen.
Soft links can cross file systems.
Demo
How many links are possible for a directory containing 3 sub-directories? 44
File Compression
Comviva Internal
45
Create New Archive
The tar command
Stands for tape archive
Writes archives directly to tape devices
Creates new archives
Lists files in existing archives
Extracts files from archives
Syntax
tar [options] [directory file]
Common Options
c create an archive (begin writting at the start of the file)
t table of contents list
x extract from an archive
v verbose
46 46
Extract Archive File
Command Function
$ tar -cvf archive.tar TestF1 TestF2 • Creates archive file archive. tar for files TestF1 and
TestF2
• Additionally, use of –v (verbose) option displays
the progress while tar works.
• To create archive, we need to specify the name of
the archive (with –f), the copy, or write operation (-
c), and the filenames.
Command Function
$ tar -xvf archive.tar tar uses –x option to extract files from an archive
47 47
Compress and Expand Compressed File
Syntax:
gzip Filename
Ex: gzip file1
48 48
Compression/Decompression
Commands
Gunzip TO Unzip a file
Syntax:
gunzip filename.gz
– $ gunzip TestGipTry.gz
49
Compression Utilities
gzip, gunzip
bzip2, bunzip2
Comviva Internal
50
Using Compression
gzip somefile
( To create compressed file )
gunzip somefile
( To uncompress a compressed file )
bzip2 somefile
( To create compressed file )
bunzip2 somefile
( To uncompress a compressed file )
Note: - We can use z for gzip and j for bzip2 type of compression with tar
Comviva Internal
51
Basic Commands (cont)
Disk usage
$ df -h /
File space usage
$ du -sxh ~/
Advance stuff
$ ssh eon who
$ ssh eon ‘cd .html ; tar cvfp - cs183 | gzip -9c’ | tar
xvfpz -
$ ssh kilo-1 ‘tar cvfp - /extra/weesan’ | tar xvfp - -C /
Comviva Internal
52
Absolute and Relative Pathnames
Directory Related Commands
The Absolute Path.
The entire pathname starting from root(/).
Example: /home/training/charigs
The Relative Path.
The path relative to your present working directory.
Example: training/charigs
Triad of permissions
User - User matching owner of file
Group - Users whose group is the same as the group of the file,
Other members of the group
Others - Users who are not the owner nor belong to the group
54
Triad Permissions
Triad of permissions for each set
r – Read permission
w – Write permission
x – Execute permission
Yielding 9 bits
– 0 indicates no access
– 1 indicates access
chown
Change ownership of file
chgrp
Change group
57
Directory Security
58
Redirection : the 3 standard files
Standard Input
file / stream represents i/p, connected to keyboard
< or pipleline
wc, cat
Standard Output
file / stream represents o/p, connected to display
wc sample.txt > newfile, cat newfile
Standard Error
file / stream represents error messages originating from command / shell also connected to display
0 – standard input
1 – standard output
2 – standard error
Cannot be redirected to a file
cat foo (cat: cannot open foo) so use cat foo 2> errorfile then cat errorfile
Filters using both standard input and output
Directory commands neither i/p nor o/p
ls,pwd,who etc., take i/p and produce o/p
lp uses i/p but no o/p
cat, wc, od, cmp, gzip etc., use both i/p and o/p
59
Devices and Terminals
60
Devices and Terminals (Contd.,)
There are three types of device files:
Block device files are used for devices performing I/O buffering such as disks like rz0
whose block device files in the /dev directory are:
brw------- 1 root system 8, 1 Jan 19 11:20 /dev/rz0a
brw------- 1 root system 8, 1 Jan 19 10:09 /dev/rz0b
Character device files are used for devices with driver handled I/O buffering. Some
disks, terminal, pseudoterminal, and tapes are defined as character device files:
crw-rw-rw- 1 root system 7, 0 Jan 31 16:02 /dev/ptyp0
crw-rw-rw- 1 root system 7, 1 Jan 31 16:00 /dev/ptyp1
Socket device files. Printer daemon lpd and error logging daemon syslogd use the
socket device files:
srw-rw-rw- 1 root system 0 Jan 22 03:40 log
srwxrwxrwx 1 root system 0 Jan 22 03:41 printer
When the disk and tape interface supports more than one device, each device file has
a major and a minor number, where the major number specifies the interface (example
SCSI connection) and the minor number specifies the unit on the interface.
61
Device Files
62
Major and Minor device numbers
63
Process Commands
Common switches
a – All
e – Everyone
f – Full
l – Long
kill command
kill -x <pid>
Values of x can be between 1 and 19 (typical)
Need not necessarily cause termination of process
Value of 9 is said to be sure-shot kill
who
List users on the system
whoami
65
Interpretation of ps -l
bash-3.2# ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0S 0 16459 16455 0 75 0 - 16539 wait pts/1 00:00:00 bash
0T 0 16769 16459 0 78 0 - 14779 finish pts/1 00:00:00 man
0T 0 17553 17551 0 75 0 - 15886 finish pts/1 00:00:00 less
4R 0 18155 16459 0 77 0 - 15873 - pts/1 00:00:00 ps
bash-3.2#
F – no longer used
S state(status)
UID – user ID
PID – process Id
PPID – Parent PID
PRI – priority
SZ - size
66
Processes
67
Processes and Signals
68
Some Signals
1 SIGHUP
2 SIGINT
3 SIGQUIT
8 SIGFPE
9 SIGKILL
10 SIGBUS
11 SIGSEGV
12 SIGSYS
14 SIGALRM
15 SIGTERM
16 SIGURG
20 SIGCHLD
30 SIGUSR1
31 SIGUSR2
69
kill, SIGKILL, pkill
The SIG part of the signal name is optional.
To list the possible signal names, run kill with the -l switch:
Signals can also be identified by a signal number.
The bash shell’s built-in kill command lists the signals with
their signal numbers, for example: $ kill –l
If you attempt to kill a process and it does not die, you can
try using the unignorable SIGKILL signal.
So don’t use kill -9, it may lead to inconsistency on your
program
A safer tool is pkill. The pkill command is used like the kill
command but instead of using the PID as the argument to
the signal, the command name is used. 70
The signal Call
71
Zombie Processes
75
Filters
76
Pipes and Filters
Connecting two or more commands in tandom: Pipes
Filters
Commonly used filters
grep sort cut paste head tail wc pg more tee tr
The fact that many UNIX commands accept input from the standard
input and send output to the standard output allows us to link them
together with a pipe ( | ).
With this facility, the shell allows any number of such commands to be
connected in a sequence, known as a pipeline where the standard
output of each command in the sequence is piped into the standard
input of the next command as shown in the figure.
77
The wc Command
78
Regular Expressions
The patterns used by the grep utility are called regular expressions.
The regular expressions are always given in double quotes.
Regular Meaning
Expressions
[] To specify a pattern which consists of any one of a set of
characters.
Example "[xyz]" specifies the pattern either "x“ or "y" or "z"
[ ] with hyphen To specify a pattern which consists of any one of the range of
characters specified in the square brackets.
Example "[1-3]" specifies the pattern either 1 or 2 or 3.
Similarly "[ 1-9 a-z]" specifies a pattern of any one character which
can be a number from 1-9 or a lower case alphabet from a to z.
^character This character is used to specify that the pattern following it must
(caret symbol) occur at the beginning of the line.
Example "^[123]" specifies that the pattern either 1 or 2 or 3 should
appear at the beginning of each line to be selected.
79
Regular Meaning
Expressions
[^characters] When a caret symbol is specified within the square brackets and is
followed by one or more characters or ranges of characters
(indicated by hyphens) then strings containing any of the specified
will be excluded.
Example "[^123]" specifies that the searched string should not
contain 1, 2 or 3
Dot (.) This character is used in conjunction with any other characters and
indicates any one character
Example "[123]." specifies the pattern either 1 or 2 or 3 followed by
any single character
\ (Backslash) This character is used in conjunction with the above characters and
indicates that grep should ignore the special meaning of the
character following it in the regular expression.
Example "\[abc\]" specifies the pattern [abc] since the characters'['
and ']' are treated as ordinary characters and not signifying a set or
range of characters.
$(dollar) To specify that the pattern preceding it must occur at the end of
each line. Example "[abc]$" specifies the pattern either a or b or c at
the end of each line.
80
grep (Global search for Regular
Expression and Print)
Searching a pattern in a file
$ grep [options] "<pattern>" <filename>
options:
-n: prints line numbers
-v: the reverse search criterion
-c: display only a count of matching patterns
regular expressions:
"^" beginning of line
"$" end of line
"." any single character
[...] any one character from the list
81
grep (Contd.,)
Example:
Display the line(s) containing the string MURALI.
$ grep “MURALI" emp.dat <Enter>
83
Emp.dat file
Searching through the employee file using the grep command
Consider the file employee having the following data:
PATHAK MANI 2057 CD E1 3 03/03/56 1575
BHATIA NAVEEN 9090 SALES E2 4 03/04/67 9000
YADAV JOHNNY 8909 MKT E3 6 09/12/78 8500
ALVA VIJAY 9009 MKT E6 7 09/11/53 29000
MISHRA M.R. 2103 ADMIN E3 7 03/11/54 12000
JOSE JOBBY 9001 MKT E2 5 03/11/75 3000
BABY P.K. 9002 ADMIN E3 10 03/12/69 6000
JAMEEL S. 9012 CD E3 10 03/12/62 6500
LATHA N. 9023 CD E2 10 03/12/56 6400
SINGH ROHIT 9015 TRG E2 4 09/07/69 6899
SINHA RAJEEV 1289 CD E3 11 12/11/62 9812
GULATI RAJENDER1190 ADMIN E1 3 05/03/66 5400
DUGGAL TARUNA 1193 ADMIN E3 7 02/07/65 7200
VERMA MOHIT 2390 CD E1 2 02/09/67 4389
SAXENA RAHUL 3476 ADMIN E6 18 01/04/55 18500
MISHRA JR 7654 MKT E3 7 07/07/64 9065
DAS HK 3420 MKT E1 2 05/03/68 5400
MURALI N 2978 TRG E4 8 04/03/61 9000
FRANC SHAM 1390 TRG E2 5 07/07/69 7000
84
Use of grep as a filter
Finding a particular file in the directory:
$ ls | grep "emp.dat"
The output of the ls command is a list of files in the directory and this output
is directly given to the grep command as input file. The pattern "emp.dat"
will be searched in the list of file names produced by the ls command.
Note that the symbol "|" is used to join the two commands in the pipe
line.
To find out whether a given user has logged in or not:
$ who | grep "mano“
who command will produce a list of user-names who are currently logged in.
This output of who will become the input for grep and grep will search for
the pattern "mano".
Note that, no error message is produced, if the search using the grep
command fails.
85
sort
Arranges the output
$ sort [- option] [+pos] [-pos] [-o output file name] [filename]
options :
-d Dictionary order: only letters, digits and blanks
-n Sort on the first numeric field
-r Reverse order of sort
-u Eliminate duplicate lines in sorted output
-t Specify field separator character (within double quotes after -t)
-o Stores output in a file
The sort command takes lines from input file and sorts
them, displaying the output on the standard output device.
By default the sorting is done on the first field in ascending
order. 86
sort (Contd.,)
Each line from the input file is treated as a series of one or more fields
separated from each other by a space, or tab by default or any other
field delimiter if specified.
The command line format is: $ sort [option] [filename]
sort without a filename takes input from the standard input and
arranges each line alphabetically and produces the output on the
standard output.
$ sort <Enter> the output will be:
Alka
Alka
Bhupinder
Lalit Lalit
Bhupinder Malvika
Puneet Puneet
$
Malvika
<CTRL+D> 87
sort (Contd.,)
89
cut
Retrieving selected fields from a file: $ cut [options] <filename>
options:
-c selects columns specified by list
-f selects fields specified by list
-d field delimiter (default is tab)
It is often useful to organize information in a file as a table, with rows
and columns.
Each row of a table represents one record in the file.
An example can be of an employee file (emp.dat) having records of
different employees.
The cut command of UNIX is useful, when a file has to be queried to
display selective fields from a file. This command picks up selected
columns or fields from a file, by specifying a list of character positions or
a list of fields.
90
Example
The command line format is:
$ cut [options] <filename> <Enter>
To display the employee code, firstname and basic salary from the employee file
$ cut -d" " -f2,3,8 emp.dat <Enter> where
-d is used to specify the field delimiter. A space in this case.
-f represents fields and the numbers after it are the field positions.
The specified columns are copied to the standard output.
To display characters 4 to 9 both inclusive, from each record of the file employee:
$ cut -c4-9 emp.dat <Enter>
To display fields from first to fourth:
$ cut -d" " -f-4 emp.dat <Enter> (note that -4 means 1-4)
To display from the second to the fourth fields:
$ cut -d" " -f2-4 emp.dat <Enter>
To display from the fourth field onwards:
$ cut -d" " -f4- emp.dat <Enter>
91
tee
The output redirection will redirect the standard output to a file or
some destination other than the terminal. In case the output is
required to be sent to two different destinations at the same time, the
redirection operator can not be used.
The tee command is used to send the output to more than one
destination.
As the name suggest, the command is like a 'T', having one source of
input and two destinations of outputs.
92
tee (Contd.,)
tee transfers data without any change from its standard input to its
standard output, but at the same time it directs a copy of the data into
another file, that is specified at the command line. This command can be
used anywhere in a pipeline.
If the tee command is given at the $ prompt, it will expect input from the
keyboard and at the end of input, it displays the output on the terminal and
also stores it in the file specified. The format is: $ tee <filename> <Enter>
This command differs from other filters as it sends a copy of its output to a
disk. $ <command1> | tee <file> | <cmmand2>
The standard output of the command1 becomes the standard input for
tee. This input is then passed on as the standard input for command2 and
a copy is diverted to file at the same time.
$ sort emp.dat | tee tempfile | pg will sort the emp.dat file, display the
result on the screen page wise and store it in tempfile. 93
The paste command
Vertical combination of files
paste <file1> <file2>
options: -d (field delimiter)
95
Examining file contents (Contd.,)
tail
This command displays the last ten lines of the file by default.
The number of lines can be changed by providing it along with the
tail command.
The options can be of two types:
$ tail emp.dat will display the last ten lines from emp.dat
$ tail -7 emp.dat will display the last seven lines
$ tail +10 e emp.dat will display lines from the 10th line till end of
the file
https://www.linux.com/blog/14-tail-and-hea
d-commands-linuxunix
96
Examining file contents (Contd.,)
The options available with wc are:
-c: for character count only
-w: for word count only
-l: for line count only
$ wc -c emp.dat may display 1756
$ wc -w emp.dat may display 296
$ wc - l emp.dat may display 24
The head, tail and wc commands are filters and can be used anywhere in a pipeline.
more
controls the display of a file on the screen
$ more [filename]
options:
– + line number : starts the display at the line number
The more command, reads the contents of a long text file one
screenful at a time. To display the next screen the space bar is
pressed, if the user presses the <Enter> only the next line is
displayed. The more command continues displaying the contents till
the EOF (end of file) is reached.
The command line format is:
$ more <filename> <Enter>
This command displays 23 lines, at a time, then the prompt ---
More--- is displayed on the last line. To quit before the EOF is
reached,' q' can be pressed which will terminate the program and
return to the shell prompt.
$ more employee will display the file employee one screenful at a
time
98
tr
99
Example (Contd.,)
To convert all lower case alphabets into upper case and display them on the
terminal:
$ cat emp.dat | tr "[a-z]" "[A-Z]"
Display all the occurrences of small "a" as "A":
$ ls -l | tr "a" "A"
To produce the file size and names in descending order of size:
$ ls -l | tr -s " " | cut -d" " -f 5,9 | sort -nr <Enter>
The output of ls -l and who are in a tabular form, where the columns are
separated by one or more spaces. To provide these output to cut as input, the
extra spaces need to be squeezed to a single space.
To produce only the login names and time of login:
$ who | tr -s " " | cut -d" " -f1,5 <Enter>
100
vi Editor
102
vi Editor
vi works in 3 different modes
Command mode : Keys are interpreted as commands
Insert mode : Keys echoed in edit buffer
Esc colon (:) mode - Keys interpreted as commands of
ex mode
103
Editor Work Modes
$ vi filename write
UNIX Shell on the prompt to All types of operations
open file. and navigation are done
in command mode.
Command Mode
Append (a,A) Allows to make
Insert (i,I) Text Input Mode changes in the file.
Change (o,O)
Replace (R)
Delete (x , dd)
Join (J) Press esc key to
move back to the
Undo (u,U) esc command mode.
Yank ( y, Y) ,etc.
Press : (colon) key
to go to Exit Mode.
Allows exit and
set commands.
Exit Mode UNIX Shell
Under different modes the same key press creates different effect.
104
What is the input mode?
The Input mode is used to insert, append, replace or change
text.
A summary of input mode commands are given below:
Command Function
i Inserts text to left of cursor
I Inserts text at beginning of line
a Appends text to right of cursor
Command Function
A Appends text at
i end of line
o Opens line below
O Opens line above
r ch Replaces single character at cursor with character ch
R Replaces text from cursor to right
s Replaces single character at cursor with any number of characters
S Replaces entire line
105
Saving & Quitting: The Last Line Mode
vi uses the ZZ command to save and quit editor. The ‘ex’ mode, also referred to as last
line mode, can also be used.
To switch from command mode to ex mode, a colon (:) is pressed, which appears as ex
prompt in the bottom line. Any ex command can be entered at this prompt.
Following commands can be used for saving and quitting from the ex mode:
Command Function
w Write buffer into the editor
x Save and quit the editor
wq Write and quit editor
q Quit editor
The Repeat Factor
A number can be prefixed to any command: most commands will interpret the
instruction to repeat the command that many times. Hence this number is called as the
repeat factor.
For example, to insert a series of 30 asterisks in a line, 30i* can be used.
The repeat factor can be used with input as well as command mode. 106
Navigation (Cursor Movement commands):
Command Function
h or backspace Move cursor left
j Move cursor down
k Move cursor up
l or spacebar (or spacebar) Move cursor right
^ Move cursor to beginning of first word of line (no
repeat factor)
0 or | Move cursor to beginning of line (no repeat factor
with 0)
$ Move cursor to end of line
b Move cursor back to beginning of word
e Move cursor forward to end of word
w Move cursor forward to beginning of word
107
Paging Functions
Command Function
<ctrl-f> Full Page forward
<Ctrl-b> Full Page backward
<Ctrl-d> Half Page forward
<Ctrl-u> Half Page backward
<Ctrl-l> Redraw page screen (no repeat
factor)
108
Search and repeat commands
Command Function
/pat Searches forward for pattern pat
109
set command
There are many options which can be used with :set command
To get the list of all options in set command use :set all
110
Commands Summary
111
Review of vi Motion Commands
Movement Command
Scroll forward one screen ^F
Scroll backward one screen ^B
Scroll forward half screen ^D
Scroll backward half screen ^U
Scroll forward one line ^E
Scroll backward one line ^Y
Move current line to top of screen and scroll z ENTER
Move current line to center of screen and scroll z.
Move current line to bottom of screen and scroll z-
Redraw the screen ^L
Move to home—the top line of screen H
Move to middle line of screen M
Move to bottom line of screen L
Move to first character of next line ENTER
Move to first character of next line + 112
Review of Motion Commands (Contd.,)
113
Operators
115
Yank and put
116
Search and Replace
/pattern
Searches forward for first occurrence of a pattern.
?pattern
Searches backward for first occurrence of a pattern.
n
Repeats the last search.
N
Repeats the last search command in opposite
direction.
117
Search and Replace (Contd.,)
:s/ptn1/ptn2
Replaces first occurrence of ptn1 with ptn2.
: s/ptn1/ptn2/g
Replaces all occurrences in the current line.
: m, n s/ptn1/ptn2/g
Replaces all occurrences in lines m to n.
: ., $ s/ptn1/ptn2/g
Replaces all occurrences from current line to end of file.
% s/ptn1/ptn2/g
Replaces all occurrences in file
118
Handling Multiple Files
Can open more than one file
vi file1 file2 file3
Opens all files
: n
Permits editing of next file in the buffer
: rew
Permits editing of first file in buffer
: args
Displays names of all files in the buffer
: f
Displays the name of the current file
119
Customizing vi
The $HOME/.exrc file
The set command
– :set nu
120
Named Registers
The “ command
Example “adw deletes word placing it in register a
“ap puts contents of register a
121
vi v/s vim
Vim (vi-improved)
Functionally, vim is almost a proper superset of vi. Therefore,
everything that is in vi is available in vim.
Vim adds onto those features. Here are a some of the extended vim
features:
– Vim has been ported to a much wider range of OS's than vi.
– Vim includes support (syntax highlighting, code folding, etc) for
several popular programming languages (C/C++, Python, Perl, shell,
etc).
– Vim integrates with cscope.
– Vim can be used to edit files using network protocols like SSH and
HTTP.
– Vim includes multilevel undo/redo.
– Vim allows the screen to be split for editing multiple files.
– Vim can edit files inside a compressed archive (gzip, zip, tar, etc). 122
vi v/s vim (Contd.,)
– Vim includes a built in diff for comparing files (vimdiff).
– Vim includes support for plugins, and finer control over config and startup
files.
– Vim can be scripted with vimscript, or with an external scripting language
(e.g. python, perl, shell).
There are many more differences. Refer below sources which are few of
good places to start finding out more.
:h vi-differences
123
Shell Programming
Types of shells
Creating and running a shell program
Variables
Expressions
String operations
Operators
Read and echo
Conditional statements
125
The UNIX Shells
Bourne Shell
sh
C Shell
csh
Korn Shell
ksh
Bourne Again Shell
bash
126
The UNIX Shells (Contd.,)
Bourne Shell
The oldest amongst all shells
Still the shell language of choice
C Shell
Early BSD enhancement
Provides C like syntax
Significant advances over Bourne shell
– History and command substitution
Extremely non-intuitive and convoluted syntax
– Alias
– Arrays
127
The UNIX Shells (Contd.,)
Korn Shell
Developed in mid 80’s by David Korn
Completely compatible with the Bourne shell
Extends the Bourne shell
Firstly with the addition of all C Shell features
– More intuitively than the C shell
Secondly, with the addition of completely new features
– select and let
128
The UNIX Shells (Contd.,)
Shell Type
Feature sh csh ksh bash
Job Control O P P P
Command History O P P P
Command Line Editing O O P P
Local Variables O O P P
Aliases O P P P
Custom Prompt O O P P
130 130
How to Run Shell Scripts
132
Variables
Is a database or memory location name, is temporary location, its
value can change during execution
Shell has only string variables, no data types
Special handling for numeric values
var=value
Examples
greeting1=Hello
greeting2=World
greeting=“$greeting1 $greeting2”
Three types:
Local variables
Constant variables
Global variables
133
Constant and Global variables
Constant variables
Will not change their value during the execution
readonly is the keyword to create constant variables
x=100
readonly x
Global variables
Scope of these is global
export is the keyword to create global variables
export variablename
To comment a line
#
# this line is commented
134
Some Important Shell Variables
135
Shell Keywords
136
Built-in Variables
IFS
Internal Field Seprator, space/tab/\n
PS1 (Prompt String)
Primary Shell prompt
PS1=“BVP$”
PS2
The system prompt2, default >
PS3
Used for display of select command
PS4
set –x tracing
137
Built-in Variables
$?
Exit status
of last cmd
$!
PID of last
bg cmd
$$
PID of script
$0, $1…$9
Arguments to script
$#
Number of arguments
$* or $@
All arguments
138
The read and echo commands
read
Take input from the user
read variablename
– Dot Not read $var like this
139
Substitution Operators
Variable substitution operator
$variablename or ${variablename}
Command Substitution operator
`commandname` or $(commandname)
Quotes and double quotes
Used when whitespace and other special characters
Single quotes do not cause variable substitution
Double quotes cause variable substitution
Backquotes
Cause the output of a command to be stored in the variable
year=`date | cut -d” “ –f6`
a = 100, b = 1.5, n = $a, course = UNIX, name=“Comviva”,
mypath=“/home/bin/abc”
k = `ls’, c = `cat emp.dat`
140
The expr command
Float
– a=10.56
– b=5.4
– echo `echo $a + $b | bc -l` - for float redirect to bc (calculator cmd with library
option)
141
File Operators
Option Meaning
-s file True if exist and its size > 0
-f file True if exist and it is not a directory
-d file True if exist and it is a directory
-c file True if exist and it is character special file
-b file True if exist and it is block special file
-r file True if exist and it has read permission
-w file True if exist and it has write permission
-x file True if exist and it has execute permission
142
String Operators
Arithmetic Operators
• str1 = str2
Operator Meaning • Equal =
+ Addition • str1 != str2
• Not equal !=
- Subtraction
• -n “str1”
* Multiplication
• Not null (true if str1 is not
/ Division empty)
% Remainder • -z “str1”
• Null (true if str1 is empty)
143
Numeric Operators
Operator Meaning
-eq Equal to
-ne Not equal to Logical Operators
-le Lesser than or equal to
-lt Lesser than
Operator Meaning
-ge Greater than or equal to
-gt Greater than -a Logical and
-o Logical or
! Logical not
144
The if Construct
145
The case Construct
case word in
Pat1) commands
;;
Pat2) commands
;;
*) commands
;;
esac
Pattern examples
Yes | yes)
[yY]*)
146
Looping constructs
while
As long as condition is true
until
As long as condition is false
for
Not like C for loop
147
Looping constructs syntax
until expr
while expr
do
do commands
done
commands num=1
done until [ $num -gt 10 ]
do
echo $num
num=1 num=`expr $num + 1`
done
while [ $num -le 10 ]
do for var in word1 word2 … wordn
do
echo $num commands
num=`expr $num + 1` done
for i in *
done
do
echo $i
done 148
Debugging Shell Scripts
Syntax error, and one of the ways to search for and correct them is to use –n
while running a script
Set –x displays + lines as they are executed
Set –v (verbose) displays lines as they are executed
Set –vx causes all
set -x
a=1
b=2
let c=$a+$b
echo $c
Returns values
To declare a function, simply use the following statement:
name () { commands; }
Called with the name along with arguments
The function call args will be in $1 to $9, $#, $?
Advantages:
Reduce code length of the program
Reduce the cost of maintenance
Easy to maintain the application
150
Examples
1. Simple function with out any argument
function hello() {
152
Running and Stopping Services
Commands
Comviva Internal
154
Network Configuration Utilities
netconfig
Text-based network configuration tool
Only writes config files. Does not activate device or changes.
Use ifup/ifdown to active changes or restart the network service
system-config-network
GNOME-based network configuration tool
Can be launched by a non-privileged user, but requires authentication as root.
Comviva Internal
155
ssh: Secure Shell
ssh allows remote logins and remote command execution via a secure
encrypted connection
Example
ssh [email protected]
Comviva Internal
156
Network (IP) Bonding
Network bonding is a process of combing or joining two or more
network interfaces together into a single interface.
Network bonding offers performance improvements and redundancy
by increasing the network throughput and bandwidth.
If one interface is down or unplugged the other one will work.
It can be used in situations where you need fault tolerance,
redundancy or load balancing networks.
In Linux, a special kernel module named bonding is used to connect
multiple network interfaces into a single interface.
Two or more network interfaces can be connected into a single logical
“bonded” interface.
The behavior of the bonded interfaces depends on the type of
bonding method used.
157
Types of Network Bonding
mode=0 (balance-RR)
mode=1 (active-backup)
mode=2 (balance-XOR)
mode=3 (broadcast)
mode=4 (802.3ad)
mode=5 (balance-TLB)
mode=6 (balance-ALB)
158
Types of Network Bonding (Contd.,)
mode=0 (balance-RR)
This mode is based on Round-robin policy and it is the default mode.
This mode offers fault tolerance and load balancing features.
It transmits the packets in Round robin fashion that is from the first
available slave through the last.
mode-1 (active-backup)
This mode is based on Active-backup policy.
Only one slave is active in this band, and another one will act only when
the other fails.
The MAC address of this bond is available only on the network adapter
part to avoid confusing the switch.
This mode also provides fault tolerance.
159
Types of Network Bonding (Contd.,)
mode=2 (balance-XOR)
This mode sets an XOR (exclusive or) mode that is the source MAC
address is XOR’d with destination
MAC address for providing load balancing and fault tolerance.
Each destination MAC address the same slave is selected.
mode=3 (broadcast)
This method is based on broadcast policy that is it transmitted everything
on all slave interfaces.
It provides fault tolerance. This can be used only for specific purposes.
160
Types of Network Bonding (Contd.,)
mode=4 (802.3ad)
This mode is known as a Dynamic Link Aggregation mode that has it created
aggregation groups having same speed.
It requires a switch that supports IEEE 802.3ad dynamic link.
The slave selection for outgoing traffic is done based on a transmit hashing
method.
This may be changed from the XOR method via the xmit_hash_policy option.
mode=5 (balance-TLB)
This mode is called Adaptive transmit load balancing.
The outgoing traffic is distributed based on the current load on each slave
and the incoming traffic is received by the current slave.
If the incoming traffic fails, the failed receiving slave is replaced by the MAC
address of another slave.
This mode does not require any special switch support. 161
Types of Network Bonding (Contd.,)
mode=6 (balance-ALB)
This mode is called adaptive load balancing. This mode does not require any
special switch support.
162
Batch Scheduling
The at command
Run command “at” specified time
at HH:MM command
User must be allowed in at.allow and not denied in at.deny
163
Try it by yourself
1. Locate your document and check the current date as in the following:
$ ls
– important_report.swx
$ date
– Thu Dec 9 14:53:06 CST 2004
2. Type the at command followed by the time you want the job to occur:
$ at 17:00
3. Type the copy command (cp), the name of file you want to copy, and the location of the file in
which you want to place the backup copy on the file system, and then press Enter:
at> cp important_report.swx ~username/backups
4. Press Ctrl + D to exit the at shell; you should see something akin to the following:
job 1 at 2004-12-10 17:00
Use the atq command to double-check that the job has been scheduled. To delete a command
before it has been executed, use the atrm command:
$ atq
1 2004-12-10 17:00 a username
To delete a command before it has been executed, use the atrm command:
$ atrm 1
164
The cron scheduler and crontabs
165
The crontab format
Six fields
minute (from 0 to 59)
hour (from 0 to 23)
day of month (from 1 to 31)
month (from 1 to 12)
day of week (from 0 to 6) (0=Sunday)
Command to be run
0 1 * * 5 /bin/execute/this/script.sh causes script to be run every Friday at
1 AM
166
Network related commands
ifconfig
netstat
Listing all the LISTENING Ports of TCP and UDP
connections Listing all ports (both TCP and UDP)
using netstat -a option.
Listing only TCP (Transmission Control Protocol)
port connections using netstat -at.
Listing all active listening ports connections
with netstat -l.
167
Summary
Introduction to Linux OS
Commands- basic and advanced
vi editor
Shell Scripting
Debugging – GDB
SED
AWK
DB Connection to MySQL
168
Thank You
169