UNIX
UNIX is, in short, a good operating system, for programmers.
-- Kernighan and Pike.
Pike
Education & Research Dept.
Infosys
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 1
Session Plan
Day 1
Introduction to Unix.
General UNIX commands.
Introduction to the on-line Tutor.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 2
Session Plan (Contd.)
Day 2
The vi editor
Regular expressions.
Text processing utilities.
Advanced commands.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 3
Session Plan (Contd.)
Day 3
The Shell variables.
The Korn shell.
The Shell programming.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 4
Session Plan (Contd.)
Day 4
Compression utilities.
Communication.
Software development tools.
Project
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 5
Session Plan (Contd.)
Day 5
The Project.
Submission by 2:00 pm.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 6
Some Selected References
Rebecca Thomas and Yates : A User
Guide to the Unix System, Mc. Graw
Hill, 1985
Kernighan and Pike : The Unix
Programming Environment, PHI, 1990
Stephen G. Kochan &
Patrick H. Wood : Unix Shell
Programming
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 8
Evaluation Strategy
Project.
Theory exam.
Tutor usage.
Assignments.
Quizzes.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 9
Introduction
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 10
What is UNIX ?
A very popular multi-user,
multitasking, operating system.
UNIX has become the operating
system of choice for engineering and
scientific workstations.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 11
Background
-- how was it developed?
1969 : Originally developed in Bell
laboratories by Ken Thompson and
Dennis Ritchie on a PDP-7 machine.
1973 : Re-written in high-level language
C , thus making it machine-
independent.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 12
Background
-- how was it developed?
1977-1982
– UNIX is made widely available at minimal cost.
– Becomes popular for scientific applications.
1983 System III.
1984 System V, Release …
– …………………
1992 4.4 BSD.
1999 LINUX, A freeware flavor of UNIX
by Linus Trivolds is released.
– ………………….
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 13
Unix has so many flavors
AIX.
HP-UX.
MINIX.
SCO.
SOLARIS. Two variations maintain
SUN OS. popularity today :
XENIX. AT&T System V &
LINUX. Berkeley Standard Distribution.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 14
Key features
what made UNIX different ?
Portability as it was written in C.
– achieving machine independence.
Background Processing.
Hierarchical File System.
Pipes and redirection tools.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 15
Key features
-- what made UNIX different ?
UNIX Shells.
Security.
Software development tools.
Stable and reliable OS.
Communication.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 16
UNIX OS structure
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 17
Major Components of UNIX
Kernel
Shell
Utilities
User applications
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 18
Unix System Architecture
User
User
User
User
SHELL
SHELL
UNIX OTHER
COMMANDS APPLICATIONS
HARDWARE
DATABASE KERNEL
COMPILERS
PACKAGES
SHELL
SHELL
User
User User
User
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 19
The Login sequence
Login id and password.
The /etc/passwd file.
The /etc/profile and .profile file.
Shell prompt.
Environment variables
– The $HOME, $PATH, $PS1, $TERM
and other variables.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 20
The other system settings
The $PATH variable.
Aliases.
The stty command.
The [ctrl-c] , [ctrl-d] and other keys.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 21
How your command is
interpreted ?
LOGIN
SHELL ASKS FOR A COMMAND
USER TYPES COMMAND
SHELL EXECUTES
UTILITY TO CARRY
OUT COMMAND
USER INTERACTS WITH
UTILITY
SHELL PROMPTS FOR
NEXT COMMAND
USER TYPES CONTROL-D
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 22
LOGOUT
UNIX file System
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 23
UNIX file system
A hierarchical structure.
Consistent treatment of file data.
Treatment of peripheral devices as files.
Information of files is stored in inode-
block.
Unix keeps track of files using inode
numbers.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 24
A UNIX File System
LEGEND
directory root
........
To other directories
file
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 25
Directory organization in UNIX
/ (root)
bin dev etc tmp lib usr
sh ls tty lp
passwd terminfo user1
user2
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 26
Directory organization in UNIX
/dev
– Where special files are kept .
/bin
– Executable system utilities, like sh, cp, rm .
/etc
– System configuration files and databases.
/lib
– Operating system and programming libraries .
/tmp
– System scratch files (all users can write here)
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 27
Inodes and linking
A directory
FILENAME INODE Another directory
NUM
FILENAME INODE
file1 0221 NUM
file2 0412 name1 0221
file3 0981
• File1 and name1 are links
with same inode numbers
Inode
block #0412 #0221
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 28
The absolute path and relative paths
The absolute path
– The entire pathname starting from root(/).
– Example : /home/trng01/file1.
The relative path
– The path relative to your present working
directory.
– .. represents parent directory.
– . represents present working directory itself.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 29
File and device independence
TAPES
DISK
FILES
UNIX
FILE PRINTERS
UNIX UTILITIES SYSTEM
TERMINALS
COMMUNICA-
TIONS LINES
PIPES
USER PROGRAMS
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 30
Processes in UNIX
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 31
Process in UNIX.
A process is a program in execution.
Each process is allocated a process-
identifier or PID.
In general , each process is started by
another, known as parent process.
When system starts, it runs a single
program, the process number one
called init ( PID = 1).
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 32
Process related commands
The ps command.
– The info of all the processes in system.
The background processing.
– fg command
The who commnad
– The info of all the users logged into the system.
– whoami command.
The kill command.
– kill –[signal] [PID]
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 33
UNIX command line structure
A command is a program that tells UNIX
system to do something.
It has the form : command [options]
[arguments]
Option is generally preceded by hyphen(-)
– More than one option can be strung together.
– e.g. : ls -alR
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 34
Getting help
The man command
– man [options] command_name.
The – help
– command_name – help.
The whatis command.
– whatis command_name.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 35
I/O Redirection
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 36
I/O Redirection
Processes have access to 3 opened files
0.standard input.
1.standard output.
2.standard error.
These files can be redirected
independently.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 37
I/O Redirection (Contd .)
Examples:
$ ls > myfiles
output from ls command goes to the file
"myfiles"
$ mail sam < letter
contents of file letter is mailed to user sam
$ nroff < docl > docl.out 2> errors.
standard input and output and error redirected
simultaneously
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 38
A simple demo
How to create a simple file which has all
the filenames of current directory ?
$ ls > temp
Can I write into the other terminal using
the same command ?
The /dev directory has terminal files.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 39
PIPES -- a powerful feature.
A data-connection between two or more
commands where in the output of one
command acts as an input to next
command.
Usage :
– Command1 | Command2.
– Command1 | Command2 | Command3.
Saves time and disk-space.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 40
Pipes
process
A single process :
input output
The use of Pipe : Output 1 Input 2
pipe
Process1 Process
2
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 41
Pipe
Motivated example
$ grep “pattern” file1 file2 > junk
$ wc -l junk
$ grep “pattern” file1 file2 | wc -1
What’s the difference ?
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 42
Commands as hands – on
File related commands
– cat , cp , rm , mv
Directory commands
– pwd, mkdir, cd, ls, rmdir
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 43
The ln command :
Creates a link for the file.
– ln [file1] [file2]
The second column of ls –l is link-count.
To delete a file with many links, you
need to delete all the links to that file.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 44
File Security
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 45
File Security
Permissions for a file
read (r) , write (w) and execute (x)
Classes of users
Owner
Group
Others
Nine bit field
Composite permission for each file.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 46
Setting file permissions
Method of calculating file permissions
Owner Group Others
r w x r w x r w x
4 4 4
2 2 2
1 1 1
Digit Digit Digit
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 47
Setting file permissions
(Contd.)
Set permission rwxr-xr-- on a <file>
Owner Group Others
r w x r w x r w x
4 4 4
2 - -
1 1 -
7 5 4
$ chmod 754 <file>
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 48
Setting file permissions...
Symbolically setting file permissions
chmod [who] [+ - =] [permission] <file>
who : u, g or o
+ adds , - removes and , = assigns
specified mode and removes others if
any for u/g/o
permission : r or w or x
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 49
The umask command
System wide default permission for a file
– rw-rw-rw (666)
umask (User file creation mask)
– Sets default permissions for a newly created
file and directory.
$ umask
022
$ umask -S
– u=rwx,g=rx,o=rx
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 51
Default File Permissions
Calculate system default file
permissions
6 6 6 - System wide default permissions
0 2 2 - Denial ‘mask’ set by UMASK
6 4 4 - Resultant permissions that will be set on all
files created (-rw-r—r--)
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 52
Default Directory Permissions
System wide default permissions for a
directory: rwxrwxrwx (777)
UMASK
Default permissions for a directory:
7 7 7 - System wide default permissions
0 2 2 - Denial ‘mask’ set by UMASK
-----------
7 5 5 - Resultant permissions that will be
set on all files created (drwxr-xr-x)
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 53
Changing Owner and Group
Change the owner of a file (or directory)
$ chown newowner textfile
Change group to which a file belongs:
$ chgrp newgroup textfile
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 54
Unix On-line Tutor
for you !
Developed by E&R,
Infosys Technologies Ltd.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 55
Today you have to do …
Login
Check home dir/ working dir
Change Password
Pathname and setting variables
Simple commands and man help.
Invoke Tutor : $ tut
After the session, type exit to come out.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 56
Regarding tutor…
Recording the trails and logs of
– Topics covered.
– Time spent.
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 57
Exercise
Submission : Tomorrow
Run and see the usage and meaning of
following commands and write down
with an example
cat, cd , chmod, comm, cp, date, diff, du,
echo, file, grep, head, kill, less, login, ln,
ls, mail, man, mesg, mkdir, more, mv,
passwd, ps, pwd, rm, rmdir, sort, stty,
tail, tee, tr, tty, wc, who, write
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 58
UNIX
End of day 1
ER/CORP/CRS/OS04/003-1 Ver 2.0 Page 59