Unix Programming (Code:18CS56) : Presented by
Unix Programming (Code:18CS56) : Presented by
Presented by:
VENKATESH
Senior Associate Professor
Department of Computer Science and Engineering
Alva’s Institute of Engineering and Technology, Moodbidri
Module-1 SYLLABUS
•Introduction:
•Unix files:
MODULE-1 INTRODUCTION SYLLABUS
• Unix Components/Architecture. Features of Unix. The UNIX
Environment and UNIX Structure, Posix and Single Unix
specification. General features of Unix commands/
command structure. Command arguments and options.
Basic Unix commands such as echo, printf, ls, who,
date,passwd, cal, Combining commands. Meaning of
Internal and external commands. The type command:
knowing the type of a command and locating it. The root
login. Becoming the super user: su command.
MODULE-1 -- UNIX FILES SYLLABUS
• Naming files. Basic file types/categories. Organization of
files. Hidden files. Standard directories. Parent child
relationship. The home directory and the HOME variable.
Reaching required files- the PATH variable, manipulating the
PATH, Relative and absolute pathnames. Directory
commands – pwd, cd, mkdir, rmdir commands. The dot (.)
and double dots (..) notations to represent present and
parent directories and their usage in relative path names.
File related commands – cat, mv, rm, cp, wc and od
Module-2 SYLLABUS
• File attributes and permissions:
• The shells interpretive cycle:
• Connecting commands:
• Shell programming:
File attributes and permissions:
• The ls command with options. Changing file permissions:
the relative and absolute permissions changing methods.
Recursively changing file permissions. Directory
permissions.
The shells interpretive cycle:
Connecting commands:
Shell programming:
Textbooks:
• 1) Sumitabha Das., Unix Concepts and Applications., 4thEdition.,
Tata McGraw Hill ( Chapter 1,2,3,4,5,6,8,13,14)
• 2) W. Richard Stevens: Advanced Programming in the UNIX
Environment,2nd Edition, Pearson Education, 2005
(Chapter3,7,8,10,13,15)
• 3) Unix System Programming Using C++ - Terrence Chan, PHI,
1999. ( Chapter 7,8,9,10)
Reference Books:
• 1. M.G. Venkatesh Murthy: UNIX & Shell Programming,
Pearson Education.
• 2. Richard Blum , Christine Bresnahan : Linux Command Line
and Shell Scripting Bible, 2nd Edition, Wiley,2014.
Module-1 (8hrs.) Syllabus
• Introduction:
• Unix files:
Module-1 (8hrs.) Syllabus -- Introduction
• Unix Components/Architecture. Features of Unix. The UNIX
Environment and UNIX Structure, Posix and Single Unix
specification. General features of Unix commands / command
structure. Command arguments and options.
• Basic Unix commands such as echo, printf, ls, who, date, passwd,
cal, Combining commands. Meaning of Internal and external
commands.
• The type command: knowing the type of a command and
locating it. The root login.
• Becoming the super user: su command.
Module-1 (8hrs.) Syllabus -- Unix files
• Naming files. Basic file types/categories. Organization of files.
Hidden files. Standard directories. Parent child relationship. The
home directory and the HOME variable. Reaching required files-
the PATH variable, manipulating the PATH, Relative and absolute
pathnames. Directory commands – pwd, cd, mkdir, rmdir
commands. The dot (.) and double dots (..) notations to represent
present and parent directories and their usage in relative path
names. File related commands – cat, mv, rm, cp, wc and od
commands.
Module-1 (8hrs.)
• What is Unix?
• A history of Unix.
• Architecture(Components) of Unix.
• Features of Unix.
What is Unix?
• OS is a set of programs that act as a link between the user
and the computer.
• Unix is command user interface O.S
• It can be able to work on any type of computers.
• Unix O.S is a multiuser and multitasking O.S.
• Multiuser: Several people can use a Unix computer at the
same time.
• Multitasking: A user can also run multiple programs at the
same time.
A history of Unix
• In 1969, at AT&T popularly known as Bell Labs started
as a project, a team lead by Ken Thompson and
Dennis Ritchie.
• This project name is MULTICS
• It is the first official O.S in the s/w industries.
• It is modern O.S’s origin.
• Based on this o.s now we are enjoying all modern
O.S’s.
Contd…
• Drawback: MULTICS maximum support 2 users and
no networking support.
• In 1971 to recover this drawback a new project called
UNICS(developed on low level language or Assembly
level language or symbolic language).
• In 1972, Dennis Ritchie launched C language in the IT
industry.
Contd…
• In 1973, a group of members lead by Ken Thompson, Dennis
Ritchie along with other members they re-implemented
UNICS on C language and renamed as UNIX.
• There are various Unix variants available in the Market =>
Solaris Unix, AIX, HP Unix and BSD are a few examples.
• Linux is also a flavor of Unix which is freely available.
The Unix Architecture
/
etc
shadow
Basic Unix commands
7) cal : THE CALENDAR
cal command can be used to see the calendar of any specific
month or a complete year.
Syntax:
cal [ [ month] year ]
Everything within the rectangular box is optional. So cal can be
used without any arguments, in which case it displays the
calendar of the current month.
Example
$cal
September 2020
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Cont’d…
The syntax show that cal can be used with arguments, the
month is optional, but year is not.
To see the calendar of month August 2020, we need to
use two arguments as shown in the next slide page:
Cont’d…
$cal 08 2020
August 2020
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
ROOT: THE SYSTEM ADMINISTRATOR’S LOGIN
The unix system provides a special login name for the exclusive
use of the administrator it is called root.
This account doesn’t need to be separately created but comes
with every system. Its password is generally set at the time of
installation of the system and has to be used on logging in.
The job of system administration involves the management of
the entire system- ranging from maintaining user accounts,
security and managing disk space to performing backups.
Cont’d…
1. Becoming the super user at login time:
eg) Login: root
Password: ********* [Enter]
#-
The prompt of root is #
Once you login as a root you are placed in root’s home directory.
Depending on the system, this directory could be / or /root.
Administrative commands are resident in /sbin and /usr/sbin in
modern systems and in older system it resides in /etc.
Roots PATH list includes detailed path:
For example: /sbin:/bin:/usr/sbin:/usr/bin:/usr/dt/bin
Cont’d…
2. Becoming the super user using su command .
Any user can acquire superuser status with the su command if
they know the root password.
Eg) $ su
Password: ******** #Password of root user
#_
FLEXIBILITY OF COMMAND USAGE
$ rm -i ch1 ch2
rm : remove ch1 (yes/no)? ? y [Enter ]
rm : remove ch2 (yes/no)? ? n [Enter ]
A ‘y‘ removes the file (ch1) any other response like n or any
other key leave the file undeleted
Recursive deletion (-r or -R): It performs a recursive search for all
directories and files within these subdirectories. At each stage it
deletes everything it finds.
$rm -r *
It deletes all files in the current directory and all its subdirectories.
4) cp : COPYING A File
Syntax: cp [OPTION] source destination
Example: Copies the contents from file1 to file2 and
contents of file1 is retained.
$cp file1 file2
If the destination file (file2) doesn‘t exist, it will first be
created before copying takes place. If not it will simply be
overwritten without any warning from the system.
cp options
Interactive Copying(-i) : The –i option warns the user before
overwriting the destination file, If unit1 exists, cp prompts
for response.
$cp -i ch1 unit1
cp: overwrite unit1 (yes/no)? Y
A ‘y’ at this prompt overwrites the file, any other response
leaves it uncopied.
5) wc: COUNTING LINES, WORDS AND CHARACTERS
The syntax of wc command as shown below:
$wc [options] filenames
wc command performs Word counting including counting of lines and
characters in a specified file. It takes one or more filename as
arguments and displays a four columnar output.
$ wc ofile
4 20 97 ofile
Line: any group of characters not containing a newline.
Word: group of characters not containing a space, tab or newline.
Character: smallest unit of information, and includes a space, tab and
newline.
wc options
wc –l : counts only number of lines.
wc –w: counts only number of words.
wc –c: counts only number of characters.
For multiple filenames, wc produces a line for each file, as well as
a total count.
$wc -c ofile file
97 ofile
15 file
112 total
6) od: DISPLAYING DATA IN OCTAL
od command displays the contents of executable files in a
ASCII octal value.
Ex) $cat > ofile
this file is an example for od command
^d used as an interrupt key
od options:
-b option displays this value for each character separately.
Each line displays 16 bytes of data in octal, preceded by the offset in
the file of the first byte in the line.
$od –b ofile
0000000 164 150 151 163 040 146 151 154 145 040 151 163 040 141 156 040
0000020 145 170 141 155 160 154 145 040 146 157 162 040 157 144 040 143
0000040 157 155 155 141 156 144 012 136 144 040 165 163 145 144 040 141
0000060 163 040 141 156 040 151 156 164 145 162 162 165 160 164 040 153
0000100 145 171
-c option: it shows the printable characters and its corresponding
ASCII octal representation.
Example:
$od –bc ofile
0000000 164 150 151 163 040 146 151 154 145 040 151 163 040 141 156 040
T h i s f i l e i s a n
0000020 145 170 141 155 160 154 145 040 146 157 162 040 157 144 040 143
e x a m p l e f o r o d c
0000040 157 155 155 141 156 144 012 136 144 040 165 163 145 144 040 141
o m m a n d \n ^ d u s e d a
0000060 163 040 141 156 040 151 156 164 145 162 162 165 160 164 040 153
s a n i n t e r r u p t k
0000100 145 171
e y
END OF
MODULE 1