BCA1 Linux

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Introduction to UNIX

UNIT2-The File System and Some File Handling Commands

The file:

* The file is a container for sharing information.

* UNIX file doesn‟t contain the eof (end – of - file) mark. A file‟s size is not stored in
the file nor even its name.

* All file attributes are kept in a separate area of the hard disk, not directly accessible to
humans, but only to the kernel.

* UNIX treats directories and devices as file as well. A directory is simply a folder
where you store filenames and other directories.

* All physical devices like the hard disk, memory, CD-ROM, printer and modem are
treated as files.

* Files divided into 3 categories:

1) Ordinary file: Also known as regular file it contain only data as a steam of
characters.

2) Directory file: It‟s commonly said that a directory contains file and other directories,
but it contains their names and a number associated with each name.

3) Device file: To read or write a device, you have to perform these operations
on its associated file.

1. Ordinary files (Regular):


An ordinary file or regular file is the most common file type. All programs you write
belong to this type. It can be divided into 2 types

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 1


Introduction to UNIX

1. Text file
2. Binary file
A text file contains only printable characters, and you can often view the contents and
make sense out of them. All C and JAVA program sources, shell and Perl scripts are
text files. A text file contains lines of characters where every line is terminated with the
new line character, also known as linefeed (LF).

A binary file one 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 & executable that you produce by compiling C programs are also binary
files. Picture, sound and video files are binary files. Displaying such files with a simple
cat command produces unreadable output & may even disturb your terminals setting.

2. Directory file:
A directory contains no data, but keeps some details of the files & subdirectories that it
contains. 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 components:

* The file name

* A unique identification number for the file or directory.

A directory contains the file name and no the files contents. You can‟t write a directory
file, but you can perform some action that makes the kernel write a directory. When you
create or remove a file, the kernel automatically updates its corresponding directory by
adding or removing the entry associated with the file.

3. Device file:
Printing files, installing software from CD- ROM or backing up files to tape. All of
these activities are performed by reading or writing the file representing the device to
treat devices as file as some of the commands used to access an ordinary file also work
with device files.

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 2


Introduction to UNIX

Device filenames are generally found inside a single directory structure, 1 dev, its not
really stream of characters. In fact, it doesn‟t contain anything at all. The operation of a
device is entirely governed by the attributes of its associated file. The kernel identifies a
device from its attributes and then use them to operate the device.

What’s in a (File) name:

A filename can consist of up to 255 characters, though this figure is normally not
reached. Files may or may not have extensions, & can consist of practically any ASCII
character expect the / & the NULL character. You are permitted to use control
characters or other unprintable characters in a filename.

The following characters to be used in filenames:

* Alphabetic characters and numerals.


* The period (.), hyphen (-) and underscore (_).
In all cases, it‟s the applicants that impose the restriction. Thus the C compiler expects c
program filenames to end with .c. oracle requires SQL scripts to have the .sql extension,
& so forth.

Dos/Windows users must also keep these 2 points in mind:


* A file can have as many dots embedded in its names a.b.c.d. e is a perfectly valid
filename.
* UNIX is sensitive to case.

The parent - child relationship

All files in UNIX are "related" to one another. The file system in UNIX is a collection
of these related files organized in a hierarchical structure. This system has also been
adapted by DOS and Windows.

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 3


Introduction to UNIX

The implicit feature of every UNIX file system is that there is a top, which serves
as the reference point for all files. This top is called root and is represented by a /(front
slash). Root is actually a directory.

The root directory (/) has a number of subdirectories under it. These
subdirectories in turn have more subdirectories & other files under them. In these
parent-child relationships, the parent is always a directory home and kumar are both
directories as they are both parents of at least one file or directory.

Fig. The UNIX File System Tree

Entire file system as comprising two groups of file.

* one group – contains the files that are made available during system installation.

* second group – users related.

 /bin and / user / bin – Company used UNIX commands are found PATH variable
always shows these directories in its list.

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 4


Introduction to UNIX

 / sbin and / user / sbin – if there‟s a command that you can‟t execute but the system
administrator can then it would probably be in one of these directories.

 / etc – contain the configuration file of the system you can change a very important
aspect of system functioning by editing a text file in this directory.

 / dev – This directory contain al device files pfs, dsk&rdsk in this directory device
files are stored.

 / lib and / user / lib – contain all library file in binary form you will need to link
your C programs with file in the directories.

 / user / include – contains the standard header file used by C programs.

# include <stdio.h> used in C refers to stdio.h file.

 / user / share / man – This is where the man page are stored. These are separate
subdirectories that contain the pages for each section. These subdirectories may have
different names on your system.

Second group:

Content of these directories would change as more software and utilities are added to
the system. Users also work with their own files they write programs, send and receive
mail and also create temporary files.

 / tmp – the directories where users are allowed to create temporary files. These
files are wiped away regularly by the system.

 / var – The variable part of the file system contains all your print jobs and your
outgoing and incoming progs.

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 5


Introduction to UNIX

 / home – System users are housed here. kumar have his

Home directories in / home / Kumar. Your system may use a different location
for home directories.

* The HOME VARIABLE : The Home directory:

When you log on to the system, UNIX automatically places you in a directory
called the home directory. It is created by the system when a user account is apend. If
you log in using the login name kumar, you will land up in a directory that could have
the pathname /home/kumar.

$ echo $HOME

/home/kumar First/represents the root directory

Home Variable:

Absolute pathname, which is simply a sequence of directory names separated by slashes


(/). It shows files location with reference to the top, i.e. root. These slashes (/) act as
delimiters to file & directory names, except that the first slash is a synonym for root.

It‟s often convenient to refer to a file Foo located in your home directory as
$HOME/foo . Most shells also use the ~ symbol for this purpose. So, $HOME/foo is the
same as ~/foo in these shells.

If user Sharma has the file foo in his home directory, then Kumar can access it as ~
Sharma/foo. A tilde followed by / (like ~/foo) refers to one‟s own home directory, but
when followed by a string (~Sharma) refers to the home directory of that users
represented by the string.

* Pwd: checking your current directory

A user is placed in a specific directory of the file system. You can move around from
one directory to another, but at any point of time, you are located in only one directory.
This directory is known as your current directory.

Syntax: Pwd(option)

$ pwd

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 6


Introduction to UNIX

/home/kumar

Pwd displays the absolute, as you navigate the file s/m with the cd command, you‟ll be
using pwd to know your current directory.

* cd : changing the current directory

1. You can move around in the file system by using the cd command.

$ pwd

/home / kumar

$ cdprogs

$ pwd

/home / kumar / progs

Progs must be in current directory. Though pwd displays the absolute path name.
Cd doesn‟t need to use one. When you need to switch to the /bin directory where most
of the commonly used UNIX commands are kept, you should use the absolute
pathname:

$ pwd

/ home / kumar / progs

$ cd / bin absolute pathname required here

$ pwd because bin isn‟t current directory

/bin

We can also navigate to /bin using a different type of pathname.

3. cd can also used without any arguments:

$ pwd

/home / kumar / progs

cd used without arguments reverts to the home directory.

$ cd

$ pwd
By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 7
Introduction to UNIX

/ home / kumar

Syntax: Pwd [option]

Ex: $ pwd

/home/kumar

pwd displays the absolute pathname. As you navigate the file system with the cd
command, you will be using pwd to know your current directory.You can force an
immediate return to your home directory by simply using cd:

$ cd /home/ sharma

$ pwd

/ home / sharma

$ cd Returns to home directory

$ pwd

/ home/ kumar

mkdir : Making Directories

Directories are created with the mkdir command. The command is followed by names
of the directories to be created.

Syntax : mkdir [option]. Directory

Ex: $ mkdirMydir

$ mkdirkcD.

 Create more than one directories

$mkdirMydirkcDpictures.

 You can‟t create a subdirectories before creation of it‟s parent directory.

$ mkdiretc |kcd

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 8


Introduction to UNIX

mkdir : cannot create directory „etc|kcd‟ : permission denied. User does not have
permission to create directory in parent directory.

 Sometimes, the system refuses to create a directory:

$mkdir test

mkdir: Failed to make directory “test” : Permission denied.

This can happen due to these reasons:

* the directory test test may already exist.

* There may be an ordinary file by that name in the current directory.

* The permissions set for the current directory don‟t permit the creation of files &
directories by the user.

Options:

* -m : Set file mode

* -p : make parent directory as needed if no error exist

* -v : print message for each created directory.

rmdir : Removing directories

The rmdir command removes directories.

Ex: $rmdirprgms Directory must be empty.

rmdir can also delete more than one directory in one shot.

If directory is not empty, rmdir will not remove it.

$rmKcdKscd,

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 9


Introduction to UNIX

To remove both a parent and subdirectory, subdirectory must be specified first. So


parent is empty when rmdir tries to remove it.

If you want to remove a directory that is not empty you can use „rm‟ command with –r
option.

Ex: $ rm –r

Ex: $ rmdirdir /subdir

Delete sub a parent directory.

Ex: $ rmdir –p dir /subdir.

Absolute pathnames:

Absolute path is defined as the specifying the location of a file or directory from the
root directory.

Ex: / home / kumar / login.sql

When you have more than one / in pathname, for each /, you have a descend one level in
the file system.

Thus Kumar is, one level below home, and two levels below root .no two file in a UNIX
system can have identical absolute pathname. You can have two files with the same
name, but in different directories, their pathname will also be different.

Ex:
/ home / kumar / prage / c2f.pl can cd-exist with file
/ home / kuamr / safe / C2f.pl
When you specify the date command, the system has to locate the file date from a list of
directories specified in the PATH variable, and then execute it.

$ / bin / date

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 10


Introduction to UNIX

Thu sep | 09:30:09 IST 2005

Relative pathname:

Relative pathname is defined as path related to the present working directory. It is starts
with your current directory, never begin with a /.UNIX offers a shortcut the relative
pathname that uses the current or parent directory as reference and specifies the path
relative to it. A relative pathname uses one of these cryptic symbols.

1) (single dot) – this represent the current directory

2) (two dot) – represents the parent directory

$ pwd

/ home / kumar / abc

$ cd ..

$ pwd

/ home / kumar / progs / data moves one level up.

The command cd.. Translate to this: changing your directory to the parent of the current
directory you can combine any number of such sets of ..separated by /.

$ cd /

$ pwd

/ home move two levels up

Ex: Relative path concept Absolute path

$ pwd $ pwd

/ home / kcd / home / kcd

$ cdabc $ cd / home / kcd / abc

$ pwd $ pwd

/ home / kcd / abc / home / kcd / abc


By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 11
Introduction to UNIX

ls: Listing directory contents


ls is used to obtain a list of all file name and directories in the current directory.
Ex: $ls
08-packets.html Numerals first
Toc.sh Uppercase Next
Calendar Then lowercase
Dept.lst
Emp.1st
helpdir
progs
ls with the file name

Ex:

$lsperl

Perl: No such file or directory.

ls also be used with multiple filenames, and has options that list most of the file
attributes.

Option :

-x display the filename in multiple columns.

-f identify Directories and executables

-a showing hidden files.

-R Recursive listing

Ex: $ls –x

08-packets.html Toc.sh Calendar Cptodos.sh.

Dept.lst emp.lst helpdir progs

Usdskox Usdskx07x Usdsk08x Ux2nd06

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 12


Introduction to UNIX

$ lc –Fx

08 – packets.html Toc.sh* calendar * copodes.sh*

Dept.lst emp.lst helpdir/ progs/

Ucdc106x usdsk01x usdsk08x ux8cd06

$ lc –aXF

./ ../ .cXrc .kshrc

.profile .rhosts .sh- history .xdtsupcheck

.Xinitic os - packects.html* Toc.sh* .calendar*

Use of two symbols * and / as type indicators. The * indicates that the file contains
executable code and the / refers to a directory.

Basic Comands on Ordinary File:

1. Cat : It is mainly used to display the content of a small file on the terminal.
Syntax:
cat options [files…..]
Ex: 1) $ cat sham.txt

This is the BCA dept

Options:

1) –v :Displaying Nonprinting Characters

Cat normally used for displaying text file only. If you have nonprinting ASCII
characters in your input. You can use cat with the –v option to display these characters.

2) –n :Numbering Lines

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 13


Introduction to UNIX

The C compilers indicate the line number where error are detected and this numbering
facility often helps a programmer in debugging programs, but then your vi editor can
show line number too, and if your version of cat doesn‟t support –n, you can use the „pr‟
command to do the same job.

Create a file using cat:

Operation used with cat:

1) Redirect output cat command to file rather than standard output. The file will be
created if it doesn‟t exist and will be overwritten if it does exist.

2) Append the output of cat command to end of existing file. The destination file
will be created if it doesn‟t exist.

3) Send output of cat into another command for further processing.

Ex: $ cat file1

Display content of file

$ cat kcd.txt add.c

This is the BCA dept

Main()

$ cat> BCA

BCA VI sem student are not wearing uniform regularly today onwards they will pay
20rs per day.

Concatenate two files

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 14


Introduction to UNIX

$ cat kcd.txt add.c>Ram

Append content of file to

Main()

This is BCA dept

Cat is a versatile command. It can be used to create, display concatenate and append to
file. More importantly it doesn‟t restrict itself to handling file only, it also act on a
stream. You can supply input to cat not only by specifying a filename, but also from the
output of another command.

CP: copying a file:

The cp command copies a file. It creates an exact image of the file on disk with a
different name. syntax requires at least two filename.

Syntax: CP [options] source destination

Ex: $ CP file1 file2

If destination file file2 doesn‟t exist, it will first be created before copying takes place.
If not it will simply overwritten without any warning from the system. So be careful
when you choose your destination filename. Just check with the ls command wither or
the file exist.

Only one file to be copied. The destination can be either file or directory there is 2 way
to copy

$ cp chap01 progs/kcd //chapo1 copied to kcd under progs

$ cp chap01 progs // chapo1 retain its name under progs

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 15


Introduction to UNIX

CP can be used to copy more than one file with invocation of command but last name
must be directory

Ex: $ cp chap01 chap02 chap03 progs

Or

$ cp chapo1 progs

Options:

1) –i : interactive copying.

The –i option warns the user before overwriting the destination file.

Ex: $ CP –i chapo1 kcd

CP: overwrite kcd (yes/no) ? Y

2) –R : copying directory structure

The cp –R command behave recursively to copy an entire directory structure.

Ex: $cp –R progsnewprogs // newprogs must not exist

rm: Deleting files


The rm command deletes one or more file.
Ex: $rm chap01 chap02 chap03 /deletes 3 files/

A file once delete cant be recovered.rm wont normally remove a directory. But it can
remove files from one rm chap01 could be dangerous to use.

You may sometimes need to delete all files in directory as part of a cleanup operation.
The * represents all files.

Ex: $ rm * //all files deleted.

$-

Options:
1) –i : interactive deletion

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 16


Introduction to UNIX

In cp the –i option make the command ask the user for confirmation before removing
each file.

Ex: $ rm –I chap01 chap 02

rm : remove chap 01 (yes/no) ? y

rm : remove chap 02 (yes/no) ? y

A Y removes the file any other response leave the file undeleted

2) –R : or –r recursive deletion

With the –r or –R option rm perform free traversal for all subdirectories & files within
subdirectories. At each stage it deletes.

Ex: $ rm –r delete all subdirectories & files.

3) –f : forcing removal
rm prompts for removal if a file is write protected. The –f option overrides this minor
protection & force removal when we combine it with the –r option it could be the most
ristly thing to do
$ rm –rf *

Deletes everything in the current directory and below.

mv :renaming files:

The mv command renames files. It has two distinct fuction

 it renames a file.

 It moves a group of file to a different directory.

Syntax : mv [option] source destination

Moves file or directories from one place to another move single, multiple files &
directories.

mv doesn‟t create a copy of the file. It renames it. No additional space is consumed on
disk.

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 17


Introduction to UNIX

Ex : Rename the file BCA to BSC


$ mv BCA, BSC
 If destination file doesn‟t exist it will be created.
 mv simply replaces the file name in the existing entry with the new
name.Bydefault mv doesn‟t prompt for overwriting the deshination file it exist.
So be careful again.
Ex: $ mv chap01 chap02 chap03
progs
 Mv can also be used to rename directory, $ mv FC progs
more: Paging output
more used to view the text file in the command prompt displaying one screen at a
time in case the file is larger

Syntax: more [option ] file

Options :
1) –num: type the number of lines that you want to display
2) –d : to help user to navigate, it display
“[ press „space‟ to continue ,‟9‟ to quit]” and display.
“[press „h‟ for instructions]” when wrong key is procced
Ex: $ more –d kcd.txt

More (5%)[press space 9]


3) –s : to compress multiple blank line into one
Ex : $ more –s kcd.txt

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 18


Introduction to UNIX

4) –p : cleans the screen & the display the text.


5) –c : command is used to scroll forward a page at a time area by overlapping the
previous displayed text
Navigation :
More used the spacebar to scrollforword a page at a time you can also scroll by
small and large increment of line or screens. To move forward one page use.
1) f or spacebar
To move back one page
2) b
The repeat features:
Many navigation command in more including f and b , use a repeat factor
1) 10f – for scrolling forward by 10 page
2) 30b – for scrolling back 30 page
Repeating the last command(.) : more has repeat command the dot, that repeats the last
command you used.
Searching for a pattern:
You can perform a search for a pattern with the / commands followed by the
strings,
Ex:$ /while search while keyword in your program
n: for repeat search
b: for move back
Pipeline:
The man syntax doesn‟t indicates this, but we often use more to page. The output
of another command. The Ls output won‟t fit on the screen if there are too many files,
so the commands has to be used like this:
$ ls | more
The lp SUBSYSTEM: PRINTING FILE.

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 19


Introduction to UNIX

No user allowed direct access to the printer. Instead, one has to spool (line up ) a
job along with others in a print queue. Spooling ensures the orderly printing of jobs
and relives the user from of necessary of administering the print resources
Example $lp kcd.txt
Request id is prl-320 (1 file)
Several users can print their files in this way without conflict. lp notifies the
request id .
Options:
-d :reports the current default printer

-p : see the list of printers

-t : title prints the title on the first page

-n: multiple copies of print

-m: mail options.

Commands in the lp subsystem;


The print queue is viewed with the lpstat command. By viewing this list. You
can use the cancel command to cancel any jobs submitted by you. Cancel uses the
request. idor printer name as argument:
$ cancel laser cancel current job on printer laser

$ cancel prt-280 cancel job with require_id prt-320

File : Knowing the file types

A regular file may contain plain text, a C program or executable code. UNIX provides
the file command to determine the type of file, especially of an ordinary file. you can
use it with one or more filenames as arguments.

$ file archive.zip

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 20


Introduction to UNIX

Archive.zip : zip archive

File correctly identifies the basic file types. Using the * to signify all files.

$ files *

All the files displayed from current directory.

wc: counting lines, words AND characters

UNIX features a universal word. Counting program that also counts line & characters. It
takes one or more filenames as arguments & displays a four columnar output. Before
you use WC on the file infile, just use cat to view its cotents:

S cat infile

I am the WC command

I count characters, words and lines

With options

You can now use WC without options to make a “word count” of the data in the file:

$ WC infile

3 20 103 infile.

We count 3 lines,20words
& 103 characters. The file name has also been shown in the fourth column.
Option:
-l : no of lines display.
A line is ony group of character not containing a new line.
Ex : $we -l infile
3 infile –no of lines
-w : no of words display
A word is agroup of characters not containing space, tab or newline.

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 21


Introduction to UNIX

Ex: $ we-w infile


20infile –no of words
-c : no of characters the smallest unit of information & includes a space, tab and
newline
Ex : $ wc –c infile
103 infile –no of characters
 When used with multiple filenames. Wc produce a line for each file as well as
total count.
 $ wc chap01 chap02 chap03
305 4058 23179 chap-01
550 4732 28132 chap-02
377 4500 25521 chap-03
1232 13290 76532 total –A total as a bonus.

od : Displaying data in octal.

Executable files contain nonprinting characters, and most UNIX commands don‟t
display them properly.

Symbox: od[option] [file]..


Ex: $ more odfile

The ^G character rings a belt

Options:

-b : display the content of input in octal form

-c : display the content of input in character.

The –b option display this value for each character seperately.

$ od –b odfile

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 22


Introduction to UNIX

Each line displays 16 bytes of data in octal, proceeded by the offset in the file of the
first byte in the line. When the –b and –c options are combined the output is

$ od –bcodfile

00000000 124 150 145 040 007 143 150 141

T H e - 007 -c h a

162 141 143 164 145 162 040

r a c t e f -

0000020 162 151 156 147 163 040 142

r I n 9 5 - b

145 154 154 012

E | | /n

cmp: comparing two file

You may often need to know whether two file are identical so one of them can be
deleted. Cmp (compare) command. Obviously it needs two filenames.

$ cmp chap01 chap02

Chap01 chap02 differ char 9, line 1

 Two files are compared byte by byte and the location of the first mismatch (in
the ninth character of the first line) is echoed to the screen.

 cmp doesn‟t bother about possible subsequent mismatches.

If two file are identical, cmp displays no message, but simply returns the prompt.

$ _cmp chap01 chap02

$_

Comm: what is common?

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 23


Introduction to UNIX

It requires the both files are sorted & have some differences. When you run comm, it
displays a three columnar output.

Ex: $ cat chap05 $ cat chap06

Anil Ayush

Pranav Pranav

Vaidik Vaidik

Veeraj

Both file are sorted and have some differences. When you run comm, it displays a
three columns output.

comparing file1 and file2


$ comm file[12]
Anil
Ayush
Pranav
Vaidik
Veeraj
The first column contains two lines unique to the first file, and the second column
shows three lines common to the both files.

Ex: $ cat files S cat files2

1 8

2 3

3 2

$ comm file1 file2

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 24


Introduction to UNIX

1 2

8 3

Options:

-1 : drop the particular column file1

-2 : drop the particular column file2

-3 : common.

Spell: checking the spelling

Options:

-b: use british english dictionary if it is available

-n : print line number (for each misplaced word)

-o: print file name(

Example:

$ spell on file1.txt file2.txt

file1.txt: 1 :this2

File.txt: 2 : file

File2.txt:3 : ha2

File.txt. 13: mistakes

File2.txt:6 :aree

File2.txt:19 :also

Diff: Differences in two files.

0r

diff: Converting one file to other.

 Used to display the differences in the file by comparing line by line.

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 25


Introduction to UNIX

 Comp and comm tells us which lines in me file have to be changed to make
the two files identical.

Syntax: diff [option] file1 file2

Special symbol used:


a : add or append
c : change
d : delete

$cat a.txt $ cat b.txt

0 Gujrath Tamilnadu

1 uttarpradesh Gujrath

2 Kolkata Andrapradesh

3 Bihar Bihar

4 Jammu Uttarpradesh

$ diff a.txt b.txt

0a1
>Tamilnadu
2.3c3
<uttarpradesh
Andrapradesh
5c5
Uttarpradesh
 The first line of different o/p contain
 line numbers corresponding to the first file

 A special symbol

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 26


Introduction to UNIX

 Line numbers corresponding to the second file

0a1 – after line 0 add Tamilnadu to match the second file line number 1.

2,3c3 – from line no 2 to 3 in first file needs to be changed to match line no3 in second
file.

5c5 – from line no 5 in first file needs to changed to match line no 5.

By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 27

You might also like