Unix Module 1
Unix Module 1
UNIT 1
Introduction
The Operating system
Operating system allocates memory for the programs and loads the program to
the allocated memory.
Operating system loads the CPU registers with control information related to
the program.
The instructions provided in the program are executed by the CPU. The
operating system keeps track of the instruction that was last executed. This enables
it to resume a program if it had to be taken out of the CPU before it completed
execution.
If program need to access the hardware, it makes a call to the operating system
rather attempt to do the job itself.
Eg:-If program needs to read a file on disk, the operating system directs
the disk controller to open the file and make the data available to the program.
After the program has completed execution the operating system cleans up
the memory and registers and makes them available for the next program.
Page 1
1.1 The UNIX operating system:
The main concept in the unix architecture is the division of labor between two
agencies the KERNEL and SHELL. The kernel interacts with hardware and shell interacts
with user.
The kernel is the core of the operating system- a collection of routines mostly written
in C. It is loaded into memory when the system is booted and communicates directly with
the hardware. User applications (programs) that need to access the hardware, uses the
services of kernel. These programs access the kernel through a set of function called system
calls.
Apart from providing support to user programs, the kernel also performs other tasks like
managing system’s memory, scheduling processes, decides their priorit ies etc. So the
kernel is often called the operating system- a program’s gateway to the computer’s resource.
The shell is the command interpreter, it translates command into action. It is the
interface between user and kernel. System contains only one kernel, but there may be
several shells.
When user enters a command through the keyboard the shell thoroughly examines
keyboard input for special characters, if it finds any, it rebuilds simplified command line
and finally communicates with kernel to see that the command is executed.For eg,consider a
echo command which has lots of spaces between the arguments:
Eg: $echo Sun solaris
In the above example shell first rebuilds the command line i.e. it will compress all extra
spaces, then it will produces output.
Sun solaris
Multi-user system:
Unix is a multi-user system i.e. multiple user can use the system at a time,
resources are shared between all users. In unix systems, computer breaks up a unit of
time into several segments. So at any point in time, the machine will be doing the
job of a single user. The moment the allocated time expires, the previous job will be
preempted and next user’s job is taken up. This process goes on until clock has
turned full circle and the first user’s job is taken up once again.Unix is a multi-
programming system, it permits multiple programs to run. This can happen in two
ways:
Multiple user can run separate jobs.
A single user can also run multiple jobs.
Multi-tasking system:
A single user can run multiple tasks concurrently, in multitasking
environment a user sees one job running in the foreground, the rest running in the
background. It is possible to switch the job between background and foreground,
suspend or even
terminate them.
Here ‘*’ is the special character, which matches the filename, which
starts with ‘chap’.
Programming facility:
Unix shell is also a programming language. It has control structures,
variables, loops that establish it as a powerful programming language. This features
can be used to design a shell scripts.
Documentation:
Unix contains ‘man’ pages for each command, it contains references for
commands and their configuration files.Apart from man page, we can even get the
command information in internet.there are several newsgroups on unix where we
can fire our queries to get the solution to a problem.
1.5 Unix Environments
Unix can be used in 3 different environments:
1) Personal Environment
2) Time sharing environment
3) Client-server environment
Personal environment : Even though unix is a multi user operating system ,it can
be installed in personal computers. This is called as personal environment.
Time sharing environment :
Employees in large companies often work in so called time sharing environment.In a time
sharing environment many users are connected to one or more computers.Their terminals
are often nonprogrammable.also in a time sharing environment the output devices and
storage devices are shared by all users .A typical time sharing environment has been shown
shown in above figure.
In time sharing environment,all of the computing must be done by the central
computer.centeral computer has many duties.It must control the shared resources .I t must
manage the shared data and printing and it must also do the printing. All the workload will
be given to central computer.Hence the system becomes slow.
Client-server Environment
server
clients
The client –server environment
A client server computing environment splits the computing function between a
central computer called server and users computers.In client –server environment users
microcomputers or workstations are called as clients.The client is a one which requests for
service. The central computer which is a powerful microcomputer,minicomputer or
supercomputer is called a server.Server is a one serves the request of a client.The users are
given personal computers or workstations so that some of the computations can be moved
off to clients.Here workload will be shared between server and clients and hence the system
becomes fast.
Unix Structure:
Note: For Diagram refer the Unix Architecture.
The main components of the Unix structure
are:
1. Kernel.
2. Shell.
3. Application.
1. Kernel:
The kernel provides a bridge between the hardware and the user. It is a software
application that is central to the operating system. The kernel handles the
files, memory, devices, processes and the network for the operating system. It is the
responsibility of the kernel to make sure all the system and user tasks are performed
correctly.
2. Shell:
The program between the user and the kernel is known as the shell. It translates the
many commands that are typed into the terminal session. These commands are
known as the shell script. There are two major types of shells in Unix. These are
Bourne shell and C Shell. The Bourne shell is the default shell for version 7 Unix.
The character $ is the default prompt for the Bourne shell. The C shell is a command
processor that is run in a text window. The character % is the default prompt for the
C shell.
3. Application:
The applications and utility layer in Unix includes the word processors,
graphics programs, database management programs, commands etc. The application
programs provide an application to the end users.
For example, a web browser is used to find information while gaming software
is used to play games. The requests for service and application communication
systems used in an application by a programmer is known as an application program
interface (API).
For ex:
$ ls –x chap*
In the above example ls is the command, –x is a option and chap* is a argument.
The command with its arguments and options is known as command line.
The first word in the command line is called as command, all subsequent words are actually
called as arguments. options are also arguments but given a special name because their list is
predetermined. There should be atleast one space to separate command and arguments.
multiple spaces are also allowed but shell compresses them to form a simplified
command line.
Options:
There is a special type of argument, that is mostly used with a ‘-‘ sign. Such a
arguments are called options.
Eg: $ls –l
-l : long listing showing seven attributes of a file.
-l is an option to ls command. Options are also arguments but their list is
predetermined. Options can normally be combined with single ‘-‘ sign instead of
using
$ls –l –a –t
i.e.
$ls –lat
File name arguments:
Many unix commands use a filename as argument, so the command can take
input from the file. It is even possible to give multiple filename as argument.
Eg:
$ls –l chap1 chap2
The command with its arguments and options is known as command line.
Exceptions:
There is exceptions to the general syntax of commands. There are commands
(pwd) that don’t accept any arguments. Some commands (EX: who) may or may not take
arguments. Some commands like
‘ls’ can run without arguments(ls), with only options (ls -l), with only filenames (ls
chap1 chap2) or can be run with options and filenames (ls –l chap1 chap2).
1.8 Understanding some basic commands
echo: Displaying a message:
echo command can be used to
To display a message (like echo “cseise”)
To evaluate shell variables (echo $PAGER)
Originally echo was an external command, but now all shells have echo as built in
command. We can see escape sequence with echo, but we should use –e option.
Some escape sequences are:
\a Bell
\b Backspace
\c No newline
\f Form feed
\n New line
\r Carriage return
\t Tab
\v Vertical tab
\\ Backslash
\0n ASCII character represented octal value n
echo interprets number octal when it is preceded by \0.
Eg: ASCII value of (ctrl-g) is 7, which results in sounding of a beep, we can use
this value as an argument to echo but only after preceding it with \0
$echo ‘\07’
--beep heard- -
Eg:
$printf “Value of 255 is %o in octal and %x in hexadecimal\n” 255 255
Output: Value of 255 is 377 in octal and ff in hexadecimal.
$date
Tue Aug 28 1:58:03 IST 2018
Date displays 6 fields of
output:
1. day 2.Month name 3. Date 4.time 5.time zone 6.year
To extract individual fields we can use following options with date
Options with date:
+%a – day
+%h – Month name
+%d – date
+%T –time
+%Z- Time Zone
+%Y-year
+%H-Hour
+%M- Minute
+%S - Second
Ex:
$ date +%h
Aug
$date +”%h %d “
Aug 28
ls: listing files:
It is possible to list the names of the files in the directory using ls command.
Eg: $ls
README
Chap01
Chap02
Chap03
Chap01
Chap02
Chap03
$ ls a.txt
The above command searches for file a.txt and if it is present it displays it otherwise displays
the message “file not found”.
Options used with ls
-a : Displays all files including hidden files,current directory(.) and
parent directory(..)
-d dirname : Searches for directory with name dirname,if found displays it,otherwise
produces a
message directory not found.
-i : displays file with inode number
-l : displays all files with seven attributes such as permissions,links,owner name
,group
name,size in bytes,date and time ,file name.
-r : displays files in reverse alphabetical order
-R : Displays recursive list of files
-t : Sorts file based on last modified time
-u : sorts files based on last access time
-x : multi columnar output
Unix has special symbols called meta-characters for creating and storing information in files.
‘>’ symbol can be used to save the information in the file.
6 6 42 list.txt
Output shows that the file ‘list’ contains 6lines, 6words and 42 characters.
It is possible to give output of one command as the input to the another command ‘|’
(pipe) it connects two commands to create
Unix maintains an account of all users, who are logged on to the system. ‘who’ command
displays listing
of these users.
$who
abc pts/1 Jan 1 20:25 (:0)
(heavens.co
xyz pts/10 Jan 1 14:49 m)
First column shows the username, second shows device name of user’s terminal, third,
fourth,
fifth shows
date and time of logging in. User can login remotely to a unix system. Last column shows
the machine name from where user logged in. (:0) indicates user logged in from his
own terminal.
To display the
-H : header.
-u : detailed list
First column shows name of the user, second shows terminal name, 3 rd,4th,5th shows date
and time. 6th column shows system’s IDLE timing, i.e. from how long system is idle.
A ‘.’ Against xyz shows that activity has occurred in the last minute before the command
was invoked.
‘abc’ seems to idling for the last 48minutes. The PID is the process ID, a number that
uniquely identifies a process. We can use the argument ‘am’’i’ with who to know who
logged in the system.
Eg: $who am i
abc pts/1 Jan 13 07:51 (:0)
passwd: changing password:
Eg: $passwd
Passwd: changing password for xyz
Enter login password: ******
New password: ***********
Re-enter new password: ***********
Passwd (SYSTEM) :passwd successfully changed for xyz
When user enters a password, the string is encrypted by the system. Encryption
generates string of random characters and stores in the file /etc/shadow
Syntax:
$cal [[month] year]
January 2013
Su Mo Tu We ThFr 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
We can use cal with pager more or
less.
Eg:
$ (cal 2003; cal 2006; cal 2007) | more
We can use cal command with
arguments. Eg:
$cal 03 2006
Displays calendar of march 2006.
When ‘cal’ is used with arguments month is optional but year is mandatory. A
single argument to cal is interpreted as year.
Eg: $cal 2003
Displays the calendar of 2003
Combining commands:
Unix allows to specify more than one command in the command line. Each
command has to be separated from the other by a ; (semi-colon).
Eg: $ls
ls is /bin/ls
‘ls’ command having an independent existence in the /bin directory (or /usr/bin). So
‘ls’ is a external command.
Internal commands doesn’t have independent existence, they are shell built -
Shell is an external command with difference, i.e. shell possesses its own set
of internal commands. If a command exists both as an internal command of the shell as well
as an external one, the shell will give top priority to its internal command.
Eg: echo command, which is also found in /bin directory but rarely ever executed,
because the shell makes sure that the internal echo command takes precedence over the
external.
Command arguments can take the form of an expression (in grep), a set
of instructions (in sed), or a program (in awk and perl).
1. 10 The type command: knowing the type of a command and locating it.
The type command is used to describe how its argument would be translated if used
as commands. It is also used to find out whether it is built-in or external binary file.
Synatx:
type [options] command names
Type command options:
-a: This option is used to find out whether it is an alias, keyword or function and it
also displays the path ofan executable. If available.
-t: This option will display a single word as an output. alias – if command is a shell
alias
1. keyword – if command is a shell reserved word
2. builtin – if command is a shell builtin
3. function – if command is a shell function
4. file – if command is a disk file
Ex: type -t pwd
type -t cp
type -t ls
type -t while
Login : root
Password : ******[Enter}
#:
The prompt of root is #,unlike the $ or % used by nonprivileged users.Once you login as
root,
you are placed in roots home directory.Depending on the system this directory could be /
or
/root.roots PATH list is also different from the one used by other
users:
Any user can acquire superuser status with the su command if he/she knows the
root password. For example ,the user Juliet becomes a superuser in this way :
$ su
Password: *********
# pwd
/home/Juliet
Though the current directory doesn’t change ,the # prompt indicates that Juliet now
has powers of a superuser.To be in root’s home directory on superuser login use su –l.
Su – henry
This sequence executes henry’s profile and temporarily creates henry’s environment.This
mode can be terminated by hitting [ctrl-d] or using exit.
UNIX FILES
Introduction:
Files are the building blocks of any operating system. When you execute a command
in
UNIX, the UNIX kernel fetches the corresponding executable file from a file system, loads
its instruction text to memory, and creates a process to execute the command on your behalf.
In the course of execution, a process may read from or write to files. All these operations
involve files. Thus, the design of an operating system always begins with an efficient file
management system.
Naming Files:
A filename can consists of upto 255 characters. Files may or may not have extensions,
and can cosists of practically any ASCII character except the / and the NULL
character(ASCII value 0). We are permitted to use control characters or other unprintable
characters in a filename. The following are the valid filenames in UNIX:
.last_time
list.
^V^B^D-++bcd
-{}[]
@#$%*abcd
a.b.c.d.e
The third filename contains three control characters. These characters shoud be definitely be
avoided in framing filenames. Moreover, since the UNIX system has a special treatment for
characters like $, `, ?, *, & among others, it is recommended that only the following
characters be used in filenames.
2. Binary File:
A binary file, on the other hand, contains both printable and unprintable
characters that cover the entire ASCII range (0 to 255). Most UNIX commands are
binary files, and the object code and executables that you produce by compiling C programs
are also binary files. Picture, sound and video files are binary files as well. Displaying such
files with a simple cat command produces unreadable output and may even disturb your
terminal's settings.
2. Directory File:
A directory contains no data, but keeps some details of the files and
subdirectories that it contains The UNIX file system is organized with a number of
directories and subdirectories, and you can also create them as and when you need. You
often need to do that
to group a set of files pertaining to a specific application. This allows two or more files in
separate directories to have the same filename.
A directory file contains an entry for every file and subdirectory that it houses. If you have
20 files in a directory, there will be 20 entries in the directory Each entry has two
components: .
The filename
A unique identification number for the file or directory (called the inode
number),
If a directory bar contains an entry for a file foo, we commonly (and loosely) say that the
directory bar contains the file foo Though we'll often be using the phrase "contains the
file
rather than "contains the filename, you must not interpret the statement literally A directory
contains the filename and not the file's contents.
3. Device File:
You'll also be printing files, installing software from CD-ROMs or backing up files
to tape. All of these activities are performed by reading or writing the file representing
the
device. For instance, when you restore files from tape you read the file associated with
the tape drive. It is advantageous to treat devices as files as some of the commands used to
access
an ordinary file also work with device
files.
Device file names are generally found inside a single directory structure, /dev. A device
file is indeed special, it's not really a stream of characters. In fact, it doesn't contain
anything at all.
Files in Unix System are organized into multi-level hierarchy structure known as a
directory tree. At the very top of the file system is a directory called “root” which
is represented by a “/”. All other files are “descendants” of root.
Directories or Files and their description:
/ : The slash / character alone denotes the root of the filesystem tree.
/bin : Stands for “binaries” and contains certain fundamental utilities, such as ls or
cp, which are generally needed by all users.
/boot : Contains all the files that are required for successful booting process.
/dev : Stands for “devices”. Contains file representations of peripheral devices and
pseudo-devices.
/etc : Contains system-wide configuration files and system databases. Originally
also contained “dangerous maintenance utilities” such as init,but these have typically
been moved to /sbin or elsewhere.
/home : Contains the home directories for the users.
/lib : Contains system libraries, and some critical files such as kernel modules or
device drivers.
/media : Default mount point for removable devices, such as USB sticks,
media players, etc.
/mnt : Stands for “mount”. Contains filesystem mount points. These are used,
for example, if the system uses multiple hard disks or hard disk partitions. It is also
often used for remote (network) filesystems, CD-ROM/DVD drives, and so on.
/proc : procfs virtual filesystem showing information about processes as files.
/root : The home directory for the superuser “root” – that is, the system administrator.
This account’s home directory is usually on the initial filesystem, and hence not
in
/home (which may be a mount point for another filesystem) in case specific
maintenance needs to be performed, during which other filesystems are not available.
Such a case could occur, for example, if a hard disk drive suffers physical failures and
cannot be properly mounted.
/tmp : A place for temporary files. Many systems clear this directory upon startup;
it might have tmpfs mounted atop it, in which case its contents do not survive a
reboot, or it might be explicitly cleared by a startup script at boot time.
/usr : Originally the directory holding user home directories,its use has changed. It now
holds executables, libraries, and shared resources that are not system critical, like the
X Window System, KDE, Perl, etc. However, on some Unix systems, some user
accounts may still have a home directory that is a direct subdirectory of /usr, such as
the default as in Minix. (on modern systems, these user accounts are often related to
server or system use, and not directly used by a person).
/usr/bin : This directory stores all binary programs distributed with the
operating system not residing in /bin, /sbin or (rarely) /etc.
/usr/include : Stores the development headers used throughout the system. Header
files are mostly used by the #include directive in C/C++ programming language.
/usr/lib : Stores the required libraries and data files for programs stored within /usr
or elsewhere.
/var : A short for “variable.” A place for files that may change often – especially
in size, for example e-mail sent to users on the system, or process-ID lock files.
/var/log : Contains system log files.
/var/mail : The place where all the incoming mails are stored. Users (other than root)
can access their own mail only. Often, this directory is a symbolic link to
/var/spool/mail.
/var/spool : Spool directory. Contains print jobs, mail spools and other queued tasks.
/var/tmp : A place for temporary files which should be preserved between
system reboots.
Hidden Files:
To show all the hidden files in the directory, use ‘-a option’. Hidden files in
Unix starts with ‘.’ in its file name.It will show all the files including the ‘.’ (current
directory) and
‘..’ (parent directory).
ls –a
ls –A: To show the hidden files, but not the ‘.’ (current directory) and ‘..’ (parent directory).
Directory Commands:
1. pwd: Checking your current directory
User can move around from one directory to another, but at any point of time,
if user wants to find out in which directory he is present then user can use Print
Working Directory(pwd) command.
Ex: pwd
/home/kumar/SantoshReddyP
cat options:
o Displaying Nonprinting Characters (-v)
cat without any option it will display text files. Nonprinting ASCII
characters can be displayed with –v option.
o Numbering Lines (-n)
-n option numbers lines. This numbering option helps programmer
in debugging programs.
rm options
Interactive Deletion (-i) : Ask the user confirmation before removing each file:
$ rm -i ch1 ch2 rm: remove ch1 (yes/no)? ? y rm: remove ch1 (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 * #Works as rmdir
It deletes all files in the current directory and all its subdirectories.
-c character option
Now it shows the printable characters and its corresponding ASCII
octal representation
$ od –bc file