LGLinux 1 PDF
LGLinux 1 PDF
LGLinux 1 PDF
Environment
R.Krishna Murthy
Chief Research Scientist
Supercomputer education and Research Centre
Indian Institute of Science
___________________________________________________________________________________
E-mail: arkay.murthy@gmail.com
Phone: 23600653,4,9
Course Objective:
The course aims at providing the participants an overview of the
major features of Linux from a end user's perspective. At the end of
the course the participants will have adequate background to use
Linux for routine computing. This course will provide the requisite
background to pursue an advanced course for program
development in the Linux environment
Pre-requisites:
The participants are expected to be familiar with the use of
computers in interactive mode , programming in any high-level
language and in the use of standard tools like editors, compilers etc.
Course Coverage:
Lectures, Tutorial walk-through, Reading and Hands-on
assignments
INTRODUCTION TO LINUX
Version 2.0 June 1996- Support for Mac processors and SMP
Types of OS :
Multiprogrammed systems - more than one job scheduled for execution.
INTRODUCTION TO UNIX
Unix is an interactive , multi-user multitasking operating system
supporting a hierarchical file system and a command language
selectable on a per user basis
Developed by Ken Thompson and his associates at Bell labs in
1969 on a PDP-7
Popularity of Unix
High-level language implementation
and consequent portability ensured wide availability
micros to super computers
Unix provided a rich set of operations and commands
that supported the building block approach to problem
solving
uniq
tr cs [a-zA-Z] \015 < file | sort | uniq
STRUCTURE OF UNIX
user
User
Commands
+ Data
UTTILITIES
transfer of
control
APPLICATIONS
UNIX
SHELL
System call
interface
hardware
interface
SYSTEM ARCHITECTURE
Comm drivers
and protocols
Pipes and
filters
System call
System
maintenence
services
UNIX
User
command
interface
Hardware
Directly
Executable
programs
Kernel
Interface
Configurable
environment
SHELL
SHELL is the Command Language. Users interact with the system
through the shell
UTILITIES
Executable programs for the users to perform a variety of standard
functions supplied with the system
CATEGORIES OF UTILITIES
General Operations
date,who,cal,bc
changing password
System Administration
Adding/Removing users
creating and restoring file systems
Application Development
Creating,Editing,Debugging,Maintaining and Profiling Software
File and Process related Utilities
APPLICATIONS
with system
Unix/Linux Philosphy
Unix Philosophy by Mike Gancarz
Why Linux ?
Linux is free
Linux is fully customizable in all its components source
available for customization under GPL
Linux is available on a wide range of platforms PCs to
supercomputers
Linux systems are stable low failure rate
Linux kernel is small and compact
Linux is compatible with many OS
Linux is well supported lot easier and faster to get
patches than proprietary
User identification-name
Password
Home directory and Shell
Resource allocation and limits
stty command
Allows setting of terminal parameters
e.g.. vi usage
Logging out of the system
Your identity in the system on logging in
Process identification
Group identification
Interacting terminals identity
The id command
The logname command
The ps command
The who command
Interaction with the system is through commands
Documentation on Linux
Man pages the man command
man man
3. Library routines
4. Device related information
5. File format descriptions
6. Games
7. Miscellaneous
8. System Administration
9. Kernel related
e.g. : wc file
wc l file
Shell Prompt
The character echoed by the shell to indicate its readiness to
accept the next command
System permits type ahead
Correcting typing mistakes
erase character - #
erase line typed so far (kill character)@
FILE SYSTEMS
Unlike user files which contain information stored by the user, directories
contain information about other files and directories
In Unix separate space on the file system is allotted for storing file contents
and information about the file
Information about files are stored in a structure called index nodes (Inodes)
(except name of file , which is stored in the directory)
Symbolic link (soft links) are data files containing the name of a file it is
supposed to link
Indirect addressing
Named Pipes
File Names
Special filenames
/ root directory
. Current directory
.. Parent directory
~ shell replaces it with a home directory
Wildcards
Wildcard notation helps in specifying groups of files in a concise form
without exhaustive enumeration
Wildcard expansion in filenames is done by shell
Wild Pattern
?
*
[a b]
[a-z]
[!a-z]
Matches
Any single character
Any group of zero or more characters
either a or b
any one character between and z inclusive
any one character other than a to z
Wild Pattern
?(abc)
+(abc)
!(abc)
Matches
zero or more instance of abc
one or more instances of abc
match anything that does not contain abc
Examples
*.txt
chapter.[0123]
x?(abc)x
Extension :
while there is no concept of an extension required by OS ,
some utilities make use of It to indicate the nature of Content of the
file.
Example:
Extension
Description
.txt
.tar
.c
ASCII text
tar archive
C source files
PATHNAMES
Absolute pathnames
/bin
/dev
bin
/etc
/lib
games
lib
adm
etc
adm
adm
include
adm
/u
/mnt
skel
man
local
adm
/usr
preserve
adm
adm
/tmp
tmp
spool
ucb
adm
adm
Description
/bin
/dev
/etc
Directory
/lib
Description
This directory contains library files needed for
system applications.
/opt
/tmp
/usr
Consider the typical Unix file system tree shown in the accompanying
figure
Example
The absolute pathname of the file news is /usr/spool/news
The relative pathname for the file news if you are located currently in
spool is news
Links
Association of filenames with the files (inodes) constitutes a link
Link is established in directories
Links are established when files are created or when an alias is created
to an existing file
Link count of a file represents the number of references to that file
DIRECTORY
Name
INODE
A
Z
HARD LINKS
DIRECTORY
INODE
DIRECTORY
B
INODE
Symbolic Link
FILE ATTRIBUTES
File name is not stored in Inode
File Ownership
User owner
Group owner
This facilitates sharing
Size , access and modification times
Retrieval information specify which blocks belong to the file
FILE PERMISIONS
Owner uid of owner of the object
same as the uid of accessor
group gid of owner is same as gid of object
Miscellaneous bits
- set user (or group) id (s)
- sticky bit ; save text (file) or prevent removal of files by nonowners (directory) (t)
Specification of permission numeric mode
3 digits corresponding to the three categories owner,group,world
FILE PERMISSIONS :
Access Modes
Access type
r
w
x
Directory
Command
cd /home/hk
N/A
ls /home/rk/*.c
none
Ls s /home/ rk/*.c
none
cat text
Program
x(bin)
rx(script)
rm program
none
wx
Access Permission
Meaning
No access
rx
rwx
Access Classes
user (u), group (g), others (o)
-t
-s
-S
-l
Name
Meaning
File/Directory Commands
PROCESS
A Process is an instance of a program that is being
executed by the operating system.
A Process is created by the fork system call in UNIX.
Some operating system use the term task instead a
process.
Each process operates in its own address space
Process has a unique identity pid.
Every process has an owner the owners(user) identity is
internally stored a unique number uid(externally available
as a name).Uid 0 or root is has absolute privileges
Every user belongs to atleast one group(primary) gid. A
user can belong to multiple groups. Group is a mechanism
for controlled sharing
A Process typically has the arrangement shown as below:
The states through which a process passes together with the events
causing these transition is described by the state transition diagram
Ready
5
Start
Running
4
wait
stop
1. Job enters
2. Process Scheduled
3. Process preempted
4. Resource wait
5. Resource allocated
6. Process exits
PROCESS
Types :
Interactive Process : initiated from & controlled by a terminal
May run as foreground (attached to terminal) or background
Job Control
bg
fg %n
Batch Processes : are those which are not associated with a terminal
and submitted through a queue.
Daemons
: Server processes which are not associated with a
terminal initiated normally at the boot time and wait in the background
until some process requires service.
Process Attributes
Process Id (PID), Parent Process Id (PPID)
Nice number : a number indicating processes priority
relative to others, Used in computing
executing priority
tty
Real and Effective UID & GID
PROCESS ATTRIBUTES
Process states : ready , running, sleep, waiting
Process address space layout
User-area : all information relevant to process execution
it holds
real and effective uid ,gid
open file handles/descriptors
signal disposition
program invocation arguments
accounting information
etc
User context
Stack
Kernel context
heap
Un initialized data
Initialized
Read /write
Initialized
Read only data
text
exec
:
with
the command.
Execution of
a command(grep)
fork
Init
(pid 1)
Init
(pid 420)
exec
Login shell
(pid 420)
Process Control
nice command
Foreground , Background modes
sending signals
1 PROCESS CREATION
login shell
Process a
Pid = nnnn
Before fork
After fork
2. $cat file
Process a
Pid = nnnn
Process b
Pid = mmm
Before exec
Process b
Exec cat program
Pid= mmmm
Cat program is an
executable file
Exec
4.
Cat program
6. after exit
process a
Pid = nnnn
cat program
Pid =
mmmm
Process b is no longer
known to the login shell
and its memory area is
freed up
forks sh
Pid = aaaa
sh
execs
cft
cft
executes,
then exits
sh
execs
cft
cft
executes,
then exits
Kill command
kill signal process_id
The above sends signal signal to a process with pid
process_id
e.g. : kill -9 420
Kills process with pid 420
INPUT REDIRECTION
File
fred
terminal
standard
input
shell
program
(cat)
standard
output
shell
terminal
OUTPUT REDIRECTION
terminal
standard
input
shell
program
(cat)
standard
output
File
(joe)
shell
terminal
$ cat >fred
STANDARD I/O
terminal
standard
input
shell
program
(cat)
standard
output
shell
terminal
$ cat
PIPED PROCESSES
Sh
Forks sh
Forks sh
sh
Execs
who
sh
Who
execs
sorts
Executes
writing to
pipe
Pipe
buffer
$ who | sort
sort
executes
Reading
form pipe
PIPES
File
/etc/passwd
standard input
shell
Program grep
standard input
shell
standard output
Program
(sort)
standard output
shell
terminal
$ grep I joe /etc/passwd | sort
REGULAR EXPRESSIONS
Regular Expressions (RE) are used to specify text patterns for searching
and replacing
RE constitute a powerful language capable of describing complex
pattern classes
Several important Unix tools support the capabilities of RE
e.g. grep,fing ,sed,awk,vi
REs are not limited to literals . They can also contain metacharacters
The list of metacharacters in RE ,their function and the utilities
supporting them are described in the table
Metacharacters
The characters below have special meaning only in search patterns
. Matches any single character except new line
* Match any number (or none) of the single character that immediately
precedes it. The preceding character can also be regular expression
e.g. since . (dot) means any character , .* means match any number of
any character
\(\) save the pattern enclosed between \( \ ) into a special holding space.
Matches
^A
A$
A^
An A anywhere on a line
$A
$A anywhere on a line
^\^
^^
Same as ^\^
\$$
$$
Same as \$$
Regular Expression
Matches
[0-9]
Any digit
[^0-9]
[-0-9]
Any digit or a -
[0-9-]
Any digit or a -
[^-0-9]
[]0-9]
Any digit or a ]
[0-9]]
[0-99-z]
[]0-9-]
Any digit, a -, or a ]
Matches
\*
\\
^*
^A*
^A\*
Any line
Any line starting with an A*
^AA*
Regular
Expression
Matches
^AA*B
^A\{4,8\}B
Any line starting with four, five, six, seven, or eight A's
followed by a B
^A\{4,\}B
^A\{4\}B
\{4,8\}
A{4,8}
Result
s/.*/( & )/
/^$/d
:g/^$/d
ex version of previous.
/^[ tab]*$/d
spaces
:g/^[
tab]*$/d
s/*//g
:%s/
ex version of previous.
Turn one or more spaces into one space.
*/ /g
ex version of previous.
Command
:s/[0-9]/Item &:/
Result
Turn a number into an item label (on the current
line).
:s
:&
Same.
:sg
:&g
Same.
:%&g
:.,$s/Fortran/\U&/g
last line.
:%s/.*/ \L&/
:s/\<./\u&/g
current
:%s/yes/No/g
:%s/Yes/~/g Globally
Examples of searching
Pattern
bag
^bag
bag$
^bag$
[Bb]ag
Bag or bag.
b[aeiou]g
b[^aeiou]g
b.g
^...$
^\.
^\.[a-z][a-z]
^\.[a-z]\{2\}
^[^.]
bugs*
"word"
A word in quotes.
"*word"*
[A-Z][A-Z]*
[A-Z]+
[A-Z].*
[A-Z]*
[a-zA-Z]
Any letter.
[^0-9A-Za-z]
[567]
ex or vi pattern:
\<the
the\>
\<the\>
[]
\(\)
\{\}
\<\>
()
ex
*
sed
*
ed
*
Action
Escape character following.
\n
&
~
pattern.
\u \U
Change character's) to
uppercase.
\l \L
Change character's) to
lowercase.
\E
\e
FIND
find is one of UNIX's most useful and important utilities
It finds files that matches a given set of parameters (like name ,
permissions, size, access & modification times etc.)
Command format
find
path
operators (options)
Some of the options are
-name filename #filename
-perm mode
#access mode
-type c
#type of file - f, d, 1, b etc..,
-user name
-size n
#files that are ' n' blocks
+n => '>n' blocks
-n => '<n' blocks
optl -a opt2
find files which watch both optl
and opt2
optl -o opt2
file that match either
! operator
\(expression \)
- print
- exec command
Examples
1. find -name " *.o" -exec rm -f {} \;
2. find . -print
3. find ~ ~barny /usr/local
-print
4. find . -name " [a-zA-Z] *.o" \ -print
5. find
-mtime +6 -print
6. find
-name \*.p -perm 664 -print
7. find
-perm -100 print (if -perm argument is negative then all
permission bits including SUID bit is examined)
8. All SUID files of owner root find
-user root -perm -4000 -print
6. find /tmp -type f -mtime +7 \ -execute run {} \;
Command Format
egrep [options] [regular-expr] file(s)
options
-v print the lines that do not match pattern
-c Do not print lines but only the count of lines matching
-i ignore uppercase/lowercase distinction
-n print line numbers along with lines
-e print lines which begin with a - (minus)
-f get regular expression from a file
Examples
egrep -c . inpfile
egrep -n `, . *,.*, inpfile
cat exprfile egrep -n -f exprfile inpfile
SED
sed is a stream , editor
Commands of sed are similar to ed
sed can take input from a file, edit the text of the file , but the text of
the input file is not altered.
sed can take input from a command
sed is a stream editor and hence non-interactive
sed is useful for
1) Editing files too large for efficient interactive editing
2) Global editing. Only one pass is made on input
3) Complex editing which would be tedious for interactive editing
sed does not create a temporary file. A few lines are stored in core.
Hence more efficient than ed or vi
Execution of sed
Before executing commands sed commands are compiled into an
efficient form
One line is pulled from the the input stream & placed in pattern space
All commands act on this line. The next line is pulled and the above
process repeats.
Command format
sed [-n] [--e script] [-f file] [file ...
.]
-e argument is the command script for sed on the command line
-f
specifies that commands are not on the command line but in the
file. If files are specified input text is taken from then else sed takes
it from standard input.
SED EXECUTION
Addresses
addresses select lines from inputs to be operated on by the command
Two types of addresses
Line number Addresses
Context Addresses
Examples
1,20
/#/,$
/Begin/,/End
Commands
one character in length
Text substitution
s/regular expression/replacement/[gp]
g - substitute every occurrence of re
p - specifies line to be printed in case of substitution succeeds.
This is on by default.
Deleting Text
E.g.: '/^ $/d' double
Appending Text
-a
The text is written to the standard output after the line number
specified for 'a'. If no line number is given, the text added at the end.
AWK
Aho, Weinberger and Kernighan
Awk is a pattern scanning and processing language
awk [-f command file] [commands][file..]
Records and fields
Each line of input is a record which is terminated by a record
separator(RS) which is new line by default
The current record number is stored in NR
Every record is divided into fields which are separated by a field
separator (FS), space by default
The variable NF denotes the number of fields in the current record
Fields can be referenced by the notation $n where 'n' is the field
number. When 'n' is zero, $n refers to the entire record.
E.g.:
Awk program
Overall structure
pattern {action}
pattern {action}
action - valid awk statement or group of statements
action statements are optional when only pattern is specified, it
prints all lines containing the pattern
patterns can be regular expressions, defined BEGIN or END pattern
or relational pattern
pattern is also optional. Actions without patterns are executed
unconditionally
patterns are used to select lines when a pattern matches a line
or pattern it is said to be selected and the associated action is
performed
Text patterns
[abc]
[a-z]
[^abc] ()
|
*
+
?
-
^
$
~
!~
/etc/passwd
RELATIONAL PATTERNS
$ awk '$2 >= 10' data
$ awk '$1 >= "gr" data
Strings are enclosed in double quotes
string comparison done if neither expression is numeric
If data contains
awk
15
sed
12
tr
6
grep
8
cut
2
What is the output of the last two awk programs ?
Pattern combination
Patterns are combined using boolean operators
Symbol
Function
||
logical or
&&
logical and
!
logical not
$awk '$2>8
&& $2<14' data
sed 12
Actions
{print $1 $2} data
where data consists of 2 records listed
above then the output is,
tr
6
grep
8
{print $1, $2 }data
tr
6
grep 8
Output Field Separator (OFS)
$awk'BEGIN {OFS="="}
>
{print $1, $2} data
tr
6
grep 8
When white space separates the arguments of 'print', the output is not
separated by OFS.
Formatted print
printf "control-string" , argl,
control-string - is similar to that in "c"
Print redirection
{print " %s \t\t %o octal \n", $1, $2 >> comlog)
Variable assignment
var = "text" Assigned text
var = 9
Assigned numeric data
If var = "9", both var > 5 and var > "+" are valid
Arithmetic operation
All standard operations are available
Example
$ cat data
awk
15
sed
12
tr
6
grep
8
cut
2
$ cat prog
BEGIN {sum = 0}
{sum = $sum + $2)
END {printf "Total is % d\n", sum)
$ awk -f grog
Total is 43