Thanks to visit codestin.com
Credit goes to www.slideshare.net

MOS USING UNIX/LINUX
Session - 1
 Objectives
 Introduction about OS
 Introduction to Unix/Linux
 Versions of Unix/Linux
 Structure of Linux
 Kernel and Shell
 File and Directory structure
What is Linux
 Linux is a UNIX clone
 It can run on 32 bit and 64 bit hardware
 Linux is a true multitasking environment
 Fully capable of taking advantage of multiple processors
 Can address up to 64 GB of RAM
 Partial POSIX Compliance
 POSIX , an acronym for "Portable Operating System Interface", is a
family of standards specified by the IEEE for maintaining compatibility
between operating systems. POSIX defines the application programming
interface (API), along with command line shells and utility interfaces, for
software compatibility with variants of Unix and other operating systems
Linux – Free Software
Free software, as defined by the FSF (Free Software
Foundation), is a "matter of liberty, not price." To qualify as
free software by FSF standards, you must be able to:
 Run the program for any purpose you want to, rather than be
restricted in what you can use it for.
 View the program's source code.
 Study the program's source code and modify it if you need to.
 Share the program with others.
 Improve the program and release those improvements so that
others can use them.
History
 The history of Linux began with Unix in 1969
 Unix was created at Bell Labs with the goals:
 Simplicity
 Recyclable code
 Written in C as opposed to assembly
 Development started in 1991
 Linus Torvalds wanted to create a free
implementation of UNIX
 By 1993 there were 12000 Linux users
 Today Linux rivals UNIX in stability and scalability
OS and its Function
 Linux components are divided into 3 parts.
 Kernel
 Environment
 File Structure
 It is a core program that executes all other
programs
 First loaded in computer
 Handles the hardware devices.
 Like
 CPU
 Memory
 I/O Devices
 Provides interface to user
 Receives commands from user and send to kernel
for execution
 Called as interpreter
 Types of Environment
 Desktop
 Window managers
 Command line (shell)
Way of storing files
The Unix file system looks like an tree structure.
File structure starts with a Root Directory (/).
doc
home bin usr var dev etc
sbin
/
(root)
bin sbin lib share
man
Directory Function
/ Begins the file structure
/home Contains user’s home directory
/bin Holds the standard commands
/usr Holds the commands used by the system
/usr/bin Holds user oriented commands
/usr/sbin Holds system administration commands
/usr/lib Holds the library program files
Directory Function
/usr/share/doc Holds the Linux documentation
/usr/share/man Holds the online man files
/var/spool Holds the spool files, such as those generated for printing jobs
and network transfer
/sbin Holds the system administration commands for booting the system
/var Holds the files that vary, such as mailbox files
/dev Holds file interfaces for devices such as the terminal and printers
/etc Holds the system configuration files
 The shell is a command interpreter that provides
an interface between user and the operating
system.
 Shells used in Linux
 Bash Shell (Bourne Again Shell)
 Korn Shell (Ksh)
 C Shell (Csh)
 The Linux shell displays a prompt
 Prompt should be one single character or
multiple line
 Shells prompt are handled by PS1 & PS2
 Bash Shell ( default shell) - - $
 C Shell - - %
 Korn Shell - - $
 Root - - #
Session - 2
 Objective
 Unix File System
 Indirection Technique
Unix File System
Boot Block
 It is located in the first few sectors of a file system.
The boot block contains the initial bootstrap
program used to load the operating system.
 Only one File System Contains a Boot Program,
other File System contains an empty boot block.
Super Block
 Each filesystem has one super block, it contains info about
 Type of filesystem (ext2, ext3...)
 The block size
 Pointers to a list of free blocks
 The inode number of the root directory
 Contains the layout of the Disk
 – Size of Disk
 – Number of Inodes
 – Number of Data Blocks
 – Where inodes start, where data blocks start, etc….
Inode Table
 Every file have one inode and vice versa. In linux every
file is recognized with integer number known as inode
number. This structure consists of info of file about
 Which disk blocks go with which file.
 Data structure for bookkeeping
 – List of Blocks
 – File or Directory
 – Link Count
 – Other information…owner/permissions
Inode Table in Details
 file ownership indication
 file type (e.g., regular, directory, special device, pipes, etc.)
 file access permissions.
 time of last access, and modification
 number of links (aliases) to the file
 pointers to the data blocks for the file
 size of the file in bytes (for regular files), major and minor device numbers for
special devices.
 Inodes include pointers to the data blocks. Each inode contains 15 pointers:
the first 12 pointers point directly to data blocks
 the 13th pointer points to an indirect block, a block containing pointers to data
blocks
 the 14th pointer points to a doubly-indirect block, a block containing 128 addresses
of singly indirect blocks
 the 15th pointer points to a triply indirect block (which contains pointers to doubly
indirect blocks, etc.)
Data Block
 This is where the file data itself is stored. Since a
directory is simply a specially formatted file,
directories are also contained in the data blocks. An
allocated data block can belong to one and only
one file in the system. If a data block is not
allocated to a file, it is free and available for the
system to allocate when needed.
Directories
 Like a file: List of files and directories
 – name
 – inode number
 Can read it like a file
 Always has at least 2 entries:
 “.” current directory
 “..” parent directory
Session - 3
 Linux Commands
Command name - - clear
Purpose - - Clear the user screen
Syntax - - clear
Ex - - $ clear
Command name - - exit or logout
Purpose - - Quit Linux
Syntax - - exit
Ex - - $ exit
Ctrl - d
Command name - - uname
Purpose - - Display the kernel name
Syntax - - uname
Ex - - $ uname
Output $uname
Linux
$
Command name - - date
Purpose - - Display the current system date and
time.
Syntax - - date
Ex - - $ date
Output $date
Sat jul 02 10:10:50 BDT 2011
$
Command name - - who
Purpose - - Display the information of all active
login user.
Syntax - - who
Ex - - $ who
Output $who
root tty1 jul 2 10:20
user1 tty2 jul 2 11:08
user5 tty3 jul 2 12:05
$
Command name - - who am i
Purpose - - Display the information of the user.
Syntax - - who am i
Ex - - $ who am i
Output $who am i
user1 tty2 jul 2 11:08
$
Command name - - tty
Purpose - - Display the file name of terminal
connected to standard input
Syntax - - tty
Ex - - $ tty
Output $ tty
Command name - - cal
Purpose - - Display the cal of a specified date.
Syntax - - cal [month][year]
Ex - - $ cal
Display the calendar of the current month.
$cal 07 2011
Display the calendar of the July 2011
$cal 2011
Display the calendar of the 2011
Escape sequence characters
n - new line character
c – continue character
Ex- printf “rkcitn”
rkcit
$
Command name - - echo
Purpose - - Same as printf.
Syntax - - echo “MESSAGE”
Ex - - $ echo “rkcit”
Ourput - - $ echo “rkcit”
rkcit
$
Session - 04
 Objectives
 Directory Management Commands
 Wildcards
 File management Commands
 Redirection
 Aim - - To provide knowledge about Directory &
File management in Linux
Every directory and file are listed in its parent
directory.
In the case of the root directory, that parent is itself.
A directory is a file that contains a table listing the
files contained within it, giving file names to the
inode numbers in the list.
The information about all the files and directories is
maintained in INODE TABLE
An Inode (Index Nodes) is an entry in the table containing information about
a file including file permissions, UID, GID, size, time stamp, pointers to files
data blocks on the disk etc.
Command name - - pwd
Purpose - - Display the complete path of the
current working directory.
Syntax - - pwd
Ex - - $ pwd
Ourput - - $ pwd
/home/user1
$
Command name - - ls
Purpose - - Lists files & subdirectory names under
certain directory.
Syntax - - ls [option] [directory_name]
Ex - - $ ls
Ourput - - $ ls
weather reports letters
$ ls /home/user2/king
Sunday Monday Tuesday
Options of ls Command
Option Purpose
-x Get multi-columnar output.
-r List files & sub-directories in reverse order.
-t List files & sub-directories in time order.
-a List all files including hidden files.
-A List all files excluding . And ..
-c All files by inode modification time.
-i List the inode for each file.
-l Display permissions, owner, size, modification time etc.
Ex - - $ ls - l
total 20
drwxrwxr-- 2 user1 grp1 4096 Nov 29 01:51 Orissa
drwxrwxr-X 3 user2 grp2 4096 Nov 29 01:54 Cool
-rwxrw - - - - 1 user3 grp1 18 Nov 30 04:38 report
drwxrwxr-X 2 user1 grp1 4096 Dec 09 10:50 south
-rw –r- - - - 1 user1 grp1 23 Nov 29 01:54 king
Type
Permissions
Links
owner group
Size in bytes Modification
Date & Time
File name
 . . . Special symbols can be used along with different commands.
Wildcard Used to
* Match any number of character including zero.
? Match one and only character
[abc] Match one character which should be either a, b or c.
[!abc] Match one character which is not a, b or c.
[p-t] Match one character which falls within the range of p-t
[!p-t]
Match one character which does not falls within the
range of p-t
Examples
$ ls p*
(Display all names of files & directories starts with
p)
$ ls ??
(Display all names of files & directories having any
two characters)
$ ls linux[1234]
linux1 linux2 linux3 linux4
$ ls linux[!12]
linux4 linux5 linux8
$ ls uni[x-z]
unix uniy uniz
(Display all files & directories starts with uni and followed
by x, y & z.)
$ ls Linux[!0-9]
(Display all files & directories starts with Linux and
followed by a non-numeric characters)
Command name - - mkdir
Purpose - - Creates a new directory.
Syntax - - mkdir [path]<Directory Name>
Ex- $ pwd
/home/user1
$ mkdir india
home
root (/)
cool sunday
user1
$ ls
india
$ mkdir /home/user1/cool/orissa
india
orissa
Command name - - cd
Purpose - - Change the current directory.
Syntax - - cd [path][Directory Name]
Ex- $ pwd
/home/user1
$ ls
india Sunday cool
home
root (/)
cool sunday
user1
india
$ cd india
$ pwd
/home/user1/india
$
$pwd
/home/user1/india home
root (/)
cool sunday
user1
india
$ cd /home/user1/cool
$ pwd
/home/user1/cool
$ cd
$pwd
/home/user1
Session - 05
 Objectives
 Directory Management Commands
 File management Commands
 Redirection
 Aim - - To provide knowledge about Directory &
File management in Linux
Command name - - rmdir
Purpose - - Delete a directory if it is empty.
Syntax - - rmdir [path]<Directory Name>
Ex- $ ls
india cool sunday
home
root (/)
cool sunday
user1
india
$ rmdir india
$ ls
cool sunday
Command name - - cat
Purpose - - Used to create & to display the
content of a file.
Syntax - - cat <filename>
Ex- - $cat > myfile1
write the contents of file.
(ctrl-d)
$
$cat myfile1
contents of myfile1.
$
Command name - - cp
Purpose - - Creates copy of a file.
Syntax - -
cp <source-file> <destination-file>
$ cp /home/user1/india/file1 /home/user1/Sunday/file1
File1 (F)
india
home
root (/)
cool sunday
user1
File1 (F)
Command name - - mv
Purpose - - Move a file from one directory to
another.
Rename a file.
Syntax - -
mv <source-file> <destination-file>
$ mv /home/user1/india/file1 /home/user1/Sunday/file1
File1 (F)
india
home
root (/)
cool sunday
user1
$ mv /home/user1/india/file1 /home/user1/india/file2
File1 (F)
india
home
root (/)
cool sunday
user1
File2 (F)
Command name - - rm
Purpose - - Delete a file.
Syntax - - rm <filename>
home
root (/)
cool sunday
user1
File1 (F)
Ex- $ pwd
/home/usr1
$ rm file1
Different views of a file.
Page wise - - pg, more
Top of a file - - head
Bottom of a file - - tail
Page wise - - pg
To display the contents of a file or a
command at one time.
$ pg linux01.txt
Enter – new line
Page wise - - more
To display the contents of a file or a
command at one time.
$ more linux01.txt
Space bar – next page
Enter – next line
head - - To display the top of a file.
$ head
(by default it will display top 10 lines)
$ head -6 my_file
(it will display only top 6 lines)
tail - - To display the bottom of a file.
$ tail
(by default it will display bottom 10 lines)
$ tail -4 my_file
(it will display only bottom 6 lines)
Command Name - - wc
Purpose - - used to count the number of
lines, words, characters of a given text or
file.
Options - -
 - l  Count only the number of lines in a file
 - w  Count only the number of words in a file
 - c  Count only the number of characters in a file
Ex - -
$ wc myfile
82 511 2313 myfile
(lines) (words) (character)
$ wc –l myfile
82 myfile
(lines)
Standard input  Keyboard
Standard output  Display Screen (Monitor)
We can direct the input & output to other stream
Command can be inputted from - -
 Keyboard
 A file (using redirection operator <, > and >>)
 By another command ( using the redirection
operator | ). (|- pipeline Symbol)
Output can be send to - -
Standard output (monitor)
A file
Another command
Input Redirection
Using operator Left chevron(<)
Ex- - $ cat > f1 < f2
Output Redirection
Using operator Right chevron(>)
Ex- - $ cat myfile > newfile
(Write the contents of myfile to new file)
Using operator ( >> )
Ex - - $ cat myfile1 >> newfile1
(Append the content of myfile1 with newfile1)
Connecting commands
Using pipes ( | )
Ex- - $ ls | wc –l
Counts the number of files and directories.
Output of ls is piped to wc command.
Ex- - $ who | wc –l
Counts the number of active login users.
Output of who is piped to wc command.
Session - 06
 Objectives
 File permission
 Changing File permission
 Aim - -
In UNIX/LINUX, there is a concept of user and an associated
group
The system determines whether or not a user or group can
access a file or program based on the permissions assigned
to them.
Apart from all the users, there is a special user called Super
User or the root which has permission to access any file and
directory
There are three permissions for any file, directory or
application program.
The following lists the symbols used to denote each, along
with a brief description:
r — Indicates that a given category of user can read a file.
w — Indicates that a given category of user can write to a
file.
x — Indicates that a given category of user can execute the
file.
Each of the three permissions are assigned to three defined
categories of users.
The categories are:
owner — The owner of the file or
application.
group — The group that owns the file or
application.
others — All users with access to the
system.
One can easily view the permissions for a file by invoking
a long format listing using the command ls -l.
For instance, if the user user1 creates an executable file
named test, the output of the command ls -l test would
look like this:
-rwxrwxr-x 1 user1 grp1 1251 Sep 26 12:25 test
The permissions for this file are listed are listed at the
start of the line, starting with rwx.
This first set of symbols define owner access.
The next set of rwx symbols define group access
The last set of symbols defining access permitted for all
other users.
This listing indicates that the file is readable, writable,
and executable by the user who owns the file (user user1)
as well as the group owning the file (grp1).
The file is also readable and executable, but not
writable.
Default permission of a file  rw-r--r--
Default permission of a directory  rwxr-xr-x
Command to change the permission of a file or directory
chmod <arguments> <file/ directory name>
chmod Arguments
Category Operation Permission
u (owner) + (assign) r (read)
g (group) - (deny) w (write)
o (other)
= (absolute
Assignment)
x (Execute)
a (all)
chmod Example - -
$ chmod u+x myfile 
assign execute permission to user.
$chmod g-w myfile 
remove write permission to group.
$chmod og-x myfile 
remove the execute permission of other & group.
$ chmod o-rwx myfile 
remove the all permission of others.
chmod Example - -
$ chmod ugo=r myfile 
assign read permission to user, group, other by removing
other permissions.
Octal notation
Read Permission = 4
Write Permission = 2
Execute Permission = 1
777 signifies all permissions and 000 signifies absence of all
permissions.
chmod Example - -
instead of
$ chmod a=r myfile
we can give
$ chmod 444 myfile
instead of
$ chmod ugo=rw myfile
we can give
$ chmod 666 myfile
Session - 07
 Objectives
 Changing ownership
 Changing group ownership
 Umask
 File links
 Aim - - Linux permission changing commands
The creator became the owner of the file.
Only owner can change the permissions of a file.
The ownership of the file or directory can be changed using the
command
chown <owner> <file/directory name>
Ex- - $ chown user4 myfile
Grant the ownership of myfile to user4.
 Ownership once surrendered cannot be re-established.
The group ownership of the file or directory can be
changed using the command
chgrp <group> <file/directory name>
Ex- - $ chgrp grp2 myfile7
Grant the group ownership of myfile7 to grp2.
 By default, the group owner of a file is also the
group to which the other owner belongs.
Default file permission
 umask – system variable, decides the default file &
directory permission.
 When the a file is created the permissions are set to
 File – rw-rw-rw- (666)
 Directory – rwxrwxrwx (777)
 Value of umask is subtracted from original value.
 umask – 022
For file - - 666 – 022 = 644 (rw-r--r--)
For directory - - 777 – 022 = 755 (rwxr-xr-x)
Default file permission
 To change permission
$ umask 044
$ cat > new
contents
$
666 – 044 = 622
new (rw--w--w-)
Sticky Bit
 Sticky bit can be added to a directory to prevent the
files within it from getting deleted.
 No one can delete the files from the directory except
owner and the root user.
 To add sticky bit to a directory
$ chmod o+t <directory name>
$ chmod o+t ram
(contents of ram directory will not get deleted by others)
Session - 08
 Objectives
 File links
 Aim - - Linux file links
File Links
 Every file has inode numbers to identified by the
system
 When we create a copy of a file then it will create
two files with two different names and different
inodes .
 When we make a link of a file then we have single
file, different names, same content and same inode
numbers.
 Links are of two types
 Hard links
 Soft links ( symbolic link)
 Hard links
 The ln command is used to create multiple links of a
file.
Example
$ ln stuff junk
$ ls –i
1659 junk 1659 stuff
 If we change the contents of stuff file those changes
will appear in junk file and vice versa.
 If we delete one of the file. Then one of the link to
that file will be deleted
 A file is truly deleted from the file system only when
it has no links to it.
 By default a file has just one link.
 A directory contains two hard links
 . ( a link pointing itself)
 .. ( a link to pointing to its parent)
 Symbolic link ( soft link)
 It is another type of link which is different from hard link.
 A symbolic link allows us to give a file another name, but does
not link the file by inode number
ln –s stuff sub
The file having symbolic link have the permission rwxrwxrwx
$ ls –l
-rw-rw-rw- 2 user1 grp1 23 Jul 24 04:19 stuff
lrwxrwxrwx 1 user1 grp1 5 Jul 24 05:33
sub  stuff
$
Difference between hard link & soft link
 One can create a symbolic to a file that does not exist.
The same is not possible with hard links.
 Symbolic links identifies the files they point to. But with
hard links, there is no easy way to determine which files
are linked to the same inode.
 You can hard-link files only when they are on the same
file-system; symbolic links do not have this reflection.
File handling commands
 od – used to display data in octal format
 Also used for printing the non printable characters.
 -b – option used along with od to display each
character separately
 -c – option used with –b option to display the
character with octal code
 Each line of the output displays 16 bytes of data in
octal, preceded by the position in the file of the first
byte in the line.
$ cat > f3
rkcit
college
$
$ od f3
0000000 066551 062560 064562 066141 061412 066157 062554 062547
$
$ od -b f3
0000000 151 155 160 145 162 151 154 012 143 157 154 154 147 145
0000020 012
0000021
$
$ od -bc f3
0000000 151 155 160 145 162 151 154 154 012 143 157 154 154 145 147 145
i m p e r i a l
n c o 1 1 e g e
0000020 012
n
0000021
$
Session - 09
 Objectives
 File comparison
 Access modification time change
 Aim - - touch command
Comparing two files
 cmp – this comand is used to check whether two files
are identical or not
 Syntax – cmp file1 file2
 Compare two files byte by byte
 Example
$ cat > f1
rkcit collegE
$cat > f2
rkcit College
$
$ od -b f1
151 155 160 145 162 151 141 154 040 143 157 154 154 145 147 105
$od -b f2
111 155 160 145 162 151 141 154 040 103 157 154 154 145 147 145
$cmp f1 f2
f1 f2 differ: byte 1, line 1
$cmp –l f1 f2
1 151 111
10 143 103
16 105 145
Common between two files
 comm - used to check the comm things in the files
 Both files should be sorted and also have some
difference
 Syntax – comm file1 file2
 The command displays three collumn output
1. Contains Unique contents of first file
2. Contains unique contents of second file
3. Common contents of both file
$cat > f1 $cat > f2
apple ball
Fruits grapes
john john
pass
mike stel
people xerox
result $
stel
uninor
$
$ comm f1 f2
apple
ball
fruits
grapes
john
mike
pass
people
result
stel
uninor
xerox
Session - 10
 Objectives
 Vi editor
 Different modes
 Switching between modes
 Commands used in vi
 Aim - - working in a vi editor
VI editor
 Vi is the most popular text editor available in
almost UNIX and LINUX systems.
 It is entirely different from other modern text
editors
 vi uses different types of commands to perform
different operations.
 Vi editor has three modes
 Command mode
 Input mode
 Ex mode
Modes of vi
 Command mode
 Where you place your commands, when we invoke vi
editor we will initially placed in this mode.
 Input mode
 It also known as input mode
 In this mode we can add text to the file
 Ex mode
 Enables us to give commands at the command line.
Commands given in command mode
 h – move one character left
 l – move one character right
 k – move up
 j – move down
 G – move cursor to the beginning of the last line in the file
 #G – move cursor to the beginning of the #th
line.
 i – insert text before the current cursor position
 I – insert text at the beginning of the current line
 a – append text after the cursor
 A – append text at the end of the current line
 r – replace one character
Commands given in command mode
 R – go on replacing characters until ‘esc’ key is pressed
 s – replace the character at the cursor with any number
of characters
 o – open a new line below the current line
 O – open a new line above the current line
 x – delete one character at the cursor
 X – delete one character to the left of the cursor
 dd – delete the entire line
 D – delete reminder of line after cursor
 p – paste the delete text
Commands given in command mode
 . – repeat the last executed command
 u – undo the effect of the last command
 H – move to the first line on the screen
 M – move to the middle line of the screen
 L – move to the last line of the screen
 Ctrl + f – show the next screen
 Ctrl + b – show the previous screen
Commands given in Ex mode
 :w – save the changes you have made to
the file
 :q – quit vi without saving changes
 :wq – save and quit
 :q! – force quit if ‘q’ does not work
SWITCHING BETWEEN MODES
Ex mode
command
mode
Input mode
a
A
I
I
O
o
S
s
R
r
esc
:w
:q
:wq
:q!
esc
Session - 11
 Objectives
 Vi editor
 Commands used in vi
 Aim - - working in a vi editor
 b – move back to the beginning of word
 e – move forward to the end of the word
 w – move forward to the beginning of the word
 yy-copy a single line
 J – join single lines
 Repeat factor
 To repeat a single command multiple times the vi editor
provides the repeat factor mechanism. We have to prefix a
number with the command to which we want to repeat.
 3dd – delete 3 lines
 5yy – copy 5 lines
 4j - move 4 lines downwards
 Searching
? = Search for the pattern in backward
direction
?text
/ = search for the text in forward
direction
/text
n = find next in same direction
N = find next in opposite direction
Creating files in vi
vi <filename>
 opens an existing file
 Creates a new file if it doesn’t exists
 Saving lines to a new file
. = specify current line
$ = specify last line
:1,5w <filename>
:1,.w <filename>
:.,$w <filename>
:1,$w <filename>
 Replacement
 :s
 :Address s/old/new
 :s/rkcit/rkcit
 Changes the first occurrence of rkcit with RKCIT in the current
line
 :s/rkcit/RKCIT/g
 Changes the all occurrence of rkcit with RKCIT in the current line
 :1,5 s/best/worst
 It changes the first occurrence of best with worst from line 1 to
5.
 :1,$ s/worst/best/g
 Changes all occurrence of worst with best in the entire file
 Inserting another file
:r
:r filename
 Inserting output of a command
:r ! Command name
:r ! ls – l
Session - 12
 Objectives
 Vi editor
 Commands used in vi
 Aim - - working in a vi editor
 Abbreviation
 Inserting auto text (abb)
 :abb rkc rkcit college
 Mapping
 Defining our own command
 :map (ctrl+v)key command
 :map del x
 :map q l
Handling multiple files in vi
 Vi editor facilitates to work with multiple files symontenously
 $ vi one two three
It will open one file
 To switch to next file
: n
To switch to previous file
:N
To switch between current & previous file
:e#
To switch to a specific file
:e <filename>
Session - 13
 Objectives
 User defined variables
 Escaping and quating
 Aim - - shell variables
 All Linux shell contains a list of variables which are known as
environment variables.
 We can define our own variables and also display the value with
echo command by prefixing a $ (dollar) symbol.
 HOME  user home directory
 SHELL  contains shell of the user
 PATH  contains the list of directory name in which the
executable file resides
 PS1  primary command prompt
 PS2  secondary command prompt
 LOGNAME  contains login name of the user.
Shell variables
 User defined variables
$ a=120
$ echo $a
120
$ b=“rkcit”
$ echo $b
rkcit
$
Shell variables
 Escape and quoting mark can made to explicitly to a
special character.
 difference between single quotes and double
quotes
 Single quote – to omit the special meaning of any meta character
 Double quote – used in part of the command in which we don’t
want to omit the meaning of meta character
 Ex-

If we want to display *character with echo command it will display
all the file names in the home directory.
$ echo * (will not work)
$ echo ‘*’
$ echo “*”
Escaping & Quoting
$echo my home is $HOME
My home is /home/user5
echo ‘my home is $HOME’
My home is $HOME
echo “my login name is $LOGNAME”
My login name is user6
-
 Back quote- to evaluate a command in the
command line
$ echo Current time is `date`
Current time is Sat Aug 19 10:10:01 BDT 2011
$ echo “my current working directory is `pwd`”
My current working directory is /home/user3/mohan
$a=`date`
$echo $a
Sat Aug 19 10:10:10 BDT 2011
 Alias – kourne and bourne shell provides a
command alias to assign a short name to an existing
command
$alias p=pwd
$p
/home/user2
 cut command
 Used to filter particular columns or fields out of a file.
 Options of cut
 -c = extract columns
 -f = extract fields
 - d = specify the delimiter (separator)
 Examples
$ cut –c2 emp.lst
$ cut –c 2,5,9 emp.lst
$ cut –c 2,3,4,5,6 emp.lst
Or
$cut –c 2-6 emp.lst
Filters and Regular
Expressions
$cut –d “t” -f4 emp.lst
$ cut –d “|” –f2,5 emp.lst
$cut –d ‘|’ –f2,5 emp.lst
$cut –d | -f2,5 emp.lst
$ cut –c 10- emp.lst
from 10th
character to end
$cut –c -5 emp.lst
from 1 to 5
$ cut –d ‘|’ –f3- emp.lst
3 to last
$cut –d | -f2 emp.lst > c1
Filters and Regular
Expressions
Session - 14
 Objectives
 Filter commands
 Aim - - provide knowledge about filter commands
cut, grep, paste
 Translating character (tr)
 This command is used to replace one character with another one at the
time of displaying.
$ tr ‘a’ ‘A’ < file1
$ tr ‘|’ ‘=‘ < emp.lst
$ tr ‘|’ ‘t’ < emp.lst
 Options of tr
 - d = we can delete a character at the time of displaying
$tr –d ‘|’ < emp.lst
$ls –l | tr –d ‘-’
Filters and Regular
Expressions
 -s = (squeeze) consecutive occurrence of the characters remove the
Consecutive duplicate characters
$tr –s ‘ ‘ emp.lst
$ls –l | tr –s ‘ ’ | cut –d “ ” –f 3,5
Filters and Regular
Expressions
 grep command
 It means global regular expression and print. It searches for a pattern and
display the selected lines containing the pattern
$ grep good emp.lst
$ grep cuttack emp.lst
display all employees from cuttack
$ grep sambalpur emp.lst emp2.lst
$ grep “sasank gupta” emp.lst
 Options of grep command
 -I = ignore case
 -v = display the non-matching characters
 -n = display the line numbers
Filters and Regular
Expressions
 -c = count the number of lines matching the pattern
 -l = display only the file name containing the pattern
 -e = specify multiple pattern
 -E = specify multiple pattern
 $grep ‘ctc
 > bbsr’ emp.lst
 grep –E ‘ctc|bbsr’ emp.lst
 egrep ‘ctc|bbsr’ emp.lst
 -f = specify the file name that containing the pattern
Filters and Regular
Expressions
 sort command
Arrange the files in a specific order
Sort [option] <filename>
Options
-t = specify the delimiter
-k = specify the field number
-r reverse order
-c = check whether the file is sorted or not.
Filters and Regular
Expressions
 -u remove the duplicate line
 -o specify the output file in which the sorted
information will store
 Sort –t “ ” –k 4 emp.lst
 Sort –t “ ” –k3,4 emp.lst
 Tab=`echo –e “t”` sort –t “Tab” –k2 emp.lst
 Sort – t “ ” –c emp.lst
 Sort –t “|” –k2 –c emp.lst
 Sort emp.lst –o employee
Session - 15
 Objectives
 Regular Expressions
 Aim - - provide knowledge about Regular
Expressions
 Regular Expressions
 If we want to match similar patterns with a single expression we can use the method of regular
expression using the pattern matching concepts we can represent multiple expressions with a single
pattern the regular expression is of 2 types and contain the own characters there own characters sets.
 Basic regular expressions (BRE)
 Extended regular expressions (ERE)
 Characters in Basic Regular Expressions
 * = zero or more occurrence of previous character
$ grep de*pak emp.lst
 . = Any single character
 $ grep D.pa emp.ls t
 [abc] = match any character from the range
 $ grep g[ie]ta emp.lst
 [a-z] = match any single character from the range
 $grep BCA[0-9] emp.lst
Filters and Regular
Expressions
 [^abc] = any character other than the list
 [^a-z] = any character other than the range
 ^a = line starting with character a
 ^[^R] = line doesn’t starting with R
 A$ = lines ending with character A
 List out all the directories.
 List out all the files
Filters and Regular
Expressions
 Character sets in Extended Regular Expressions
 ERE makes it possible to match dissimilar pattern with a single expression.
 It uses some additional characters such as:
+  it matches 1 or more occurands of previous characters
?  it match ‘0’ or ‘1’ occurance of previous characters
 Sed =It is a filter command like grep having some additional facilities than grep.
 It is known as the stream edition.
 It is a multipurpose tool having the features of several filter commands.
 $ sed option ‘address action’ filenames
 In address and action we can specify the line number and the commands for operation
 p  print the selected line
 q  to display the selected lines
 w  save selected lines to a new file
$ sed ‘5q’ emp.lst (first 5 lines)
$ sed ‘2,6q’ emp.lst ( display all by repeating from 2 to 6)
Filters and Regular
Expressions
$ sed ‘2,$5p’ emp.lst (all lines by repeating lines 5 and end of the files)
 -n  used to display selected lines
 $ sed -n ‘5,8p’ emp.lst
 $ sed –n ‘8,$p’ emp.lst
 $ sed –n ‘8,15!p’ emp.lst
 $ sed –n 3,5p
>9,20p’ emp.lst
 $ sed –n ‘2,5 w new.txt’ mp.lst
Filters and Regular
Expressions
Session - 16
 Objectives
 Sed command
 Shell Programming
 Aim - - provide knowledge about Regular
Expressions, Shell programming.
Context addressing
 With context addressing mechanism we can address the lines by specifying a pattern. The
lines containing the specified patterns will display.
 $ sed –n ‘/bgh/p’ emp.lst
 $ sed –n ‘/bgh/!p’ emp.lst
 $ sed –n ‘/sales/p
>/cuttack/p’ emp.lst
 $ sed –n ‘/sales/w new’ emp.lst
Substitution using sed
 s- substitute
 $ sed ‘s/old/new/g’ filename
 $ sed ‘5,15 s/ctc/cuttack/g’ emp.lst
 $ sed ‘/sales/s/ctc/cuttack/g’ emp.lst
Filters and Regular
Expressions
 Deleting blank lines from a file
 Display name and address of employees who were from sambalpur from last 10
lines.
 Who | grep –c “^$LOGNAME”
Filters and Regular
Expressions
 Shell script or shell programming is a mechanism to execute multiple commands in
a group.
 when group of commands have to be executed regularly they should be stored in
a file and the file itself executed as a shell script.
 The extension of shell program is .sh.
 When we execute the shell program it execute all the commands in it.
 We can execute a shell program with the command
 .sh
 Sh
 If a shell program has executable permission we can execute it directly
Shell Script
 Wap to display your name and address.
$ cat > oca.sh /vi oca.sh
echo STUDENTS OF BCA II
echo rkcit College
echo budharaja
echo sambalpur
 Wap to display your login name, shell and Home directory
 expr = it is a command line calculator program through which we can perform
calculations both numerical and string
 $ expr 5 + 6
 $ a=100
 $ b=200
 $ expr $a + $b
Shell Script
 Wap to display the values of 2 variables
clear
x=10
y=20
echo value of x= $x
echo value of y=$y
 Wap to add 2 numbers
clear
x=12
y=23
echo “addition =`expr $x + $y`”
Or
Z=`expr $x + $y`
Printf “value of x=%d”$x
Printf “value of y=%d”$y
Printf “addition of %d & %d is %d”$x$y$z
Shell Script
Session - 17
 Objectives
 Shell Script
 Read
 Command line arguments
 Conditional statement
 If else
 case
 Aim - - provide knowledge about shell script
 read = used to take input from the user
 Wap to input one no & display its value
clear
echo “Enter a number”
read a
Printf “value of x=%d”$a
 Wap to input two numbers and display them
clear
echo “enter 1st number”
read a
echo “enter 2nd
number”
read b
c=`expr $a + $b`
printf “addition of %d & %d is %d”,$x$y$z
Shell Script
 Command line
 Argument means the information we provide to the system
 The information with the command in the command line is known as command line argument
 Shell program accept the arguments from the command line.
 Shell uses different variables to manage the command line
 $@ / $* = display the list of arguments
 $# = display the no of arguments
 $1 $2 $3 $4 ……….$n = represents positional parameters in the command line
Shell Script
$ set rkcit bca 2nd
100
$ech o $*
Imperail bca 2nd
100
$ echo $#
4
$ echo $1
rkcit
$ echo $2
bca
 Wap to display the addition of 2no’s provided in the command line? (add.sh)
a=$1
b=$2
c=`expr $a + $b`
echo $c
$ add 120 150
270
$ sh add.sh 100 200
300
Shell Script
Conditional Statements
The shell script allows to implements the conditional statement for conditional
execution with the help of following two techniques:
if Statement
case Statement
if statement
Syntax :
if command
then
execute the command
else
execute the command
fi
Shell Script
 Wap to display a message if the pattern is found.
$ cat > search1.sh
if grep ‘manager’ emp.lst > /dev/null
then
echo “pattern found”
else
echo “pattern not found”
fi
 Exit stats of a command = success or failure of execution of a command is known as exit
status.
 The state is “0” in case of success
 The state is any non-zero value in case of failure
 The $? Variable contains exit status of last executed command.
Shell Script
$ cat oca
$echo $?
 test = used to check whether a condition is true or false
 We can use this command to evaluate a condition
$ test 5<10
$ echo $?
Operators used in shell programming
-eq =equals to
-gt = greater than
-lt = less than
-ge = greater than equals to
-le = less than equals to
-ne = not equals to
Shell Script
 Wap to input 2 numbers and display the biggest one
echo “enter two numbers
reand a,b
if test $a –gt $b
then
echo $a is big
else
echo $b is big
fi
Shell Script
echo “enter two numbers
reand a,b
if test $a –gt $b
then
echo $a is big
else
if then $b –gt $a
then
echo $b is big
else
echo both are same
fi
fi
Shell Script
elif = incase of nested if else we can use elif. Here no need to specify multiple fi
echo “enter two numbers
reand a,b
if test $a –gt $b
then
echo $a is big
elif then $b –gt $a
then
echo $b is big
else
echo both are same
fi
fi
Shell Script
 Wap to check whether a number is even or odd
echo “ enter a numbers”
read a
b=`expr $a % 2`
if test $b -eq 0
then
echo no is even
else
echo no is odd
fi
[ ] is substitute if test command
If test `expr $a % 2` -eq 0
If [ `expr $a % 2` -eq 0]
Shell Script
Session - 18
 Objectives
 Multiple conditions
 And & or Operator
 Conditional statements
 Case
 Aim - - provide knowledge about shell script
 Contents of /etc/passwd file
 This file contain the information about the users in it. A database is mentioned in
this file to store the user information. It contains a line of information for each
system user Such as :
1. User name
2. Password
3. Userid
4. Group id
5. Full name
6. Home directory
7. Shell
user1:x:1001:1002:user1,,,,,:/home/user1:/bin/bash
/etc/shadow= contains the character “x” In the password field. The user password is stored as
encrypted string of 13 characters in the file /etc/shadow.
 The file contains the password fields in an expanded format such as :
1. User name
2. Password
3. Password last change
4. Minimum number of date between password change
5. Maximum number of date password is valid
6. Number of days to warn the user to change password
7. Number of days the login may be inactive
8. Date when the login is no longer valid
9. A reserve field for future used
Wap to input your login name and display its full name if user name doesn’t exists display
a error message
echo “enter the login name”
read ln
grep ln /etc/passwd > x1
if [ $? –eq 0 ]
then
a=`cut –d “:” –f5 x1
echo full name=$a
else
echo “invalid user”
Shell Script
 Multiple conditions
 We can use multiple conditions with –a for and operator and –o options for or operator
 Biggest among 3 numbers
echo “enter 3 numbers”
if [ $a –gt $b –a $a –gt $c ]
then
echo “$a Is big”
elif [ $b –gt $a –a $b –gt $c ]
then
echo “$b is big”
else
echo $c is big
fi
Shell Script
Session - 19
 Objectives
 Conditional statements
 Case
 Aim - - provide knowledge about shell script
 Wap to input a single digit and display it in words
 Case statement
case expression in
pattern 1) command;;
pattern 2) command;;
…
…
*) command;;
esac
 Wap to input a digit and display it in words
echo enter a digit
read a
case $a in
0) echo zero;;
…
…
*) echo invalid digit;;
 Wap to input a character and check whether it is a vowel or consonant
 Wap to check whether it is an alphabet, digit or special symbol.
Session - 20
 Objectives
 String comparison
 Looping
 Aim - - provide knowledge about shell script
 String comparision
 = are used to compare two strings
 != not equals to
 Wap to input 2 strings and check whether they are equal or not
echo “input 1st
string”
read a
echo “input 2nd
string”
read b
if [ $a = $b ] ;then
echo “ strings are equal”
else
echo “strings are not equal”
fi
 && and || two conditional operators
 Command 1 && command 2
 In the above case the command2 executes in the success of command1
$ cat new.txt && date
$ grep ‘sales’ emp.lst && echo found
 Commmand 1 || command 2
 In the above case command 2 executes in the failure of command 1
$ cat new.txt || date
$ grep ‘sales’emp.lst || echo “not found”
 Looping in shell script
 Shell script provides 3 types of loops
 While
 For
 Until
 While loop
while condition is true
do
commands
----
----
done
 Wap to display a name five times
clear
a=1
while [ $a –le 5 ]
do
echo “rkcit”
a=`expr $a + 1`
done
 Wap to display addition of all numbers from 1 to 50
 Wap to display even numbers from 99 to 250
 Wap to input a number & display the addition of all digits
 Wap to input a number and display each digit in word
 Wap to input a number and check whether it is prime or composite
 Wap to display all prime numbers from 200 to 500
 Wap to check whether a number is strong or not?
Session - 21
 Objectives
 Shift command
 Aim - - provide knowledge about shell script &
procwess
 Shift command
 Shift the positional parameters to left
$ set 10 hari 9th
1st
$ echo $1
10
$ shift
$echo $1
hari
Or
clear
n=$#
a=1
while [ $a -le $n]
do
echo $1
shift
done
Session - 22
 Objectives
 AWK
 Aim - - provide knowledge about shell script &
procwess
 Wap to display the addition of all the command line arguments?
 wap to add new records to employee data base?
clear
echo “enter employee id”
read id
echo “enter employee name”
read nm
echo “ enter employee address”
read ad
echo “enter employee salary”
read sal
echo “$id | $nm | $ ad” >> emp.lst
 A process is simply an instance of running program.
 a file is treated as a simple file when it lies in a dormant state. It can also be
treated as a process when it is under execution
 Like human beings processes are born, they give birth of to another process and
also die.
 A process is said to be born when the program starts its execution.
 It remains alive as longer the program is active. After execution is completed the
process is said to be dead.
 It Is the kernel but not the shell that is ultimately responsible for the management of
the processes.
 It determines the time and properties that are allocated to a process. So that
multiple processes can share a single CPU.
 Like files processes are also have some attributes. All the attributes of processes
are maintained by the kernel and stored in the process table known as PCB
(Process Control Board).
Process
 Attributes of PCB
 PID = > each process can be uniquely identified by an integer values known as
PID. The PID number is assign to a process during the start of the execution.
 PPID = > this is the process of the parent process other than the properties there
are some additional associated with the process such as :-
 Process name
 Terminal name
 Priority
 Born time
 CPU time
 Status
 When we logon to the Linux shell processes immediately created by the kernel
which is known as login process. This process represents a linux command which
may be sh, ksh, csh,etc.
 Any command that we type at the command prompt is actually the standard input
to the shell process. The shell process remains active until we logout.
Process Control
Board
 PS command
 Each process is associated with some attributes such as pid number,
ppid number, user id number, execution time, terminal name, etc
 To display the attributes associated with the process we can use the
command PS.
 PS command
 Each process is associated with some attributes such as pid number,
ppid number, user id number, execution time, terminal name, etc
 To display the attributes associated with the process we can use the
command PS.
 Killing process
 The linux system requires to communicate with the process, we can
send a signal to a process
 Each signal is identified by a number and designed to perform a
specific operation
 If a program is running longer than the expected time, we want to
terminate an executing program we can use the command kill.
 Syntax
 $ kill option pid_numbner
 Option
 -9 = we can use this option to kill the process force fully.
 NICE
 Linux can run a lot of processes at a time, which can slow down
the speed of some high priority processes and result in poor
performance.
 To avoid this, you can tell your machine to prioritize processes
as per your requirements.
 This priority is called Niceness in Linux, and it has a value
between -20 to 19. The lower the Niceness index, the higher
would be a priority given to that task.
 The default value of all the processes is 0.
 To start a process with a niceness value other than the default
value use the following syntax
 nice -n 'Nice value' process name
 DF
 This utility reports the free disk space(Hard Disk) on
all the file systems.
System Call
 A system call is a mechanism that provides the
interface between a process and the operating
system. It is a programmatic method in which a
computer program requests a service from the
kernel of the OS.
 System call offers the services of the operating
system to the user programs via API (Application
Programming Interface). System calls are the only
entry points for the kernel system.
 An operating system can roughly be divided into
two modes:
 Kernel mode: A privileged and powerful mode
used by the operating system kernel
Why do you need System Calls in
OS?
 Following are situations which need system calls in
OS:
 Reading and writing from files demand system calls.
 If a file system wants to create or delete files,
system calls are required.
 System calls are used for the creation and
management of new processes.
 Network connections need system calls for sending
and receiving packets.
 Access to hardware devices like scanner, printer,
need a system call.
Types of System calls
 Process Control
 File Management
 Device Management
 Information Maintenance
 Communications
Important System Calls Used in
OS
 wait()
 In some systems, a process needs to wait for another
process to complete its execution. This type of situation
occurs when a parent process creates a child process,
and the execution of the parent process remains
suspended until its child process executes.
 The suspension of the parent process automatically
occurs with a wait() system call. When the child process
ends execution, the control moves back to the parent
process.
 fork()
 Processes use this system call to create processes that
are a copy of themselves. With the help of this system
Call parent process creates a child process, and the
execution of the parent process will be suspended till
the child process executes.
 exec()
 This system call runs when an executable file in the
context of an already running process that replaces the
older executable file. However, the original process
identifier remains as a new process is not built, but
stack, data, head, data, etc. are replaced by the new
process.
 kill():
 The kill() system call is used by OS to send a
termination signal to a process that urges the process to
exit. However, a kill system call does not necessarily
mean killing the process and can have various
meanings.
 exit():
 The exit() system call is used to terminate program
execution. Specially in the multi-threaded environment,
this call defines that the thread execution is complete.
The OS reclaims resources that were used by the
process after the use of exit() system call.
Shell
 SHELL is a program which provides the interface
between the user and an operating system. When
the user logs in OS starts a shell for
user. Kernel controls all essential computer
operations, and provides the restriction to hardware
access, coordinates all executing utilities, and
manages Resources between process. Using kernel
only user can access utilities provided by operating
system.
C Shell
 Denoted as csh
 Bill Joy created it at the University of California at
Berkeley. It incorporated features such as aliases
and command history. It includes helpful
programming features like built-in arithmetic and C-
like expression syntax.
 In C shell:
 Command full-path name is /bin/csh
 Non-root user default prompt is hostname %
 Root user default prompt is hostname #
Bourne Shell
 Denoted as sh
 It was written by Steve Bourne at AT&T Bell Labs. It
is the original UNIX shell. It is faster and more
preferred. It lacks features for interactive use like
the ability to recall previous commands. It also lacks
built-in arithmetic and logical expression handling. It
is default shell for Solaris OS.
 For the Bourne shell the:
 Command full-path name is /bin/sh and /sbin/sh
 Non-root user default prompt is $
 Root user default prompt is #.
Korn Shell
 Denoted by ksh
 It Was written by David Korn at AT&T Bell LabsIt is
a superset of the Bourne shell.So it supports
everything in the Bourne shell.It has interactive
features. It includes features like built-in arithmetic
and C-like arrays, functions, and string-manipulation
facilities.It is faster than C shell. It is compatible with
script written for C shell.
 For the Korn shell the:
 Command full-path name is /bin/ksh
 Non-root user default prompt is $
 Root user default prompt is #.
Bourne-Again Shell
 Denoted as bash It is compatible to the Bourne
shell. It includes features from Korn and Bourne
shell.
 For the GNU Bourne-Again shell the:
 Command full-path name is /bin/bash
 Default prompt for a non-root user is bash-g.gg$ (g.gg
indicates the shell version number like bash-3.50$)
 Root user default prompt is bash-g.gg#.

Rishav Mishra final presentation on UNIX Final.pptx

  • 1.
  • 2.
    Session - 1 Objectives  Introduction about OS  Introduction to Unix/Linux  Versions of Unix/Linux  Structure of Linux  Kernel and Shell  File and Directory structure
  • 3.
    What is Linux Linux is a UNIX clone  It can run on 32 bit and 64 bit hardware  Linux is a true multitasking environment  Fully capable of taking advantage of multiple processors  Can address up to 64 GB of RAM  Partial POSIX Compliance  POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems. POSIX defines the application programming interface (API), along with command line shells and utility interfaces, for software compatibility with variants of Unix and other operating systems
  • 4.
    Linux – FreeSoftware Free software, as defined by the FSF (Free Software Foundation), is a "matter of liberty, not price." To qualify as free software by FSF standards, you must be able to:  Run the program for any purpose you want to, rather than be restricted in what you can use it for.  View the program's source code.  Study the program's source code and modify it if you need to.  Share the program with others.  Improve the program and release those improvements so that others can use them.
  • 5.
    History  The historyof Linux began with Unix in 1969  Unix was created at Bell Labs with the goals:  Simplicity  Recyclable code  Written in C as opposed to assembly  Development started in 1991  Linus Torvalds wanted to create a free implementation of UNIX  By 1993 there were 12000 Linux users  Today Linux rivals UNIX in stability and scalability
  • 6.
    OS and itsFunction
  • 8.
     Linux componentsare divided into 3 parts.  Kernel  Environment  File Structure
  • 9.
     It isa core program that executes all other programs  First loaded in computer  Handles the hardware devices.  Like  CPU  Memory  I/O Devices
  • 10.
     Provides interfaceto user  Receives commands from user and send to kernel for execution  Called as interpreter  Types of Environment  Desktop  Window managers  Command line (shell)
  • 11.
    Way of storingfiles The Unix file system looks like an tree structure. File structure starts with a Root Directory (/). doc home bin usr var dev etc sbin / (root) bin sbin lib share man
  • 12.
    Directory Function / Beginsthe file structure /home Contains user’s home directory /bin Holds the standard commands /usr Holds the commands used by the system /usr/bin Holds user oriented commands /usr/sbin Holds system administration commands /usr/lib Holds the library program files
  • 13.
    Directory Function /usr/share/doc Holdsthe Linux documentation /usr/share/man Holds the online man files /var/spool Holds the spool files, such as those generated for printing jobs and network transfer /sbin Holds the system administration commands for booting the system /var Holds the files that vary, such as mailbox files /dev Holds file interfaces for devices such as the terminal and printers /etc Holds the system configuration files
  • 15.
     The shellis a command interpreter that provides an interface between user and the operating system.  Shells used in Linux  Bash Shell (Bourne Again Shell)  Korn Shell (Ksh)  C Shell (Csh)
  • 16.
     The Linuxshell displays a prompt  Prompt should be one single character or multiple line  Shells prompt are handled by PS1 & PS2
  • 17.
     Bash Shell( default shell) - - $  C Shell - - %  Korn Shell - - $  Root - - #
  • 18.
    Session - 2 Objective  Unix File System  Indirection Technique
  • 19.
  • 20.
    Boot Block  Itis located in the first few sectors of a file system. The boot block contains the initial bootstrap program used to load the operating system.  Only one File System Contains a Boot Program, other File System contains an empty boot block.
  • 21.
    Super Block  Eachfilesystem has one super block, it contains info about  Type of filesystem (ext2, ext3...)  The block size  Pointers to a list of free blocks  The inode number of the root directory  Contains the layout of the Disk  – Size of Disk  – Number of Inodes  – Number of Data Blocks  – Where inodes start, where data blocks start, etc….
  • 22.
    Inode Table  Everyfile have one inode and vice versa. In linux every file is recognized with integer number known as inode number. This structure consists of info of file about  Which disk blocks go with which file.  Data structure for bookkeeping  – List of Blocks  – File or Directory  – Link Count  – Other information…owner/permissions
  • 23.
    Inode Table inDetails  file ownership indication  file type (e.g., regular, directory, special device, pipes, etc.)  file access permissions.  time of last access, and modification  number of links (aliases) to the file  pointers to the data blocks for the file  size of the file in bytes (for regular files), major and minor device numbers for special devices.  Inodes include pointers to the data blocks. Each inode contains 15 pointers: the first 12 pointers point directly to data blocks  the 13th pointer points to an indirect block, a block containing pointers to data blocks  the 14th pointer points to a doubly-indirect block, a block containing 128 addresses of singly indirect blocks  the 15th pointer points to a triply indirect block (which contains pointers to doubly indirect blocks, etc.)
  • 25.
    Data Block  Thisis where the file data itself is stored. Since a directory is simply a specially formatted file, directories are also contained in the data blocks. An allocated data block can belong to one and only one file in the system. If a data block is not allocated to a file, it is free and available for the system to allocate when needed.
  • 26.
    Directories  Like afile: List of files and directories  – name  – inode number  Can read it like a file  Always has at least 2 entries:  “.” current directory  “..” parent directory
  • 27.
    Session - 3 Linux Commands
  • 29.
    Command name -- clear Purpose - - Clear the user screen Syntax - - clear Ex - - $ clear
  • 30.
    Command name -- exit or logout Purpose - - Quit Linux Syntax - - exit Ex - - $ exit Ctrl - d
  • 31.
    Command name -- uname Purpose - - Display the kernel name Syntax - - uname Ex - - $ uname Output $uname Linux $
  • 32.
    Command name -- date Purpose - - Display the current system date and time. Syntax - - date Ex - - $ date Output $date Sat jul 02 10:10:50 BDT 2011 $
  • 33.
    Command name -- who Purpose - - Display the information of all active login user. Syntax - - who Ex - - $ who Output $who root tty1 jul 2 10:20 user1 tty2 jul 2 11:08 user5 tty3 jul 2 12:05 $
  • 34.
    Command name -- who am i Purpose - - Display the information of the user. Syntax - - who am i Ex - - $ who am i Output $who am i user1 tty2 jul 2 11:08 $
  • 35.
    Command name -- tty Purpose - - Display the file name of terminal connected to standard input Syntax - - tty Ex - - $ tty Output $ tty
  • 36.
    Command name -- cal Purpose - - Display the cal of a specified date. Syntax - - cal [month][year] Ex - - $ cal Display the calendar of the current month. $cal 07 2011 Display the calendar of the July 2011 $cal 2011 Display the calendar of the 2011
  • 37.
    Escape sequence characters n- new line character c – continue character Ex- printf “rkcitn” rkcit $
  • 38.
    Command name -- echo Purpose - - Same as printf. Syntax - - echo “MESSAGE” Ex - - $ echo “rkcit” Ourput - - $ echo “rkcit” rkcit $
  • 39.
    Session - 04 Objectives  Directory Management Commands  Wildcards  File management Commands  Redirection  Aim - - To provide knowledge about Directory & File management in Linux
  • 40.
    Every directory andfile are listed in its parent directory. In the case of the root directory, that parent is itself. A directory is a file that contains a table listing the files contained within it, giving file names to the inode numbers in the list. The information about all the files and directories is maintained in INODE TABLE An Inode (Index Nodes) is an entry in the table containing information about a file including file permissions, UID, GID, size, time stamp, pointers to files data blocks on the disk etc.
  • 42.
    Command name -- pwd Purpose - - Display the complete path of the current working directory. Syntax - - pwd Ex - - $ pwd Ourput - - $ pwd /home/user1 $
  • 43.
    Command name -- ls Purpose - - Lists files & subdirectory names under certain directory. Syntax - - ls [option] [directory_name] Ex - - $ ls Ourput - - $ ls weather reports letters $ ls /home/user2/king Sunday Monday Tuesday
  • 44.
    Options of lsCommand Option Purpose -x Get multi-columnar output. -r List files & sub-directories in reverse order. -t List files & sub-directories in time order. -a List all files including hidden files. -A List all files excluding . And .. -c All files by inode modification time. -i List the inode for each file. -l Display permissions, owner, size, modification time etc.
  • 45.
    Ex - -$ ls - l total 20 drwxrwxr-- 2 user1 grp1 4096 Nov 29 01:51 Orissa drwxrwxr-X 3 user2 grp2 4096 Nov 29 01:54 Cool -rwxrw - - - - 1 user3 grp1 18 Nov 30 04:38 report drwxrwxr-X 2 user1 grp1 4096 Dec 09 10:50 south -rw –r- - - - 1 user1 grp1 23 Nov 29 01:54 king Type Permissions Links owner group Size in bytes Modification Date & Time File name
  • 47.
     . .. Special symbols can be used along with different commands. Wildcard Used to * Match any number of character including zero. ? Match one and only character [abc] Match one character which should be either a, b or c. [!abc] Match one character which is not a, b or c. [p-t] Match one character which falls within the range of p-t [!p-t] Match one character which does not falls within the range of p-t
  • 48.
    Examples $ ls p* (Displayall names of files & directories starts with p) $ ls ?? (Display all names of files & directories having any two characters) $ ls linux[1234] linux1 linux2 linux3 linux4
  • 49.
    $ ls linux[!12] linux4linux5 linux8 $ ls uni[x-z] unix uniy uniz (Display all files & directories starts with uni and followed by x, y & z.) $ ls Linux[!0-9] (Display all files & directories starts with Linux and followed by a non-numeric characters)
  • 50.
    Command name -- mkdir Purpose - - Creates a new directory. Syntax - - mkdir [path]<Directory Name>
  • 51.
    Ex- $ pwd /home/user1 $mkdir india home root (/) cool sunday user1 $ ls india $ mkdir /home/user1/cool/orissa india orissa
  • 52.
    Command name -- cd Purpose - - Change the current directory. Syntax - - cd [path][Directory Name]
  • 53.
    Ex- $ pwd /home/user1 $ls india Sunday cool home root (/) cool sunday user1 india $ cd india $ pwd /home/user1/india $
  • 54.
    $pwd /home/user1/india home root (/) coolsunday user1 india $ cd /home/user1/cool $ pwd /home/user1/cool $ cd $pwd /home/user1
  • 55.
    Session - 05 Objectives  Directory Management Commands  File management Commands  Redirection  Aim - - To provide knowledge about Directory & File management in Linux
  • 56.
    Command name -- rmdir Purpose - - Delete a directory if it is empty. Syntax - - rmdir [path]<Directory Name>
  • 57.
    Ex- $ ls indiacool sunday home root (/) cool sunday user1 india $ rmdir india $ ls cool sunday
  • 59.
    Command name -- cat Purpose - - Used to create & to display the content of a file. Syntax - - cat <filename>
  • 60.
    Ex- - $cat> myfile1 write the contents of file. (ctrl-d) $ $cat myfile1 contents of myfile1. $
  • 61.
    Command name -- cp Purpose - - Creates copy of a file. Syntax - - cp <source-file> <destination-file>
  • 62.
    $ cp /home/user1/india/file1/home/user1/Sunday/file1 File1 (F) india home root (/) cool sunday user1 File1 (F)
  • 63.
    Command name -- mv Purpose - - Move a file from one directory to another. Rename a file. Syntax - - mv <source-file> <destination-file>
  • 64.
    $ mv /home/user1/india/file1/home/user1/Sunday/file1 File1 (F) india home root (/) cool sunday user1
  • 65.
    $ mv /home/user1/india/file1/home/user1/india/file2 File1 (F) india home root (/) cool sunday user1 File2 (F)
  • 66.
    Command name -- rm Purpose - - Delete a file. Syntax - - rm <filename> home root (/) cool sunday user1 File1 (F) Ex- $ pwd /home/usr1 $ rm file1
  • 67.
    Different views ofa file. Page wise - - pg, more Top of a file - - head Bottom of a file - - tail
  • 68.
    Page wise -- pg To display the contents of a file or a command at one time. $ pg linux01.txt Enter – new line
  • 69.
    Page wise -- more To display the contents of a file or a command at one time. $ more linux01.txt Space bar – next page Enter – next line
  • 70.
    head - -To display the top of a file. $ head (by default it will display top 10 lines) $ head -6 my_file (it will display only top 6 lines)
  • 71.
    tail - -To display the bottom of a file. $ tail (by default it will display bottom 10 lines) $ tail -4 my_file (it will display only bottom 6 lines)
  • 73.
    Command Name -- wc Purpose - - used to count the number of lines, words, characters of a given text or file. Options - -  - l  Count only the number of lines in a file  - w  Count only the number of words in a file  - c  Count only the number of characters in a file
  • 74.
    Ex - - $wc myfile 82 511 2313 myfile (lines) (words) (character) $ wc –l myfile 82 myfile (lines)
  • 76.
    Standard input Keyboard Standard output  Display Screen (Monitor) We can direct the input & output to other stream Command can be inputted from - -  Keyboard  A file (using redirection operator <, > and >>)  By another command ( using the redirection operator | ). (|- pipeline Symbol)
  • 77.
    Output can besend to - - Standard output (monitor) A file Another command Input Redirection Using operator Left chevron(<) Ex- - $ cat > f1 < f2
  • 78.
    Output Redirection Using operatorRight chevron(>) Ex- - $ cat myfile > newfile (Write the contents of myfile to new file) Using operator ( >> ) Ex - - $ cat myfile1 >> newfile1 (Append the content of myfile1 with newfile1)
  • 79.
    Connecting commands Using pipes( | ) Ex- - $ ls | wc –l Counts the number of files and directories. Output of ls is piped to wc command. Ex- - $ who | wc –l Counts the number of active login users. Output of who is piped to wc command.
  • 80.
    Session - 06 Objectives  File permission  Changing File permission  Aim - -
  • 82.
    In UNIX/LINUX, thereis a concept of user and an associated group The system determines whether or not a user or group can access a file or program based on the permissions assigned to them. Apart from all the users, there is a special user called Super User or the root which has permission to access any file and directory
  • 83.
    There are threepermissions for any file, directory or application program. The following lists the symbols used to denote each, along with a brief description: r — Indicates that a given category of user can read a file. w — Indicates that a given category of user can write to a file. x — Indicates that a given category of user can execute the file.
  • 84.
    Each of thethree permissions are assigned to three defined categories of users. The categories are: owner — The owner of the file or application. group — The group that owns the file or application. others — All users with access to the system.
  • 85.
    One can easilyview the permissions for a file by invoking a long format listing using the command ls -l. For instance, if the user user1 creates an executable file named test, the output of the command ls -l test would look like this: -rwxrwxr-x 1 user1 grp1 1251 Sep 26 12:25 test
  • 86.
    The permissions forthis file are listed are listed at the start of the line, starting with rwx. This first set of symbols define owner access. The next set of rwx symbols define group access The last set of symbols defining access permitted for all other users.
  • 87.
    This listing indicatesthat the file is readable, writable, and executable by the user who owns the file (user user1) as well as the group owning the file (grp1). The file is also readable and executable, but not writable.
  • 88.
    Default permission ofa file  rw-r--r-- Default permission of a directory  rwxr-xr-x Command to change the permission of a file or directory chmod <arguments> <file/ directory name>
  • 89.
    chmod Arguments Category OperationPermission u (owner) + (assign) r (read) g (group) - (deny) w (write) o (other) = (absolute Assignment) x (Execute) a (all)
  • 90.
    chmod Example -- $ chmod u+x myfile  assign execute permission to user. $chmod g-w myfile  remove write permission to group. $chmod og-x myfile  remove the execute permission of other & group. $ chmod o-rwx myfile  remove the all permission of others.
  • 91.
    chmod Example -- $ chmod ugo=r myfile  assign read permission to user, group, other by removing other permissions. Octal notation Read Permission = 4 Write Permission = 2 Execute Permission = 1 777 signifies all permissions and 000 signifies absence of all permissions.
  • 92.
    chmod Example -- instead of $ chmod a=r myfile we can give $ chmod 444 myfile instead of $ chmod ugo=rw myfile we can give $ chmod 666 myfile
  • 93.
    Session - 07 Objectives  Changing ownership  Changing group ownership  Umask  File links  Aim - - Linux permission changing commands
  • 94.
    The creator becamethe owner of the file. Only owner can change the permissions of a file. The ownership of the file or directory can be changed using the command chown <owner> <file/directory name> Ex- - $ chown user4 myfile Grant the ownership of myfile to user4.  Ownership once surrendered cannot be re-established.
  • 95.
    The group ownershipof the file or directory can be changed using the command chgrp <group> <file/directory name> Ex- - $ chgrp grp2 myfile7 Grant the group ownership of myfile7 to grp2.  By default, the group owner of a file is also the group to which the other owner belongs.
  • 96.
    Default file permission umask – system variable, decides the default file & directory permission.  When the a file is created the permissions are set to  File – rw-rw-rw- (666)  Directory – rwxrwxrwx (777)  Value of umask is subtracted from original value.  umask – 022 For file - - 666 – 022 = 644 (rw-r--r--) For directory - - 777 – 022 = 755 (rwxr-xr-x)
  • 97.
    Default file permission To change permission $ umask 044 $ cat > new contents $ 666 – 044 = 622 new (rw--w--w-)
  • 98.
    Sticky Bit  Stickybit can be added to a directory to prevent the files within it from getting deleted.  No one can delete the files from the directory except owner and the root user.  To add sticky bit to a directory $ chmod o+t <directory name> $ chmod o+t ram (contents of ram directory will not get deleted by others)
  • 99.
    Session - 08 Objectives  File links  Aim - - Linux file links
  • 100.
    File Links  Everyfile has inode numbers to identified by the system  When we create a copy of a file then it will create two files with two different names and different inodes .  When we make a link of a file then we have single file, different names, same content and same inode numbers.  Links are of two types  Hard links  Soft links ( symbolic link)
  • 101.
     Hard links The ln command is used to create multiple links of a file. Example $ ln stuff junk $ ls –i 1659 junk 1659 stuff
  • 102.
     If wechange the contents of stuff file those changes will appear in junk file and vice versa.  If we delete one of the file. Then one of the link to that file will be deleted  A file is truly deleted from the file system only when it has no links to it.  By default a file has just one link.  A directory contains two hard links  . ( a link pointing itself)  .. ( a link to pointing to its parent)
  • 103.
     Symbolic link( soft link)  It is another type of link which is different from hard link.  A symbolic link allows us to give a file another name, but does not link the file by inode number ln –s stuff sub The file having symbolic link have the permission rwxrwxrwx $ ls –l -rw-rw-rw- 2 user1 grp1 23 Jul 24 04:19 stuff lrwxrwxrwx 1 user1 grp1 5 Jul 24 05:33 sub  stuff $
  • 104.
    Difference between hardlink & soft link  One can create a symbolic to a file that does not exist. The same is not possible with hard links.  Symbolic links identifies the files they point to. But with hard links, there is no easy way to determine which files are linked to the same inode.  You can hard-link files only when they are on the same file-system; symbolic links do not have this reflection.
  • 105.
    File handling commands od – used to display data in octal format  Also used for printing the non printable characters.  -b – option used along with od to display each character separately  -c – option used with –b option to display the character with octal code  Each line of the output displays 16 bytes of data in octal, preceded by the position in the file of the first byte in the line.
  • 106.
    $ cat >f3 rkcit college $ $ od f3 0000000 066551 062560 064562 066141 061412 066157 062554 062547 $ $ od -b f3 0000000 151 155 160 145 162 151 154 012 143 157 154 154 147 145 0000020 012 0000021 $
  • 107.
    $ od -bcf3 0000000 151 155 160 145 162 151 154 154 012 143 157 154 154 145 147 145 i m p e r i a l n c o 1 1 e g e 0000020 012 n 0000021 $
  • 109.
    Session - 09 Objectives  File comparison  Access modification time change  Aim - - touch command
  • 110.
    Comparing two files cmp – this comand is used to check whether two files are identical or not  Syntax – cmp file1 file2  Compare two files byte by byte  Example $ cat > f1 rkcit collegE $cat > f2 rkcit College $
  • 111.
    $ od -bf1 151 155 160 145 162 151 141 154 040 143 157 154 154 145 147 105 $od -b f2 111 155 160 145 162 151 141 154 040 103 157 154 154 145 147 145 $cmp f1 f2 f1 f2 differ: byte 1, line 1 $cmp –l f1 f2 1 151 111 10 143 103 16 105 145
  • 112.
    Common between twofiles  comm - used to check the comm things in the files  Both files should be sorted and also have some difference  Syntax – comm file1 file2  The command displays three collumn output 1. Contains Unique contents of first file 2. Contains unique contents of second file 3. Common contents of both file
  • 113.
    $cat > f1$cat > f2 apple ball Fruits grapes john john pass mike stel people xerox result $ stel uninor $
  • 114.
    $ comm f1f2 apple ball fruits grapes john mike pass people result stel uninor xerox
  • 115.
    Session - 10 Objectives  Vi editor  Different modes  Switching between modes  Commands used in vi  Aim - - working in a vi editor
  • 116.
    VI editor  Viis the most popular text editor available in almost UNIX and LINUX systems.  It is entirely different from other modern text editors  vi uses different types of commands to perform different operations.  Vi editor has three modes  Command mode  Input mode  Ex mode
  • 117.
    Modes of vi Command mode  Where you place your commands, when we invoke vi editor we will initially placed in this mode.  Input mode  It also known as input mode  In this mode we can add text to the file  Ex mode  Enables us to give commands at the command line.
  • 118.
    Commands given incommand mode  h – move one character left  l – move one character right  k – move up  j – move down  G – move cursor to the beginning of the last line in the file  #G – move cursor to the beginning of the #th line.  i – insert text before the current cursor position  I – insert text at the beginning of the current line  a – append text after the cursor  A – append text at the end of the current line  r – replace one character
  • 119.
    Commands given incommand mode  R – go on replacing characters until ‘esc’ key is pressed  s – replace the character at the cursor with any number of characters  o – open a new line below the current line  O – open a new line above the current line  x – delete one character at the cursor  X – delete one character to the left of the cursor  dd – delete the entire line  D – delete reminder of line after cursor  p – paste the delete text
  • 120.
    Commands given incommand mode  . – repeat the last executed command  u – undo the effect of the last command  H – move to the first line on the screen  M – move to the middle line of the screen  L – move to the last line of the screen  Ctrl + f – show the next screen  Ctrl + b – show the previous screen
  • 121.
    Commands given inEx mode  :w – save the changes you have made to the file  :q – quit vi without saving changes  :wq – save and quit  :q! – force quit if ‘q’ does not work
  • 122.
    SWITCHING BETWEEN MODES Exmode command mode Input mode a A I I O o S s R r esc :w :q :wq :q! esc
  • 123.
    Session - 11 Objectives  Vi editor  Commands used in vi  Aim - - working in a vi editor
  • 124.
     b –move back to the beginning of word  e – move forward to the end of the word  w – move forward to the beginning of the word  yy-copy a single line  J – join single lines  Repeat factor  To repeat a single command multiple times the vi editor provides the repeat factor mechanism. We have to prefix a number with the command to which we want to repeat.  3dd – delete 3 lines  5yy – copy 5 lines  4j - move 4 lines downwards
  • 125.
     Searching ? =Search for the pattern in backward direction ?text / = search for the text in forward direction /text n = find next in same direction N = find next in opposite direction
  • 126.
    Creating files invi vi <filename>  opens an existing file  Creates a new file if it doesn’t exists
  • 127.
     Saving linesto a new file . = specify current line $ = specify last line :1,5w <filename> :1,.w <filename> :.,$w <filename> :1,$w <filename>
  • 128.
     Replacement  :s :Address s/old/new  :s/rkcit/rkcit  Changes the first occurrence of rkcit with RKCIT in the current line  :s/rkcit/RKCIT/g  Changes the all occurrence of rkcit with RKCIT in the current line  :1,5 s/best/worst  It changes the first occurrence of best with worst from line 1 to 5.  :1,$ s/worst/best/g  Changes all occurrence of worst with best in the entire file
  • 129.
     Inserting anotherfile :r :r filename  Inserting output of a command :r ! Command name :r ! ls – l
  • 130.
    Session - 12 Objectives  Vi editor  Commands used in vi  Aim - - working in a vi editor
  • 131.
     Abbreviation  Insertingauto text (abb)  :abb rkc rkcit college  Mapping  Defining our own command  :map (ctrl+v)key command  :map del x  :map q l
  • 132.
    Handling multiple filesin vi  Vi editor facilitates to work with multiple files symontenously  $ vi one two three It will open one file  To switch to next file : n To switch to previous file :N To switch between current & previous file :e# To switch to a specific file :e <filename>
  • 133.
    Session - 13 Objectives  User defined variables  Escaping and quating  Aim - - shell variables
  • 134.
     All Linuxshell contains a list of variables which are known as environment variables.  We can define our own variables and also display the value with echo command by prefixing a $ (dollar) symbol.  HOME  user home directory  SHELL  contains shell of the user  PATH  contains the list of directory name in which the executable file resides  PS1  primary command prompt  PS2  secondary command prompt  LOGNAME  contains login name of the user. Shell variables
  • 135.
     User definedvariables $ a=120 $ echo $a 120 $ b=“rkcit” $ echo $b rkcit $ Shell variables
  • 136.
     Escape andquoting mark can made to explicitly to a special character.  difference between single quotes and double quotes  Single quote – to omit the special meaning of any meta character  Double quote – used in part of the command in which we don’t want to omit the meaning of meta character  Ex-  If we want to display *character with echo command it will display all the file names in the home directory. $ echo * (will not work) $ echo ‘*’ $ echo “*” Escaping & Quoting
  • 137.
    $echo my homeis $HOME My home is /home/user5 echo ‘my home is $HOME’ My home is $HOME echo “my login name is $LOGNAME” My login name is user6
  • 138.
    -  Back quote-to evaluate a command in the command line $ echo Current time is `date` Current time is Sat Aug 19 10:10:01 BDT 2011 $ echo “my current working directory is `pwd`” My current working directory is /home/user3/mohan $a=`date` $echo $a Sat Aug 19 10:10:10 BDT 2011
  • 139.
     Alias –kourne and bourne shell provides a command alias to assign a short name to an existing command $alias p=pwd $p /home/user2
  • 140.
     cut command Used to filter particular columns or fields out of a file.  Options of cut  -c = extract columns  -f = extract fields  - d = specify the delimiter (separator)  Examples $ cut –c2 emp.lst $ cut –c 2,5,9 emp.lst $ cut –c 2,3,4,5,6 emp.lst Or $cut –c 2-6 emp.lst Filters and Regular Expressions
  • 141.
    $cut –d “t”-f4 emp.lst $ cut –d “|” –f2,5 emp.lst $cut –d ‘|’ –f2,5 emp.lst $cut –d | -f2,5 emp.lst $ cut –c 10- emp.lst from 10th character to end $cut –c -5 emp.lst from 1 to 5 $ cut –d ‘|’ –f3- emp.lst 3 to last $cut –d | -f2 emp.lst > c1 Filters and Regular Expressions
  • 142.
    Session - 14 Objectives  Filter commands  Aim - - provide knowledge about filter commands cut, grep, paste
  • 143.
     Translating character(tr)  This command is used to replace one character with another one at the time of displaying. $ tr ‘a’ ‘A’ < file1 $ tr ‘|’ ‘=‘ < emp.lst $ tr ‘|’ ‘t’ < emp.lst  Options of tr  - d = we can delete a character at the time of displaying $tr –d ‘|’ < emp.lst $ls –l | tr –d ‘-’ Filters and Regular Expressions
  • 144.
     -s =(squeeze) consecutive occurrence of the characters remove the Consecutive duplicate characters $tr –s ‘ ‘ emp.lst $ls –l | tr –s ‘ ’ | cut –d “ ” –f 3,5 Filters and Regular Expressions
  • 145.
     grep command It means global regular expression and print. It searches for a pattern and display the selected lines containing the pattern $ grep good emp.lst $ grep cuttack emp.lst display all employees from cuttack $ grep sambalpur emp.lst emp2.lst $ grep “sasank gupta” emp.lst  Options of grep command  -I = ignore case  -v = display the non-matching characters  -n = display the line numbers Filters and Regular Expressions
  • 146.
     -c =count the number of lines matching the pattern  -l = display only the file name containing the pattern  -e = specify multiple pattern  -E = specify multiple pattern  $grep ‘ctc  > bbsr’ emp.lst  grep –E ‘ctc|bbsr’ emp.lst  egrep ‘ctc|bbsr’ emp.lst  -f = specify the file name that containing the pattern Filters and Regular Expressions
  • 147.
     sort command Arrangethe files in a specific order Sort [option] <filename> Options -t = specify the delimiter -k = specify the field number -r reverse order -c = check whether the file is sorted or not. Filters and Regular Expressions
  • 148.
     -u removethe duplicate line  -o specify the output file in which the sorted information will store  Sort –t “ ” –k 4 emp.lst  Sort –t “ ” –k3,4 emp.lst  Tab=`echo –e “t”` sort –t “Tab” –k2 emp.lst  Sort – t “ ” –c emp.lst  Sort –t “|” –k2 –c emp.lst  Sort emp.lst –o employee
  • 149.
    Session - 15 Objectives  Regular Expressions  Aim - - provide knowledge about Regular Expressions
  • 150.
     Regular Expressions If we want to match similar patterns with a single expression we can use the method of regular expression using the pattern matching concepts we can represent multiple expressions with a single pattern the regular expression is of 2 types and contain the own characters there own characters sets.  Basic regular expressions (BRE)  Extended regular expressions (ERE)  Characters in Basic Regular Expressions  * = zero or more occurrence of previous character $ grep de*pak emp.lst  . = Any single character  $ grep D.pa emp.ls t  [abc] = match any character from the range  $ grep g[ie]ta emp.lst  [a-z] = match any single character from the range  $grep BCA[0-9] emp.lst Filters and Regular Expressions
  • 151.
     [^abc] =any character other than the list  [^a-z] = any character other than the range  ^a = line starting with character a  ^[^R] = line doesn’t starting with R  A$ = lines ending with character A  List out all the directories.  List out all the files Filters and Regular Expressions
  • 152.
     Character setsin Extended Regular Expressions  ERE makes it possible to match dissimilar pattern with a single expression.  It uses some additional characters such as: +  it matches 1 or more occurands of previous characters ?  it match ‘0’ or ‘1’ occurance of previous characters  Sed =It is a filter command like grep having some additional facilities than grep.  It is known as the stream edition.  It is a multipurpose tool having the features of several filter commands.  $ sed option ‘address action’ filenames  In address and action we can specify the line number and the commands for operation  p  print the selected line  q  to display the selected lines  w  save selected lines to a new file $ sed ‘5q’ emp.lst (first 5 lines) $ sed ‘2,6q’ emp.lst ( display all by repeating from 2 to 6) Filters and Regular Expressions
  • 153.
    $ sed ‘2,$5p’emp.lst (all lines by repeating lines 5 and end of the files)  -n  used to display selected lines  $ sed -n ‘5,8p’ emp.lst  $ sed –n ‘8,$p’ emp.lst  $ sed –n ‘8,15!p’ emp.lst  $ sed –n 3,5p >9,20p’ emp.lst  $ sed –n ‘2,5 w new.txt’ mp.lst Filters and Regular Expressions
  • 154.
    Session - 16 Objectives  Sed command  Shell Programming  Aim - - provide knowledge about Regular Expressions, Shell programming.
  • 155.
    Context addressing  Withcontext addressing mechanism we can address the lines by specifying a pattern. The lines containing the specified patterns will display.  $ sed –n ‘/bgh/p’ emp.lst  $ sed –n ‘/bgh/!p’ emp.lst  $ sed –n ‘/sales/p >/cuttack/p’ emp.lst  $ sed –n ‘/sales/w new’ emp.lst Substitution using sed  s- substitute  $ sed ‘s/old/new/g’ filename  $ sed ‘5,15 s/ctc/cuttack/g’ emp.lst  $ sed ‘/sales/s/ctc/cuttack/g’ emp.lst Filters and Regular Expressions
  • 156.
     Deleting blanklines from a file  Display name and address of employees who were from sambalpur from last 10 lines.  Who | grep –c “^$LOGNAME” Filters and Regular Expressions
  • 157.
     Shell scriptor shell programming is a mechanism to execute multiple commands in a group.  when group of commands have to be executed regularly they should be stored in a file and the file itself executed as a shell script.  The extension of shell program is .sh.  When we execute the shell program it execute all the commands in it.  We can execute a shell program with the command  .sh  Sh  If a shell program has executable permission we can execute it directly Shell Script
  • 158.
     Wap todisplay your name and address. $ cat > oca.sh /vi oca.sh echo STUDENTS OF BCA II echo rkcit College echo budharaja echo sambalpur  Wap to display your login name, shell and Home directory  expr = it is a command line calculator program through which we can perform calculations both numerical and string  $ expr 5 + 6  $ a=100  $ b=200  $ expr $a + $b Shell Script
  • 159.
     Wap todisplay the values of 2 variables clear x=10 y=20 echo value of x= $x echo value of y=$y  Wap to add 2 numbers clear x=12 y=23 echo “addition =`expr $x + $y`” Or Z=`expr $x + $y` Printf “value of x=%d”$x Printf “value of y=%d”$y Printf “addition of %d & %d is %d”$x$y$z Shell Script
  • 160.
    Session - 17 Objectives  Shell Script  Read  Command line arguments  Conditional statement  If else  case  Aim - - provide knowledge about shell script
  • 161.
     read =used to take input from the user  Wap to input one no & display its value clear echo “Enter a number” read a Printf “value of x=%d”$a  Wap to input two numbers and display them clear echo “enter 1st number” read a echo “enter 2nd number” read b c=`expr $a + $b` printf “addition of %d & %d is %d”,$x$y$z Shell Script
  • 162.
     Command line Argument means the information we provide to the system  The information with the command in the command line is known as command line argument  Shell program accept the arguments from the command line.  Shell uses different variables to manage the command line  $@ / $* = display the list of arguments  $# = display the no of arguments  $1 $2 $3 $4 ……….$n = represents positional parameters in the command line Shell Script
  • 163.
    $ set rkcitbca 2nd 100 $ech o $* Imperail bca 2nd 100 $ echo $# 4 $ echo $1 rkcit $ echo $2 bca  Wap to display the addition of 2no’s provided in the command line? (add.sh) a=$1 b=$2 c=`expr $a + $b` echo $c $ add 120 150 270 $ sh add.sh 100 200 300 Shell Script
  • 164.
    Conditional Statements The shellscript allows to implements the conditional statement for conditional execution with the help of following two techniques: if Statement case Statement if statement Syntax : if command then execute the command else execute the command fi Shell Script
  • 165.
     Wap todisplay a message if the pattern is found. $ cat > search1.sh if grep ‘manager’ emp.lst > /dev/null then echo “pattern found” else echo “pattern not found” fi  Exit stats of a command = success or failure of execution of a command is known as exit status.  The state is “0” in case of success  The state is any non-zero value in case of failure  The $? Variable contains exit status of last executed command. Shell Script
  • 166.
    $ cat oca $echo$?  test = used to check whether a condition is true or false  We can use this command to evaluate a condition $ test 5<10 $ echo $? Operators used in shell programming -eq =equals to -gt = greater than -lt = less than -ge = greater than equals to -le = less than equals to -ne = not equals to Shell Script
  • 167.
     Wap toinput 2 numbers and display the biggest one echo “enter two numbers reand a,b if test $a –gt $b then echo $a is big else echo $b is big fi Shell Script
  • 168.
    echo “enter twonumbers reand a,b if test $a –gt $b then echo $a is big else if then $b –gt $a then echo $b is big else echo both are same fi fi Shell Script
  • 169.
    elif = incaseof nested if else we can use elif. Here no need to specify multiple fi echo “enter two numbers reand a,b if test $a –gt $b then echo $a is big elif then $b –gt $a then echo $b is big else echo both are same fi fi Shell Script
  • 170.
     Wap tocheck whether a number is even or odd echo “ enter a numbers” read a b=`expr $a % 2` if test $b -eq 0 then echo no is even else echo no is odd fi [ ] is substitute if test command If test `expr $a % 2` -eq 0 If [ `expr $a % 2` -eq 0] Shell Script
  • 171.
    Session - 18 Objectives  Multiple conditions  And & or Operator  Conditional statements  Case  Aim - - provide knowledge about shell script
  • 172.
     Contents of/etc/passwd file  This file contain the information about the users in it. A database is mentioned in this file to store the user information. It contains a line of information for each system user Such as : 1. User name 2. Password 3. Userid 4. Group id 5. Full name 6. Home directory 7. Shell user1:x:1001:1002:user1,,,,,:/home/user1:/bin/bash /etc/shadow= contains the character “x” In the password field. The user password is stored as encrypted string of 13 characters in the file /etc/shadow.
  • 173.
     The filecontains the password fields in an expanded format such as : 1. User name 2. Password 3. Password last change 4. Minimum number of date between password change 5. Maximum number of date password is valid 6. Number of days to warn the user to change password 7. Number of days the login may be inactive 8. Date when the login is no longer valid 9. A reserve field for future used
  • 174.
    Wap to inputyour login name and display its full name if user name doesn’t exists display a error message echo “enter the login name” read ln grep ln /etc/passwd > x1 if [ $? –eq 0 ] then a=`cut –d “:” –f5 x1 echo full name=$a else echo “invalid user” Shell Script
  • 175.
     Multiple conditions We can use multiple conditions with –a for and operator and –o options for or operator  Biggest among 3 numbers echo “enter 3 numbers” if [ $a –gt $b –a $a –gt $c ] then echo “$a Is big” elif [ $b –gt $a –a $b –gt $c ] then echo “$b is big” else echo $c is big fi Shell Script
  • 176.
    Session - 19 Objectives  Conditional statements  Case  Aim - - provide knowledge about shell script
  • 177.
     Wap toinput a single digit and display it in words  Case statement case expression in pattern 1) command;; pattern 2) command;; … … *) command;; esac
  • 178.
     Wap toinput a digit and display it in words echo enter a digit read a case $a in 0) echo zero;; … … *) echo invalid digit;;  Wap to input a character and check whether it is a vowel or consonant  Wap to check whether it is an alphabet, digit or special symbol.
  • 179.
    Session - 20 Objectives  String comparison  Looping  Aim - - provide knowledge about shell script
  • 180.
     String comparision = are used to compare two strings  != not equals to  Wap to input 2 strings and check whether they are equal or not echo “input 1st string” read a echo “input 2nd string” read b if [ $a = $b ] ;then echo “ strings are equal” else echo “strings are not equal” fi
  • 181.
     && and|| two conditional operators  Command 1 && command 2  In the above case the command2 executes in the success of command1 $ cat new.txt && date $ grep ‘sales’ emp.lst && echo found  Commmand 1 || command 2  In the above case command 2 executes in the failure of command 1 $ cat new.txt || date $ grep ‘sales’emp.lst || echo “not found”
  • 182.
     Looping inshell script  Shell script provides 3 types of loops  While  For  Until  While loop while condition is true do commands ---- ---- done
  • 183.
     Wap todisplay a name five times clear a=1 while [ $a –le 5 ] do echo “rkcit” a=`expr $a + 1` done  Wap to display addition of all numbers from 1 to 50  Wap to display even numbers from 99 to 250  Wap to input a number & display the addition of all digits  Wap to input a number and display each digit in word  Wap to input a number and check whether it is prime or composite  Wap to display all prime numbers from 200 to 500  Wap to check whether a number is strong or not?
  • 184.
    Session - 21 Objectives  Shift command  Aim - - provide knowledge about shell script & procwess
  • 185.
     Shift command Shift the positional parameters to left $ set 10 hari 9th 1st $ echo $1 10 $ shift $echo $1 hari Or clear n=$# a=1 while [ $a -le $n] do echo $1 shift done
  • 186.
    Session - 22 Objectives  AWK  Aim - - provide knowledge about shell script & procwess
  • 187.
     Wap todisplay the addition of all the command line arguments?  wap to add new records to employee data base? clear echo “enter employee id” read id echo “enter employee name” read nm echo “ enter employee address” read ad echo “enter employee salary” read sal echo “$id | $nm | $ ad” >> emp.lst
  • 188.
     A processis simply an instance of running program.  a file is treated as a simple file when it lies in a dormant state. It can also be treated as a process when it is under execution  Like human beings processes are born, they give birth of to another process and also die.  A process is said to be born when the program starts its execution.  It remains alive as longer the program is active. After execution is completed the process is said to be dead.  It Is the kernel but not the shell that is ultimately responsible for the management of the processes.  It determines the time and properties that are allocated to a process. So that multiple processes can share a single CPU.  Like files processes are also have some attributes. All the attributes of processes are maintained by the kernel and stored in the process table known as PCB (Process Control Board). Process
  • 189.
     Attributes ofPCB  PID = > each process can be uniquely identified by an integer values known as PID. The PID number is assign to a process during the start of the execution.  PPID = > this is the process of the parent process other than the properties there are some additional associated with the process such as :-  Process name  Terminal name  Priority  Born time  CPU time  Status  When we logon to the Linux shell processes immediately created by the kernel which is known as login process. This process represents a linux command which may be sh, ksh, csh,etc.  Any command that we type at the command prompt is actually the standard input to the shell process. The shell process remains active until we logout. Process Control Board
  • 190.
     PS command Each process is associated with some attributes such as pid number, ppid number, user id number, execution time, terminal name, etc  To display the attributes associated with the process we can use the command PS.
  • 191.
     PS command Each process is associated with some attributes such as pid number, ppid number, user id number, execution time, terminal name, etc  To display the attributes associated with the process we can use the command PS.
  • 192.
     Killing process The linux system requires to communicate with the process, we can send a signal to a process  Each signal is identified by a number and designed to perform a specific operation  If a program is running longer than the expected time, we want to terminate an executing program we can use the command kill.  Syntax  $ kill option pid_numbner  Option  -9 = we can use this option to kill the process force fully.
  • 193.
     NICE  Linuxcan run a lot of processes at a time, which can slow down the speed of some high priority processes and result in poor performance.  To avoid this, you can tell your machine to prioritize processes as per your requirements.  This priority is called Niceness in Linux, and it has a value between -20 to 19. The lower the Niceness index, the higher would be a priority given to that task.  The default value of all the processes is 0.  To start a process with a niceness value other than the default value use the following syntax  nice -n 'Nice value' process name
  • 194.
     DF  Thisutility reports the free disk space(Hard Disk) on all the file systems.
  • 195.
    System Call  Asystem call is a mechanism that provides the interface between a process and the operating system. It is a programmatic method in which a computer program requests a service from the kernel of the OS.  System call offers the services of the operating system to the user programs via API (Application Programming Interface). System calls are the only entry points for the kernel system.  An operating system can roughly be divided into two modes:  Kernel mode: A privileged and powerful mode used by the operating system kernel
  • 196.
    Why do youneed System Calls in OS?  Following are situations which need system calls in OS:  Reading and writing from files demand system calls.  If a file system wants to create or delete files, system calls are required.  System calls are used for the creation and management of new processes.  Network connections need system calls for sending and receiving packets.  Access to hardware devices like scanner, printer, need a system call.
  • 197.
    Types of Systemcalls  Process Control  File Management  Device Management  Information Maintenance  Communications
  • 198.
    Important System CallsUsed in OS  wait()  In some systems, a process needs to wait for another process to complete its execution. This type of situation occurs when a parent process creates a child process, and the execution of the parent process remains suspended until its child process executes.  The suspension of the parent process automatically occurs with a wait() system call. When the child process ends execution, the control moves back to the parent process.
  • 199.
     fork()  Processesuse this system call to create processes that are a copy of themselves. With the help of this system Call parent process creates a child process, and the execution of the parent process will be suspended till the child process executes.
  • 200.
     exec()  Thissystem call runs when an executable file in the context of an already running process that replaces the older executable file. However, the original process identifier remains as a new process is not built, but stack, data, head, data, etc. are replaced by the new process.  kill():  The kill() system call is used by OS to send a termination signal to a process that urges the process to exit. However, a kill system call does not necessarily mean killing the process and can have various meanings.
  • 201.
     exit():  Theexit() system call is used to terminate program execution. Specially in the multi-threaded environment, this call defines that the thread execution is complete. The OS reclaims resources that were used by the process after the use of exit() system call.
  • 202.
    Shell  SHELL isa program which provides the interface between the user and an operating system. When the user logs in OS starts a shell for user. Kernel controls all essential computer operations, and provides the restriction to hardware access, coordinates all executing utilities, and manages Resources between process. Using kernel only user can access utilities provided by operating system.
  • 203.
    C Shell  Denotedas csh  Bill Joy created it at the University of California at Berkeley. It incorporated features such as aliases and command history. It includes helpful programming features like built-in arithmetic and C- like expression syntax.  In C shell:  Command full-path name is /bin/csh  Non-root user default prompt is hostname %  Root user default prompt is hostname #
  • 204.
    Bourne Shell  Denotedas sh  It was written by Steve Bourne at AT&T Bell Labs. It is the original UNIX shell. It is faster and more preferred. It lacks features for interactive use like the ability to recall previous commands. It also lacks built-in arithmetic and logical expression handling. It is default shell for Solaris OS.  For the Bourne shell the:  Command full-path name is /bin/sh and /sbin/sh  Non-root user default prompt is $  Root user default prompt is #.
  • 205.
    Korn Shell  Denotedby ksh  It Was written by David Korn at AT&T Bell LabsIt is a superset of the Bourne shell.So it supports everything in the Bourne shell.It has interactive features. It includes features like built-in arithmetic and C-like arrays, functions, and string-manipulation facilities.It is faster than C shell. It is compatible with script written for C shell.  For the Korn shell the:  Command full-path name is /bin/ksh  Non-root user default prompt is $  Root user default prompt is #.
  • 206.
    Bourne-Again Shell  Denotedas bash It is compatible to the Bourne shell. It includes features from Korn and Bourne shell.  For the GNU Bourne-Again shell the:  Command full-path name is /bin/bash  Default prompt for a non-root user is bash-g.gg$ (g.gg indicates the shell version number like bash-3.50$)  Root user default prompt is bash-g.gg#.

Editor's Notes

  • #93 Leenooks Linnuks lynuks
  • #99 Leenooks Linnuks lynuks
  • #109 Leenooks Linnuks lynuks
  • #119 Leenooks Linnuks lynuks
  • #121 Internal & external Portable than other Linux / unix features
  • #127 Leenooks Linnuks lynuks
  • #134 Leenooks Linnuks lynuks
  • #137 Leenooks Linnuks lynuks
  • #146 Leenooks Linnuks lynuks
  • #153 Leenooks Linnuks lynuks
  • #158 Leenooks Linnuks lynuks
  • #164 Leenooks Linnuks lynuks
  • #175 Leenooks Linnuks lynuks
  • #178 Leenooks Linnuks lynuks
  • #180 Leenooks Linnuks lynuks
  • #183 Leenooks Linnuks lynuks
  • #188 Leenooks Linnuks lynuks
  • #190 Leenooks Linnuks lynuks