0% found this document useful (0 votes)
23 views36 pages

Unit 1

Uploaded by

patelpreet8686
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views36 pages

Unit 1

Uploaded by

patelpreet8686
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Unit-1: Basic Concepts of Unix Operating System

1.1 Features of Unix OS

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

another system uses a standard communications protocol known as Transmission


Control
Protocol/Internet Protocol (TCP/IP).
Organized File System
UNIX has a very organized file and directory system that allows users to organize
and maintain files.
Device Independence
UNIX treats input/output devices like ordinary files. The source or destination for
file
input and output is easily controlled through a UNIX design feature called
redirection.
Utilities
UNIX provides a rich library of utilities that can be used to increase user
productivity.

1.2 System structure of Unix

The UNIX system is functionally organized at three levels:


• The kernel, which schedules tasks and manages storage;

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.

1.3 Shell and it’s features


Bourne shell (sh)
This is the original Unix shell written by Steve Bourne of Bell Labs. It is available
on all UNIX systems.
This shell does not have the interactive facilities provided by modern shells such as
the C shell and Korn shell. You are advised to use another shell which has these
features. The Bourne shell does provide an easy-to-use language with which you
can write shell scripts.
C Shell
The C shell, csh, was written by Bill Joy while a graduate student at University of
California, Berkeley. The language, including the control structures and the
expression grammar, was modeled on C. The C shell also introduced a large
number of features for interactive work, including the history and editing
mechanisms, aliases, directory stacks, tilde notation, cdpath, job control and path
hashing.
4
Unit-1: Basic Concepts of Unix Operating System

Korn shell (ksh)


This shell was written by David Korn of Bell labs. It is now provided as the
standard shell on Unix systems. It provides all the features of the C shell and
Bourne Shell. It has beautiful command history feature. It also supports Aliasing.

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

UNIX kernels provide an execution environment in which applications may run.


Therefore, the kernel must implement a set of services and corresponding
interfaces. Applications use those interfaces and do not usually interact directly
with hardware resources.

The Process/Kernel Model

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:

• They run in Kernel Mode in the kernel address space.


• They do not interact with users, and thus do not require terminal devices.
• They are usually created during system startup and remain alive until the
system is shut down.

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

• A process invokes a system call.


• The CPU executing the process signals an exception, which is an unusual
condition such as an invalid instruction. The kernel handles the exception on
behalf of the process that caused it.
• A peripheral device issues an interrupt signal to the CPU to notify it of an
event such as a request for attention, a status change, or the completion of an
I/O operation. Each interrupt signal is dealt by a kernel program called an
interrupt handler. Since peripheral devices operate asynchronously with
respect to the CPU, interrupts occur at unpredictable times.
• A kernel thread is executed. Since it runs in Kernel Mode, the corresponding
program must be considered part of the kernel.

1.5 Architecture of UNIX OS


The kernel of UNIX is the hub of the operating system: it allocates time and
memory to programs and handles the filestore and communications in response
to system calls.
As an illustration of the way that the shell and the kernel work together,
suppose a user types rm myfile (which has the effect of removing the file
myfile). The shell searches the filestore for the file containing the program rm,
and then requests the kernel, through system calls, to execute the program rm
on myfile. When the process rm myfile has finished running, the shell then
returns the UNIX prompt $ to the user, indicating that it is waiting for further
commands. Amongst the functions performed by the kernel are: managing the
machine's memory and allocating it to each process. Scheduling the work done
by the CPU so that the work of each user is carried out as efficiently as is
possible.
Organizing the transfer of data from one part of the machine to another.
Accepting instructions from the shell and carrying them out. Enforcing the
access permissions that are in force on the file system the shell. The user can
customize his/her own shell, and users can use different shells on the same
machine.
History The shell keeps a list of the commands you have typed in. If you need
to repeat a command, use the cursor keys to scroll up and down the list or type
history for a list of previous commands.
You can use any one of these shells if they are available on your system. And
you can switch between the different shells once you have found out if they are
available.

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.

The main functions of the kernel are,

• 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 kernel is the main part of the Architecture of Unix OS.

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.

Unix Commands and Libraries

12
Unit-1: Basic Concepts of Unix Operating System

This layer of Unix architecture includes user-written applications, using shell


programming languages, and libraries of UNIX.

1.6 Unix Commands


Unix is a multi-tasking, multi-user operating system. This means that on any given
computer, it appears as if many things are happening at once and that there may be
more than one person logged into the computer at once. Regardless of which
machine you log into in the Institute, you will have access to your user files and the
basic commands will be identical. The Unix file system is hierarchical. Every file
is stored in a directory. A directory can contain many files or none at all, and may
also contain other directories called subdirectories. Unix has three types of files:
Normal Files: These are data files which might contain text, source code,
executable files, etc.
Special FilesThese represent physical devices such as terminals and disk drives.
The “device drivers” will translate any references to such files into the hardware
instructions needed to carry out the tasks.
Directories. These contain “pointers” to normal files, special files and other
directories. File names can be as long as you like, unlike in MS-DOS. The parent
directory of all other directories is called the “root” directory and is denoted by /.
Every file on the system can be accessed by tracing a path from this root directory.
.Each user has a “home” directory: this is where you will be located when you
login. You are then free to traverse the directory structure of the Institute and to
add to and change the part of the structure which you own.
Unix commands have the following format:
command [options...] [arguments...]
Where command is the command name, options refer to optional command
modifiers (usually prefixed by a “-“) and arguments are the optional or required
command parameters (often file names). Spaces or tabs are required between
commands and options and between options and each argument.
Commands must be typed in the proper case (nearly always lower case).
Although each option to a command may be prefixed by a “-”, with almost all of
the standard commands you may put the options together. For example, command
-a -b -c -d can often be abbreviated as command -abcd Again, this is almost always
the case - the most notable exceptions. are in the GNU commands and script.

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).

1) man - Accessing On-Line Manual Pages


The man command looks up the manual page for a command.
The format of man is
man [-k] name...
-k Prints a list of all manual pages containing the keyword name.
Use the-k option if you do not know the name of the command or program.
Ex.
$man ls

2) pwd - Print the Working Directory


The pwd command prints the full pathname of your current working directory.
The format of pwd is pwd.
For example,
$ pwd
/home/stlawrence/user/newuser
Above is absolute pathname.

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:

List all non-hidden files in the current directory

$ ls

dir1 dir2 file1 file2

List all the files including hidden files in the current directory

$ ls -a

. .. . hfile dir1 dir2 file1 file2

List all the files including hidden files in the current directory

15
Unit-1: Basic Concepts of Unix Operating System

$ ls -al

total 24

drwxr-xr-x 7 user staff 224 Jun 21 15:04.

drwxrwxrwx 18 user staff 576 Jun 21 15: 02.

-rw-r--r-- 1 user staff 6 Jun 21 15:04. hfile

drwxr-xr-x 3 user staff 96 Jun 21 15:08 dir1

drwxr-xr-x 2 user staff 64 Jun 21 15:04 dir2

-rw-r--r-- 1 user staff 6 Jun 21 15:04 file1

-rw-r--r-- 1 user staff 4 Jun 21 15:08 file2

List all the files in the current directory in long format, sorted by
modification time, oldest first

$ ls -lrt

total 16

-rw-r--r-- 1 user staff 6 Jun 21 15:04 file1

drwxr-xr-x 2 user staff 64 Jun 21 15:04 dir2

-rw-r--r-- 1 user staff 4 Jun 21 15:08 file2

drwxr-xr-x 3 user staff 96 Jun 21 15:08 dir1

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

-rw-r--r-- 1 user staff 4 Jun 21 15:08 file2

-rw-r--r-- 1 user staff 6 Jun 21 15:04 file1

drwxr-xr-x 2 user staff 64 Jun 21 15:04 dir2

drwxr-xr-x 3 user staff 96 Jun 21 15:08 dir1

List all the files recursively from the current directory

$ ls -R

dir1 dir2 file1 file2

. /dir1:

file3

. /dir2:

5) mkdir - Making Directories


The mkdir command makes directories with specified names.

17
Unit-1: Basic Concepts of Unix Operating System

The format of the mkdir command is

Mkdir directory(1) directory(2)... directory(n)

For example,

$ ls -F

Mail/ prog/ zeta.f

$ mkdir thesis zeta

$ ls -F

Mail/ prog/ thesis/ zeta/ zeta. f

6) rmdir - Removing Directories


The rmdir command removes empty directories with specified names. The
format of the rmdir command is

Rmdir directory (1) directory (2) ... directory(n)

The rmdir command will not remove a directory with files in it. –

for this use the rm -r command, which removes directories recursively.


Described later in this section, but be careful!

. For example,

$ls -F

Mail/ prog/ thesis/ zeta/ zeta. f

$ rmdir zeta

$ ls -F

Mail/ prog/ thesis/ zeta. f

18
Unit-1: Basic Concepts of Unix Operating System

7) rm - Removing Files and Directories


The rm command removes files and directories.
Caution: There is no way to reverse this process.
The format of the rm command is rm [-i] [-r] fd(1) fd(2)... fd(n)

where fd(1..n) are files or directories.

-i Inquire before removing a file (“y” to delete, anything else to not delete).

-r Recursively remove a directory and all its contents and subdirectories

(Use with extreme care).

For example,

$ ls

Mail/ prog. f

$ rm prog. f

$ ls

Mail

8) chmod - Changing Access Permissions


unix has been developed in research environment. so users of unix have been
distributed

in 3 categories,owner,group,others. each category has read,write and execute


permission.

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.

for this chmod command is used. following table is used by chmod


command.

19
Unit-1: Basic Concepts of Unix Operating System

category permission type

u=owner r=read permission + assign permission

g=group w=write " - revoke permission

o=others x=execute " =absolute permission

a=all

+,- are used for relative permission.relative permission is permission which


do not affect

other permissions but = is used for absolute permission and it affects other
permissions.

$ls -l b2

-rw-r--r--

above is default permission for file

default permission for owner= read,write

" " " group=read

" " " others=read

$chmod ug+x b2

above command gives execute permission to owner and group

$chmod g+w b2
20
Unit-1: Basic Concepts of Unix Operating System

above command give write permission to group

after issuing above commands file's permission will be

-rwxrw-r-x.

$chmod a-x b2

above command will revoke execute permission from everybody.

now file's permission will be

-rw-rw-r--

$chmod u=x b2

above command will assign execute permission to owner but will revoke
every other

permission from owner.

now file's permission will be

---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

now file's permission will be

-r--r--r--

OCTAL codes can be used for absolute permission.

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

to group. file's permission will be

--w-r---w-

$chmod 357 b2

file's permission will be

owner write and execute (2+1)

22
Unit-1: Basic Concepts of Unix Operating System

group read and execute permission (4+1)

other read, write, execute permission (4+2+1)

these notations are more popular.

Comparison Commands in unix


cmp command in Linux/UNIX is used to compare the two files byte by byte and
helps you to find out whether the two files are identical or not.

• 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]]]

SKIP1 ,SKIP2 & OPTION are optional


and FILE1 & FILE2 refer to the filenames.

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 :

$cmp file1.txt file2.txt

If the files are not identical: the output of the above command will be :

$cmp file1.txt file2.txt


file1.txt file2.txt differ: byte 9, line 2

/*indicating that the first mismatch found in


two files at byte 20 in second line*/
23
Unit-1: Basic Concepts of Unix Operating System

If the files are identical: you will see something like this on your screen:

$cmp file1.txt file2.txt


$_
/*indicating that the files are identical*/

Options for cmp command

1. -b(print-bytes) : If you want cmp displays the differing bytes in the output
when used with -b option.

//...cmp command used with -b option...//

$cmp -b file1.txt file2.txt


file1.txt file2.txt differ: 12 byte, line 2 is 154 l 151 i

/* indicating that the difference is in 12


byte ,which is 'l' in file1.txt and 'i' in file2.txt.*/

The values 154 and 151 in the above output are the values for these bytes,
respectively.

2. -i [bytes-to-be-skipped] : Now, this option when used with cmp command


helps to skip a particular number of initial bytes from both the files and then
after skipping it compares the files. This can be done by specifying the number of
bytes as argument to the -i command line option.

//...cmp command used with -i option...//

$cmp -i 10 file1.txt file2.txt


$_

/*indicating that both files are identical


after 10 bytes skipped from both the files*/

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.

//...cmp command used with -i option...//

$cmp -i 10:12 file1.txt file2.txt


$_

/*indicating that both files are identical


after 10 bytes skipped from first file and
12 bytes skipped from second file*/

4. -l option : This option makes the cmp command print byte position and byte
value for all differing bytes.

//...cmp command used with -l option...//

$cmp -l file1.txt file2.txt


20 12 56
21 124 12
22 150 124
23 151 150
24 163 151
25 40 163
26 146 40
27 150 151
28 12 24
29 124 145
30 157 163

/*indicating that files are different


displaying the position of differing
bytes along with the differing bytes
in both file*/

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.

//...cmp command used with -s option...//

$cmp -s file1.txt file.txt


1

/*indicating files are different without


displaying the differing byte and line*/

6. -n [number of bytes to be compared] option: This option allows you to limit


the number of bytes you want to compare, like if there is only need to compare at
most 25 or 50 bytes.

//...cmp command used with -n option...//

$cmp -n 50 file1.txt file2.txt


$_

/*indicating files are identical for starting


50 bytes*/

8. – -v option: This gives the output information and exits.

9. – -help option: This displays a help message and exits.

comm command in Linux with examples


• Difficulty Level : Medium
• Last Updated : 19 Feb, 2021
comm compare two sorted files line by line and write to standard output; the lines
that are common and the lines that are unique.

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

// displaying contents of file2 //


$cat file2.txt
Apaar
Hemant
Lucky
Pranjal Thakral
Now, run comm command as:
// using comm command for
comparing two files //
$comm file1.txt file2.txt
Apaar
Ayush Rajput
Deepak
27
Unit-1: Basic Concepts of Unix Operating System

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 .

Options for comm command:


1. -1: suppress first column (lines unique to first file).
2. -2: suppress second column (lines unique to second file).
3. -3: suppress third column (lines common to both files).
4. – -check-order: check that the input is correctly sorted, even if all input
lines are pairable.
5. – -no check-order: do not check that the input is correctly sorted.
6. – -output-delimiter=STR: separate columns with string STR
7. – -help: display a help message, and exit.
8. – -version: output version information, and exit.

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

//suppress second column using -2//


$comm -2 file1.txt file2.txt
28
Unit-1: Basic Concepts of Unix Operating System

Apaar
Ayush Rajput
Deepak
Hemant

//suppress third column using -3//


$comm -3 file1.txt file2.txt
Ayush Rajput
Deepak
Lucky
Pranjal Thakral
Note that you can also suppress multiple columns using these options
together as:

$comm -12 file1.txt file2.txt


Apaar
Hemant

2. Using – -check-order option : This option is used to check whether the


input files are sorted or not and in case if either of the two files are wrongly
ordered then comm command will fail with an error message.
$comm - -check-order f1.txt f2.txt
The above command produces the normal output if both f1.txt and f2.txt are
sorted and it just gives an error message if either of the two files are not
sorted.
3. Using – -nocheck-order option : In case if you don’t want to check
whether the input files are sorted or not, use this option. This can be
explained with the help of an example.
//displaying contents of unsorted f1.txt//

$cat f1.txt
Parnjal
Kartik

//displaying contents of sorted file f2.txt//

$cat f2.txt
Apaar
29
Unit-1: Basic Concepts of Unix Operating System

Kartik

//now use - -nocheck-order option with comm//

$comm - -nocheck-order f1.txt f2.txt


Pranjal
Apaar
Kartik

/*as this option forced comm not to check


the sorted order that's why the output
comm produced is also
not in sorted order*/
4 . – -output-delimiter=STR option : By default, the columns in the comm
command output are separated by spaces as explained above. However, if
you want, you can change that, and have a string of your choice as separator.
This can be done using the –output-delimiter option. This option requires
you to specify the string that you want to use as the separator.
Syntax:
$comm - -output-delimiter=STR FILE1 FILE2
EXAMPLE:
//...comm command with - -output-delimiter=STR option...//

$comm - -output-delimiter=+file1.txt file2.txt


++Apaar
Ayush Rajput
Deepak
++Hemant
+Lucky
+Pranjal Thakral

diff command in Linux with examples


diff stands for difference. This command is used to display the differences in the
files by comparing the files line by line. Unlike its fellow
members, cmp and comm, it tells us which lines in one file have is to be changed
to make the two files identical.
30
Unit-1: Basic Concepts of Unix Operating System

The important thing to remember is that diff uses certain special


symbols and instructions that are required to make two files identical. It tells you
the instructions on how to change the first file to make it match the second file.
Special symbols are:
a : add
c : change
d : delete
Syntax :
diff [options] File1 File2
Lets say we have two files with names a.txt and b.txt containing 5 Indian states.

$ 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

$ diff a.txt b.txt


3d2
< Telangana
Here above output 3d2 means delete line 3rd of first file i.e., Telangana so that
both the files sync up at line 2.
Options

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

$ diff -c file1.txt file2.txt


*** file1.txt Thu Jan 11 08:52:37 2018
--- file2.txt Thu Jan 11 08:53:01 2018
***************
*** 1,4 ****
cat
- mv
- comm
cp
--- 1,4 ----
cat
cp
+ diff
+ comm
The first file is indicated by ***, and the second file is indicated by —.
The line with *************** is just a separator.
The first two lines of this output show us information about file 1 and file
2. It lists the file name, modification date, and modification time of each
of our files, one per line.
The next line has three asterisks *** followed by a line range from the
first file (in our case lines 1 through 4, separated by a comma). Then four
asterisks ****. After that it shows the contents of the first file with the
following indicators:
33
Unit-1: Basic Concepts of Unix Operating System

(i) If the line needs to be unchanged, it is prefixed by two spaces.


(ii) If the line needs to be changed, it is prefixed by an symbol and a
space. The symbol means are as follows:
(a) +: It indicates a line in the second file that needs to be added to the
first file to make them identical.
(b) –: It indicates a line in the first file that needs to be deleted to make
them identical.
Like in our case, it is needed to delete mv and comm from first file and
add diff and comm to the first file to make both of them identical.
After that there are three dashes — followed by a line range from the
second file (in our case lines 1 through 4, separated by a comma). Then
four dashes —-. Then it shows the contents of the second file.
1. -u (unified): To view differences in unified mode, use the -u option. It is
similar to context mode but it doesn’t display any redundant
information or it shows the information in concise form.
$ cat file1.txt
cat
mv
comm
cp

$ cat file2.txt
cat
cp
diff
comm

$ diff -u file1.txt file2.txt


--- file1.txt 2018-01-11 10:39:38.237464052 +0000
+++ file2.txt 2018-01-11 10:40:00.323423021 +0000
@@ -1,4 +1,4 @@
cat
-mv
-comm
cp
+diff
+comm
The first file is indicated by —, and the second file is indicated by +++.
The first two lines of this output show us information about file 1 and file
2. It lists the file name, modification date, and modification time of each
34
Unit-1: Basic Concepts of Unix Operating System

of our files, one per line.


After that the next line has two at sign @ followed by a line range from
the first file (in our case lines 1 through 4, separated by a comma)
prefixed by – and then space and then again followed by a line range
from the second file prefixed by +and at the end two at sign @. Followed
by the file content in output tells us which line remain unchanged and
which lines needs to added or deleted (indicated by symbols) in the file
1 to make it identical to file 2.
-i: By default, this command is case sensitive. To make this
command case in-sensitive use -i option with diff.
$ cat file1.txt
dog
mv
CP
comm

$ cat file2.txt
DOG
cp
diff
comm

Without using this option:


$ diff file1.txt file2.txt
1,3c1,3
< dog
< mv
DOG
> cp
> diff

Using this option:


$ diff -i file1.txt file2.txt
2d1
diff
–version: This option is used to display the version of diff which is
currently running on your system.
$ diff --version
diff (GNU diffutils) 3.5

35
Unit-1: Basic Concepts of Unix Operating System

Packaged by Cygwin (3.5-2)


Copyright (C) 2016 Free Software Foundation, Inc.

36

You might also like