Unit 1
Unit 1
The UNIX* operating system was designed to let a number of programmers access
the computer at the same time and share its resources. The operating system
coordinates the use of the computer's resources, allowing one person, for example,
to run a spell check program while another creates a document, lets another edit a
document while another creates graphics, and lets another user format a document
-- all at the same time, with each user oblivious to the activities of the others.
The operating system controls all of the commands from all of the keyboards and
all of the data being generated, and permits each user to believe he or she is the
only person working on the computer.
This real-time sharing of resources make UNIX one of the most powerful
operating systems ever. Although UNIX was developed by programmers for
programmers, it provides an environment so powerful and flexible that it is found
in businesses, sciences, academia, and industry. Many telecommunications
switches and transmission systems also are controlled by administration and
maintenance systems based on UNIX.
While initially designed for medium-sized minicomputers, the operating system
was soon moved to larger, more powerful mainframe computers. As personal
computers grew in popularity, versions of UNIX found their way into these boxes,
and a number of companies produce UNIX-based machines for the scientific and
programming communities.
The features that made UNIX a hit from the start is:
• Multitasking capability
• Multiuser capability
• Portability
• UNIX programs
• Library of application software
1
Unit-1: Basic Concepts of Unix Operating System
Multitasking
Many computers do just one thing at a time, as anyone who uses a PC or laptop
can attest. Try logging onto your company's network while opening your browser
while opening a word processing program. Chances are the processor will freeze
for a few seconds while it sorts out the multiple instructions.
UNIX, on the other hand, lets a computer do several things at once, such as
printing out one file while the user edits another file. This is a major feature for
users, since users don't have to wait for one application to end before starting
another one.
Multiusers
The same design that permits multitasking permits multiple users to use the
computer. The computer can take the commands of a number of users --
determined by the design of the computer -- to run programs, access files, and print
documents at the same time.
The computer can't tell the printer to print all the requests at once, but it does
prioritize the requests to keep everything orderly. It also lets several users access
the same document by compartmentalizing the document so that the changes of
one user don't override the changes of another user.
System portability
A major contribution of the UNIX system was its portability, permitting it to move
from one brand of computer to another with a minimum of code changes. At a time
when different computer lines of the same vendor didn't talk to each other -- yet
alone machines of multiple vendors -- that meant a great savings in both hardware
and software upgrades.
It also meant that the operating system could be upgraded without having all the
customer's data inputted again. And new versions of UNIX were backward
compatible with older versions, making it easier for companies to upgrade in an
orderly manner.
UNIX tools
UNIX comes with hundreds of programs that can divided into two classes:
• Integral utilities that are absolutely necessary for the operation of the computer,
such as the command interpreter, and
Tools that aren't necessary for the operation of UNIX but provide the user with
additional capabilities, such as typesetting capabilities and e-mail
Several features of UNIX have made it popular. Some of them are:
Networking
While UNIX was developed to be an interactive, multiuser, multitasking system,
networking is also incorporated into the heart of the operating system. Access to
2
Unit-1: Basic Concepts of Unix Operating System
3
Unit-1: Basic Concepts of Unix Operating System
• The shell, which connects and interprets users' commands, calls programs from
memory, and executes them;
• The tools and applications that offer additional functionality to the operating
system.
The kernel
The heart of the operating system, the kernel controls the hardware and turns part
of the system on and off at the programmer’s command. If you ask the computer to
list (ls) all the files in a directory, the kernel tells the computer to read all the files
in that directory from the disk and display them on your screen.
The shell
There are several types of shell, most notably the command driven Bourne Shell
and the C Shell (no fun intended), and menu-driven shells that make it easier for
beginners to use. Whatever shell is used, its purpose remains the same -- to act as
an interpreter between the user and the computer.
The shell also provides the functionality of "pipes," whereby a number of
commands can be linked together by a user, permitting the output of one program
to become the input to another program.
Tools and applications
There are hundreds of tools available to UNIX users, although some have been
written by third party vendors for specific applications. Typically, tools are
grouped into categories for certain functions, such as word processing, business
applications, or programming.
Features of Shell
It doesn't matter which of the standard shells you choose, because they all have the
same purpose: to provide a user interface to UNIX. To provide this interface, all
the shells offer the same basic characteristics:
• Command-line interpretation
• Reserved words
• Shell meta-characters (wild cards)
• Access to and handling of program commands
• File handling: input/output redirection and pipes
• Maintenance of variables
• Environment control
• Shell programming
Command-Line Interpretation
When you log in, starting a special version of a shell called an interactive shell,
you see a shell prompt, usually in the form of a dollar sign ($), a percent sign (%),
or a pound sign (#). When you type a line of input at a shell prompt, the shell tries
to interpret it. Input to a shell prompt is sometimes called a command line. The
basic format of a command line is command arguments command is an executable
UNIX command, program, utility, or shell program. Arguments are passed to the
executable. Most UNIX utility programs expect arguments to take the following
form: options filenames for example, in the command line.
$ ls -l file1 file2
There are three arguments to ls; the first is an option, and the last two are
filenames. One of the things the shell does for the kernel is to eliminate
unnecessary information. For a computer, one type of unnecessary information is
whitespace; therefore, it is important to know what the shell does when it sees
whitespace. Whitespace consists of space characters, horizontal tabs, and newline
characters. Consider this example:
$ echo part A part B part C
Part A part B part C
Here, the shell has interpreted the command line as the echo command with six
arguments and has removed the whitespace between the arguments. For example,
5
Unit-1: Basic Concepts of Unix Operating System
if you were printing headings for a report and wanted to keep the whitespace, you
would have to enclose the data in quotation marks, as in
$ Echo 'part A part B part C'
Part A part B part C
The single quotation mark prevents the shell from looking inside the quotes. Now
the shell interprets this line as the echo command with a single argument, which
happens to be a string of characters including whitespace.
Reserved Words
All shell versions have words that have special meaning. In shell programming,
words such as do, done, for, and while provide loop control--and if, then, else, and
fi provide conditional control. Each shell version has different reserved word
pertaining to its specific features.
Shell Meta-Character (Wild Cards)
All shell versions have meta-characters, which allow the user to specify filenames.
The following are wild cards: Wild Card
• Matches any portion
• Matches any single character
• Matches a range or list of characters.
Wild cards can be useful when processing a number of specific files. The
following are some examples:
$ls t*
This lists all files starting with t.
$ls test?5. dat
This lists all files starting with test, any single character and ends with 5.dat.
$ls [a-c]*
This lists all files starting with a through c.
$ls [e,m,t]*
This lists all files starting with e, m, or t.
Program Commands
When a command is typed, the shell reads the environment variable $path, which
contains a list of directories containing program files. The shell looks through this
set of directories to find the program file for the command. The shell then passes
the true filename to the kernel.
File Handling: Input/Output Redirection and Pipes
In previous chapters, you learned about standard input and output. Unless
otherwise specified with arguments, most UNIX commands take input from the
terminal keyboard and send output to the terminal monitor. To redirect output to a
file, use the > symbol. For example,
$ls >myfiles
6
Unit-1: Basic Concepts of Unix Operating System
Lists the files in your current directory and places them in a file called myfiles.
Likewise, you can redirect input with the < symbol. For example,
$wc -l <myfiles
Feeds the command wc with input from the file myfiles. Although you could
obtain the same output by having the filename as an argument, the need for input
redirection becomes more apparent in shell programming.
To string the output from one command to the input of the next command, you can
use the | (pipe) symbol. For example,
$ls -s | sort -nr | pg
This lists the files in the current directory with blocksize and then pipes the output
to the sort, which sorts the files in numeric descending order and pipes that output
to the paging command pg for final display on the terminal's monitor. The pipe
command is one of the most useful tools when creating command constructs.
Command Substitution
Command substitution is similar to redirection except that is used to provide
arguments to a command from the output of another. For example,
$grep 'wc -l myfiles' *
takes the number of lines in the file myfiles from the wc command and places the
number as an argument to the grep command to search all files in the current
directory for that number.
Maintenance of Variables
The shell is capable of maintaining variables. Variables are places you can store
data for later use. You assign a value to a variable with an equal (=) sign:
$ LOOKUP=/usr/mydir
Here, the shell establishes LOOKUP as a variable and assigns it the value
/usr/mydir. Later, you can use the value stored in LOOKUP in a command line by
prefacing the variable name with a dollar sign ($). Consider these examples:
$ echo $LOOKUP
/usr/mydir
$ echo LOOKUP
LOOKUP
7
Unit-1: Basic Concepts of Unix Operating System
1.4 Kernel
As already mentioned, a CPU can run in either User Mode or Kernel Mode.
Actually, some CPUs can have more than two execution states. For instance, the
80 × 86 microprocessors have four different execution states. But all standard
UNIX kernels use only Kernel Mode and User Mode.
When a program is executed in User Mode, it cannot directly access the kernel data
structures or the kernel programs. When an application executes in Kernel Mode,
however, these restrictions no longer apply. Each CPU model provides special
instructions to switch from User Mode to Kernel Mode and vice versa. A program
usually executes in User Mode and switches to Kernel Mode only when requesting
a service provided by the kernel. When the kernel has satisfied the program’s
request, it puts the program back in User Mode.
8
Unit-1: Basic Concepts of Unix Operating System
Processes are dynamic entities that usually have a limited life span within the
system. The task of creating, eliminating, and synchronizing the existing processes
is delegated to a group of routines in the kernel.
The kernel itself is not a process but a process manager. The process/kernel model
assumes that processes that require a kernel service use specific programming
constructs called system calls. Each system call sets up the group of parameters
that identifies the process request and then executes the hardware-dependent CPU
instruction to switch from User Mode to Kernel Mode.
Besides user processes, Unix systems include a few privileged processes called
kernel threads with the following characteristics:
On a uniprocessor system, only one process is running at a time and it may run
either in User or in Kernel Mode. If it runs in Kernel Mode, the processor is
executing some kernel routine. Figure illustrates examples of transitions between
User and Kernel Mode. Process 1 in User Mode issues a system call, after which
the process switches to Kernel Mode and the system call is serviced. Process 1 then
resumes execution in User Mode until a timer interrupt occurs and the scheduler is
activated in Kernel Mode. A process switch takes place and Process 2 starts its
execution in User Mode until a hardware device raises an interrupt. As a
consequence of the interrupt, Process 2 switches to Kernel Mode and services the
interrupt.
UNIX kernels do much more than handle system calls; in fact, kernel routines can
be activated in several ways:
9
Unit-1: Basic Concepts of Unix Operating System
10
Unit-1: Basic Concepts of Unix Operating System
The UNIX system is actually more than strictly an operating system. UNIX
includes the traditional operating system components. In addition, a standard
UNIX system includes a set of libraries and a set of applications. Figure shows the
components and layers of UNIX. Sitting above the hardware are two components:
the file system and process control. Next is the set of libraries. On top are the
applications. The user has access to the libraries and to the applications. These two
components are what many users think of as UNIX, because together they
constitute the UNIX interface.
11
Unit-1: Basic Concepts of Unix Operating System
The Kernel
The kernel is a part of the operating system. It interacts directly with the hardware
of the computer through a device that is built into the kernel.
• Memory management
• Controlling access to the computer.
• Maintaining the file system.
• Handling interrupts.
• Handling errors.
• Performing input and output services.
• Allocate the resources of the computer among users.
The Shell
It is a software program and it acts as a mediator between the kernel and the user.
It reads the commands and then interpret them and sends a request to execute a
program. So, the shell is also called a command interpreter.
It contains nearly 100 system calls. System calls tell the kernel to carry out various
tasks for the program, such as
• Opening a file.
• Writing a file.
• Obtaining information about a file.
• Executing programs.
• Terminating a process.
• Changing the priority of processes.
• Getting the time and date.
Hardware
The hardware includes all the parts of a computer including clocks, timers, devices,
parts etc. in the Unix OS Architecture.
12
Unit-1: Basic Concepts of Unix Operating System
The following are a series of Unix commands which will help you use the computers.
They are given in their most basic form and more information will be available from their
on-line manual pages (accessed through the man command described below). Each
13
Unit-1: Basic Concepts of Unix Operating System
command will be given in a generic form, perhaps with an example of an actual usage.
In the examples, the symbol $ will indicate the prompt you see on your screen (which
you would not type in if actually using the command).
3) cd - Changing Directory
The cd command changes the current working directory to the directory specified.
The format ofcd is cd [directory].
If you do not specify directory, cd changes to your home directory.
For example,
$ cd /home/stlawrence/user/newuser
$pwd/home/stlawrence/user/newuser
$ cd Mail
$ pwd
/home/stlawrence/user/newuser/Mail
$cd
$ pwd
/home/stlawrence/user/newuser
$
14
Unit-1: Basic Concepts of Unix Operating System
4) ls Command
• ls -a: list all files including hidden files. These are files that start with “.”.
• ls -A: list all files including hidden files except for “.” Means current
directory and “..” means parent directory.
• ls -R: list all files recursively, descending down the directory tree from the
given path.
• ls -l: list the files in long format i.e., with an index number, owner name,
group name, size, and permissions.
• ls – o: list the files in long format but without the group name.
• ls -g: list the files in long format but without the owner name.
• ls -i: list the files along with their index number.
• ls -s: list the files along with their size.
• ls -t: sort the list by time of modification, with the newest at the top.
• ls -S: sort the list by size, with the largest at the top.
• ls -r: reverse the sorting order.
Examples:
$ ls
List all the files including hidden files in the current directory
$ ls -a
List all the files including hidden files in the current directory
15
Unit-1: Basic Concepts of Unix Operating System
$ ls -al
total 24
List all the files in the current directory in long format, sorted by
modification time, oldest first
$ ls -lrt
total 16
List all the files in the current directory in long format, sorted by size,
smallest first
16
Unit-1: Basic Concepts of Unix Operating System
$ ls -lrS
total 16
$ ls -R
. /dir1:
file3
. /dir2:
17
Unit-1: Basic Concepts of Unix Operating System
For example,
$ ls -F
$ ls -F
The rmdir command will not remove a directory with files in it. –
. For example,
$ls -F
$ rmdir zeta
$ ls -F
18
Unit-1: Basic Concepts of Unix Operating System
-i Inquire before removing a file (“y” to delete, anything else to not delete).
For example,
$ ls
Mail/ prog. f
$ rm prog. f
$ ls
owner is the person who creates files or directories. these files and
directories are created in owner's home directory. owner can change
permission for file or directory.
19
Unit-1: Basic Concepts of Unix Operating System
a=all
other permissions but = is used for absolute permission and it affects other
permissions.
$ls -l b2
-rw-r--r--
$chmod ug+x b2
$chmod g+w b2
20
Unit-1: Basic Concepts of Unix Operating System
-rwxrw-r-x.
$chmod a-x b2
-rw-rw-r--
$chmod u=x b2
above command will assign execute permission to owner but will revoke
every other
---xrw-r--
$chmod go=x b2
above command will assign execute permission to group and others but
every other
permission from group and others will be revoked.now file's permission will
be
21
Unit-1: Basic Concepts of Unix Operating System
---x--x--x
$chmod a=r b2
-r--r--r--
4= read permission
2=write permission
1=execute permission
$chmod 242 b2
above command will assign write permission to owner and others and read
permission
--w-r---w-
$chmod 357 b2
22
Unit-1: Basic Concepts of Unix Operating System
• When cmp is used for comparison between two files, it reports the location
of the first mismatch to the screen if difference is found and if no difference
is found i.e the files compared are identical.
• cmp displays no message and simply returns the prompt if the the files
compared are identical.
Syntax:
cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
cmp Example : As explained that the cmp command reports the byte and line
number if a difference is found. Now let’s find out the same with the help of an
example. Suppose there are two files which you want to compare one is file1.txt
and other is file2.txt :
If the files are not identical: the output of the above command will be :
If the files are identical: you will see something like this on your screen:
1. -b(print-bytes) : If you want cmp displays the differing bytes in the output
when used with -b option.
The values 154 and 151 in the above output are the values for these bytes,
respectively.
Note that in cases like these (where you use -i to skip bytes), the byte at which the
comparison begins is treated as byte number zero.
24
Unit-1: Basic Concepts of Unix Operating System
3. -i [bytes to be skipped from first file] : [bytes to be skipped from second file]
:This option is very much similar to the above -i [bytes to be skipped] option but
with the difference that now it allows us to input the number of bytes we want
to skip from both the files separately.
4. -l option : This option makes the cmp command print byte position and byte
value for all differing bytes.
The first column in the output represents the position (byte number) of differing
bytes. The second column represents the byte value of the differing byte in the first
25
Unit-1: Basic Concepts of Unix Operating System
file, while the third column represents the byte value of the differing byte in the
second file.
5. -s option : This allows you to suppress the output normally produced by cmp
command i.e it compares two files without writing any messages. This gives an
exit value of 0 if the files are identical, a value of 1 if different, or a value of 2 if an
error message occurs.
26
Unit-1: Basic Concepts of Unix Operating System
Suppose you have two lists of people and you are asked to find out the names
available in one and not in the other, or even those common to both. comm is the
command that will help you to achieve this. It requires two sorted files which it
compares line by line.
Before discussing anything further first let’s check out the syntax
of comm command:
Syntax :
$comm [OPTION]... FILE1 FILE2
• As using comm, we are trying to compare two files therefore the syntax
of comm command needs two filenames as arguments.
• With no OPTION used, comm produces three-column output where first
column contains lines unique to FILE1 ,second column contains lines
unique to FILE2 and third and last column contains lines common to both
the files.
• comm command only works right if you are comparing two files which
are already sorted.
Example: Let us suppose there are two sorted files file1.txt and file2.txt and
now we will use comm command to compare these two.
// displaying contents of file1 //
$cat file1.txt
Apaar
Ayush Rajput
Deepak
Hemant
Hemant
Lucky
Pranjal Thakral
The above output contains of three columns where first column is separated
by zero tab and contains names only present in file1.txt ,second
column contains names only present in file2.txt and separated by one tab
and the third column contains names common to both the files and is
separated by two tabs from the beginning of the line.
This is the default pattern of the output produced by comm command when
no option is used .
Note: The options 4 to 8 are rarely used but options 1 to 3 are very useful in
terms of the desired output user wants.
Using comm with options
1. Using -1, -2 and -3 options: The use of these three options can be easily
explained with the help of example:
//suppress first column using -1//
$comm -1 file1.txt file2.txt
Apaar
Hemant
Lucky
Pranjal Thakral
Apaar
Ayush Rajput
Deepak
Hemant
$cat f1.txt
Parnjal
Kartik
$cat f2.txt
Apaar
29
Unit-1: Basic Concepts of Unix Operating System
Kartik
$ ls
a.txt b.txt
$ cat a.txt
Gujarat
Uttar Pradesh
Kolkata
Bihar
Jammu and Kashmir
$ cat b.txt
Tamil Nadu
Gujarat
Andhra Pradesh
Bihar
Uttar Pradesh
Now, applying diff command without any option we get the following output:
$ diff a.txt b.txt
0a1
> Tamil Nadu
2,3c3
< Uttar Pradesh
Andhra Pradesh
5c5
Uttar pradesh
31
Unit-1: Basic Concepts of Unix Operating System
Let’s take a look at what this output means. The first line of the diffoutput will
contain:
• Line numbers corresponding to the first file,
• A special symbol and
• Line numbers corresponding to the second file.
Like in our case, 0a1 which means after lines 0(at the very beginning of file) you
have to add Tamil Nadu to match the second file line number 1. It then tells us
what those lines are in each file preceded by the symbol:
• Lines preceded by a < are lines from the first file.
• Lines preceded by > are lines from the second file.
• Next line contains 2,3c3 which means from line 2 to line 3 in the first file
needs to be changed to match line number 3 in the second file. It then
tells us those lines with the above symbols.
• The three dashes (“— “) merely separate the lines of file 1 and file 2.
As a summary to make both the files identical, first add Tamil Nadu in the first file
at very beginning to match line 1 of second file after that change line 2 and 3 of
first file i.e., Uttar Pradesh and Kolkata with line 3 of second file i.e., Andhra
Pradesh. After that change line 5 of first file i.e., Jammu and Kashmir with line 5
of second file i.e., Uttar Pradesh.
Now let’s see what it looks like when diff tells us that we need to delete a line.
$ cat a.txt
Gujarat
Andhra Pradesh
Telangana
Bihar
Uttar pradesh
$ cat b.txt
Gujarat
Andhra Pradesh
Bihar
Uttar pradesh
32
Unit-1: Basic Concepts of Unix Operating System
Linux system offers two different ways to view the diff command output
i.e., context mode and unified mode.
-c (context): To view differences in context mode, use the -c option.
Let’s try to understand this with example, we have two
files file1.txt and file2.txt:
$ cat file1.txt
cat
mv
comm
cp
$ cat file2.txt
cat
cp
diff
comm
$ cat file2.txt
cat
cp
diff
comm
$ cat file2.txt
DOG
cp
diff
comm
35
Unit-1: Basic Concepts of Unix Operating System
36