0% found this document useful (0 votes)
12 views37 pages

Unit 2 Linux

This document provides an overview of directory management in Unix/Linux, covering topics such as the hierarchical structure of directories, commands for navigating and managing directories, and file permissions. It explains how to create, remove, and rename directories, as well as how to change permissions and ownership of files. Additionally, it introduces the vi editor as a tool for editing files within Unix/Linux.
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)
12 views37 pages

Unit 2 Linux

This document provides an overview of directory management in Unix/Linux, covering topics such as the hierarchical structure of directories, commands for navigating and managing directories, and file permissions. It explains how to create, remove, and rename directories, as well as how to change permissions and ownership of files. Additionally, it introduces the vi editor as a tool for editing files within Unix/Linux.
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/ 37

BCA V SEMESTER

LINUX PROGRAMMING
UNIT 2

Unix / Linux - Directory Management

A directory is a file the solo job of which is to store the file names and the related
information. All the files, whether ordinary, special, or directory, are contained in
directories.
Unix uses a hierarchical structure for organizing files and directories. This structure
is often referred to as a directory tree. The tree has a single root node, the slash
character (/), and all other directories are contained below it.

Home Directory
The directory in which you find yourself when you first login is called your home
directory.
You will be doing much of your work in your home directory and subdirectories that
you'll be creating to organize your files.
You can go in your home directory anytime using the following command −
$cd ~
$
Here ~ indicates the home directory. Suppose you have to go in any other user's
home directory, use the following command −
$cd ~username
$
To go in your last directory, you can use the following command −
$cd -
$

Absolute/Relative Pathnames
Directories are arranged in a hierarchy with root (/) at the top. The position of any file
within the hierarchy is described by its pathname.
Elements of a pathname are separated by a /. A pathname is absolute, if it is
described in relation to root, thus absolute pathnames always begin with a /.
Following are some examples of absolute filenames.
/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3
A pathname can also be relative to your current working directory. Relative
pathnames never begin with /. Relative to user amrood's home directory, some
pathnames might look like this −
chem/notes
personal/res
To determine where you are within the filesystem hierarchy at any time, enter the
command pwd to print the current working directory −
$pwd
/user0/home/amrood

Listing Directories
To list the files in a directory, you can use the following syntax −
$ls dirname
Following is the example to list all the files contained in /usr/local directory −
$ls /usr/local

X11 bin gimp jikes sbin


ace doc include lib share
atalk etc info man ami

Creating Directories
We will now understand how to create directories. Directories are created by the
following command −
$mkdir dirname
Here, directory is the absolute or relative pathname of the directory you want to
create. For example, the command −
$mkdir mydir
$
Creates the directory mydir in the current directory. Here is another example −
$mkdir /tmp/test-dir
$
This command creates the directory test-dir in the /tmp directory.
The mkdir command produces no output if it successfully creates the requested
directory.
If you give more than one directory on the command line, mkdir creates each of the
directories. For example, −
$mkdir docs pub
$
Creates the directories docs and pub under the current directory.

Creating Parent Directories


We will now understand how to create parent directories. Sometimes when you want
to create a directory, its parent directory or directories might not exist. In this
case, mkdir issues an error message as follows −
$mkdir /tmp/amrood/test
mkdir: Failed to make directory "/tmp/amrood/test";
No such file or directory
$
In such cases, you can specify the -p option to the mkdir command. It creates all the
necessary directories for you. For example −
$mkdir -p /tmp/amrood/test
$
The above command creates all the required parent directories.

Removing Directories
Directories can be deleted using the rmdir command as follows −
$rmdir dirname
$
Note − To remove a directory, make sure it is empty which means there should not
be any file or sub-directory inside this directory.
You can remove multiple directories at a time as follows −
$rmdir dirname1 dirname2 dirname3
$
The above command removes the directories dirname1, dirname2, and dirname3, if
they are empty. The rmdir command produces no output if it is successful.

Changing Directories
You can use the cd command to do more than just change to a home directory. You
can use it to change to any directory by specifying a valid absolute or relative path.
The syntax is as given below −
$cd dirname
$
Here, dirname is the name of the directory that you want to change to. For example,
the command −
$cd /usr/local/bin
$
Changes to the directory /usr/local/bin. From this directory, you can cd to the
directory /usr/home/amrood using the following relative path −
$cd ../../home/amrood
$

Renaming Directories
The mv (move) command can also be used to rename a directory. The syntax is as
follows −
$mv olddir newdir
$
You can rename a directory mydir to yourdir as follows −
$mv mydir yourdir
$

The directories . (dot) and .. (dot dot)


The filename . (dot) represents the current working directory; and the filename
.. (dot dot) represents the directory one level above the current working directory,
often referred to as the parent directory.
If we enter the command to show a listing of the current working directories/files and
use the -a option to list all the files and the -l option to provide the long listing, we
will receive the following result.
$ls -la
drwxrwxr-x 4 teacher class 2048 Jul 16 17.56 .
drwxr-xr-x 60 root 1536 Jul 13 14:18 ..
---------- 1 teacher class 4210 May 1 08:27 .profile
-rwxr-xr-x 1 teacher class 1948 May 12 13:42 memo
$
we will discuss in detail about file permission and access modes in Unix. File
ownership is an important component of Unix that provides a secure method for
storing files. Every file in Unix has the following attributes −
 Owner permissions − The owner's permissions determine what actions the
owner of the file can perform on the file.
 Group permissions − The group's permissions determine what actions a
user, who is a member of the group that a file belongs to, can perform on the
file.
 Other (world) permissions − The permissions for others indicate what action
all other users can perform on the file.

The Permission Indicators


While using ls -l command, it displays various information related to file permission
as follows −
$ls -l /home/amrood
-rwxr-xr-- 1 amrood users 1024 Nov 2 00:10 myfile
drwxr-xr--- 1 amrood users 1024 Nov 2 00:10 mydir
Here, the first column represents different access modes, i.e., the permission
associated with a file or a directory.
The permissions are broken into groups of threes, and each position in the group
denotes a specific permission, in this order: read (r), write (w), execute (x) −
 The first three characters (2-4) represent the permissions for the file's owner.
For example, -rwxr-xr-- represents that the owner has read (r), write (w) and
execute (x) permission.
 The second group of three characters (5-7) consists of the permissions for the
group to which the file belongs. For example, -rwxr-xr-- represents that the
group has read (r) and execute (x) permission, but no write permission.
 The last group of three characters (8-10) represents the permissions for
everyone else. For example, -rwxr-xr-- represents that there is read (r) only
permission.
File Access Modes
The permissions of a file are the first line of defense in the security of a Unix system.
The basic building blocks of Unix permissions are the read, write,
and execute permissions, which have been described below −
Read
Grants the capability to read, i.e., view the contents of the file.
Write
Grants the capability to modify, or remove the content of the file.
Execute
User with execute permissions can run a file as a program.

Directory Access Modes


Directory access modes are listed and organized in the same manner as any other
file. There are a few differences that need to be mentioned −
Read
Access to a directory means that the user can read the contents. The user can look
at the filenames inside the directory.
Write
Access means that the user can add or delete files from the directory.
Execute
Executing a directory doesn't really make sense, so think of this as a traverse
permission.
A user must have execute access to the bin directory in order to execute the ls or
the cd command.

Changing Permissions
To change the file or the directory permissions, you use the chmod (change mode)
command. There are two ways to use chmod — the symbolic mode and the absolute
mode.
Using chmod in Symbolic Mode
The easiest way for a beginner to modify file or directory permissions is to use the
symbolic mode. With symbolic permissions you can add, delete, or specify the
permission set you want by using the operators in the following table.

Sr.No. Chmod operator & Description


1
+
Adds the designated permission(s) to a file or directory.

2
-
Removes the designated permission(s) from a file or directory.

3
=
Sets the designated permission(s).

Here's an example using testfile. Running ls -1 on the testfile shows that the file's
permissions are as follows −
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run on the testfile,
followed by ls –l, so you can see the permission changes −
$chmod o+wx testfile
$ls -l testfile
-rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod u-x testfile
$ls -l testfile
-rw-rwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod g = rx testfile
$ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
Here's how you can combine these commands on a single line −
$chmod o+wx,u-x,g = rx testfile
$ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile

Using chmod with Absolute Permissions


The second way to modify permissions with the chmod command is to use a number
to specify each set of permissions for the file.
Each permission is assigned a value, as the following table shows, and the total of
each set of permissions provides a number for that set.

Number Octal Permission Representation Ref

0 No permission ---
1 Execute permission --x

2 Write permission -w-

3 Execute and write permission: 1 (execute) + 2 (write) = -wx


3

4 Read permission r--

5 Read and execute permission: 4 (read) + 1 (execute) = r-x


5

6 Read and write permission: 4 (read) + 2 (write) = 6 rw-

7 All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 rwx

Here's an example using the testfile. Running ls -1 on the testfile shows that the file's
permissions are as follows −
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run on the testfile,
followed by ls –l, so you can see the permission changes −
$ chmod 755 testfile
$ls -l testfile
-rwxr-xr-x 1 amrood users 1024 Nov 2 00:10 testfile
$chmod 743 testfile
$ls -l testfile
-rwxr---wx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod 043 testfile
$ls -l testfile
----r---wx 1 amrood users 1024 Nov 2 00:10 testfile

Changing Owners and Groups


While creating an account on Unix, it assigns a owner ID and a group ID to each
user. All the permissions mentioned above are also assigned based on the Owner
and the Groups.
Two commands are available to change the owner and the group of files −
 chown − The chown command stands for "change owner" and is used to
change the owner of a file.
 chgrp − The chgrp command stands for "change group" and is used to
change the group of a file.

Changing Ownership
The chown command changes the ownership of a file. The basic syntax is as follows

$ chown user filelist
The value of the user can be either the name of a user on the system or the user id
(uid) of a user on the system.
The following example will help you understand the concept −
$ chown amrood testfile

Changes the owner of the given file to the user amrood.


NOTE − The super user, root, has the unrestricted capability to change the ownership
of any file but normal users can change the ownership of only those files that they
own.

Changing Group Ownership


The chgrp command changes the group ownership of a file. The basic syntax is as
follows −
$ chgrp group filelist
The value of group can be the name of a group on the system or the group ID
(GID) of a group on the system.
Following example helps you understand the concept −
$ chgrp special testfile
$
Changes the group of the given file to special group.

SUID and SGID File Permission


Often when a command is executed, it will have to be executed with special privileges
in order to accomplish its task.
As an example, when you change your password with the passwd command, your
new password is stored in the file /etc/shadow.
As a regular user, you do not have read or write access to this file for security
reasons, but when you change your password, you need to have the write permission
to this file. This means that the passwd program has to give you additional
permissions so that you can write to the file /etc/shadow.
Additional permissions are given to programs via a mechanism known as the Set
User ID (SUID) and Set Group ID (SGID) bits.
When you execute a program that has the SUID bit enabled, you inherit the
permissions of that program's owner. Programs that do not have the SUID bit set are
run with the permissions of the user who started the program.
This is the case with SGID as well. Normally, programs execute with your group
permissions, but instead your group will be changed just for this program to the group
owner of the program.
The SUID and SGID bits will appear as the letter "s" if the permission is available.
The SUID "s" bit will be located in the permission bits where the
owners’ execute permission normally resides.
For example, the command −
$ ls -l /usr/bin/passwd
-r-sr-xr-x 1 root bin 19031 Feb 7 13:47 /usr/bin/passwd*
$
Shows that the SUID bit is set and that the command is owned by the root. A capital
letter S in the execute position instead of a lowercase s indicates that the execute bit
is not set.
If the sticky bit is enabled on the directory, files can only be removed if you are one
of the following users −

 The owner of the sticky directory


 The owner of the file being removed
 The super user, root
To set the SUID and SGID bits for any directory try the following command −
$ chmod ug+s dirname
$ ls -l
drwsr-sr-x 2 root root 4096 Jun 19 06:45 dirname

Unix/Linux - The vi Editor Tutorial


In this chapter, we will understand how the vi Editor works in Unix. There are many
ways to edit files in Unix. Editing files using the screen-oriented text editor vi is one
of the best ways. This editor enables you to edit lines in context with other lines in the
file.
An improved version of the vi editor which is called the VIM has also been made
available now. Here, VIM stands for Vi IMproved.
vi is generally considered the de facto standard in Unix editors because −
 It's usually available on all the flavors of Unix system.
 Its implementations are very similar across the board.
 It requires very few resources.
 It is more user-friendly than other editors such as the ed or the ex.
You can use the vi editor to edit an existing file or to create a new file from scratch.
You can also use this editor to just read a text file.

Starting the vi Editor


The following table lists out the basic commands to use the vi editor −

Sr.No. Command & Description

1 vi filename
Creates a new file if it already does not exist, otherwise opens
an existing file.

2 vi -R filename
Opens an existing file in the read-only mode.

3 view filename
Opens an existing file in the read-only mode.

Following is an example to create a new file testfile if it already does not exist in the
current working directory −
$vi testfile
The above command will generate the following output −
|
~
~
~
~
~
~
~
~
~
~
~
~
"testfile" [New File]
You will notice a tilde (~) on each line following the cursor. A tilde represents an
unused line. If a line does not begin with a tilde and appears to be blank, there is a
space, tab, newline, or some other non-viewable character present.
You now have one open file to start working on. Before proceeding further, let us
understand a few important concepts.

Operation Modes
While working with the vi editor, we usually come across the following two modes −
 Command mode − This mode enables you to perform administrative tasks
such as saving the files, executing the commands, moving the cursor, cutting
(yanking) and pasting the lines or words, as well as finding and replacing. In
this mode, whatever you type is interpreted as a command.
 Insert mode − This mode enables you to insert text into the file. Everything
that's typed in this mode is interpreted as input and placed in the file.
vi always starts in the command mode. To enter text, you must be in the insert mode
for which simply type i. To come out of the insert mode, press the Esc key, which will
take you back to the command mode.
Hint − If you are not sure which mode you are in, press the Esc key twice; this will
take you to the command mode. You open a file using the vi editor. Start by typing
some characters and then come to the command mode to understand the difference.

Getting Out of vi
The command to quit out of vi is :q. Once in the command mode, type colon, and 'q',
followed by return. If your file has been modified in any way, the editor will warn you
of this, and not let you quit. To ignore this message, the command to quit out of vi
without saving is :q!. This lets you exit vi without saving any of the changes.
The command to save the contents of the editor is :w. You can combine the above
command with the quit command, or use :wq and return.
The easiest way to save your changes and exit vi is with the ZZ command. When
you are in the command mode, type ZZ. The ZZ command works the same way as
the :wq command.
If you want to specify/state any particular name for the file, you can do so by
specifying it after the :w. For example, if you wanted to save the file you were working
on as another filename called filename2, you would type :w filename2 and return.

Moving within a File


To move around within a file without affecting your text, you must be in the command
mode (press Esc twice). The following table lists out a few commands you can use to
move around one character at a time −
Sr.No. Command & Description

1 K
Moves the cursor up one line

2 J
Moves the cursor down one line

3 H
Moves the cursor to the left one character position

4 L
Moves the cursor to the right one character position

The following points need to be considered to move within a file −


 vi is case-sensitive. You need to pay attention to capitalization when using the
commands.
 Most commands in vi can be prefaced by the number of times you want the
action to occur. For example, 2j moves the cursor two lines down the cursor
location.
There are many other ways to move within a file in vi. Remember that you must be in
the command mode (press Esc twice). The following table lists out a few commands
to move around the file −
Given below is the list of commands to move around the file.
Sr.No. Command & Description

1
0 or |
Positions the cursor at the beginning of a line

2
$
Positions the cursor at the end of a line

3
W
Positions the cursor to the next word
4
B
Positions the cursor to the previous word

5
(
Positions the cursor to the beginning of the current sentence

6
)
Positions the cursor to the beginning of the next sentence

7
E
Moves to the end of the blank delimited word

8
{
Moves a paragraph back

9
}
Moves a paragraph forward

10
[[
Moves a section back

11
]]
Moves a section forward

12
n|
Moves to the column n in the current line

13
1G
Moves to the first line of the file

14
G
Moves to the last line of the file
15
nG
Moves to the nth line of the file

16
:n
Moves to the nth line of the file

17
Fc
Moves forward to c

18
Fc
Moves back to c

19
H
Moves to the top of the screen

20
nH
Moves to the nth line from the top of the screen

21
M
Moves to the middle of the screen

22
L
Move to the bottom of the screen

23
nL
Moves to the nth line from the bottom of the screen

24
:x
Colon followed by a number would position the cursor on the line number
represented by x

Control Commands
The following commands can be used with the Control Key to performs functions as
given in the table below −
Given below is the list of control commands.
Sr.No. Command & Description

1
CTRL+d
Moves forward 1/2 screen

2
CTRL+f
Moves forward one full screen

3
CTRL+u
Moves backward 1/2 screen

4
CTRL+b
Moves backward one full screen

5
CTRL+e
Moves the screen up one line

6
CTRL+y
Moves the screen down one line

7
CTRL+u
Moves the screen up 1/2 page

8
CTRL+d
Moves the screen down 1/2 page

9
CTRL+b
Moves the screen up one page

10
CTRL+f
Moves the screen down one page
11
CTRL+I
Redraws the screen

Editing Files
To edit the file, you need to be in the insert mode. There are many ways to enter the
insert mode from the command mode −

Sr.No. Command & Description

1 I
Inserts text before the current cursor location

2 I
Inserts text at the beginning of the current line

3 A
Inserts text after the current cursor location

4 A
Inserts text at the end of the current line

5 O
Creates a new line for text entry below the cursor location

6 O
Creates a new line for text entry above the cursor location

Deleting Characters
Here is a list of important commands, which can be used to delete characters and
lines in an open file −

Sr.No. Command & Description


1 X
Deletes the character under the cursor location

2 X
Deletes the character before the cursor location

3 Dw
Deletes from the current cursor location to the next word

4 d^
Deletes from the current cursor position to the beginning of the line

5 d$
Deletes from the current cursor position to the end of the line

6 D
Deletes from the cursor position to the end of the current line

7 Dd
Deletes the line the cursor is on

As mentioned above, most commands in vi can be prefaced by the number of times


you want the action to occur. For example, 2x deletes two characters under the
cursor location and 2dd deletes two lines the cursor is on.
It is recommended that the commands are practiced before we proceed further.

Change Commands
You also have the capability to change characters, words, or lines in vi without
deleting them. Here are the relevant commands −

Sr.No. Command & Description

1 Cc
Removes the contents of the line, leaving you in insert mode.
2 Cw
Changes the word the cursor is on from the cursor to the
lowercase w end of the word.

3 R
Replaces the character under the cursor. vi returns to the command
mode after the replacement is entered.

4 R
Overwrites multiple characters beginning with the character currently
under the cursor. You must use Esc to stop the overwriting.

5 S
Replaces the current character with the character you type.
Afterward, you are left in the insert mode.

6 S
Deletes the line the cursor is on and replaces it with the new text.
After the new text is entered, vi remains in the insert mode.

Copy and Paste Commands


You can copy lines or words from one place and then you can paste them at another
place using the following commands −

Sr.No. Command & Description

1 Yy
Copies the current line.

2 Yw
Copies the current word from the character the lowercase
w cursor is on, until the end of the word.

3 P
Puts the copied text after the cursor.
4 P
Puts the yanked text before the cursor.

Advanced Commands
There are some advanced commands that simplify day-to-day editing and allow for
more efficient use of vi −
Given below is the list advanced commands.
Sr.No. Command & Description

1
J
Joins the current line with the next one. A count of j commands join many
lines.

2
<<
Shifts the current line to the left by one shift width.

3
>>
Shifts the current line to the right by one shift width.

4
~
Switches the case of the character under the cursor.

5
^G
Press Ctrl and G keys at the same time to show the current filename and
the status.

6
U
Restores the current line to the state it was in before the cursor entered
the line.

7
U
This helps undo the last change that was done in the file. Typing 'u' again
will re-do the change.
8
J
Joins the current line with the next one. A count joins that many lines.

9
:f
Displays the current position in the file in % and the file name, the total
number of file.

10
:f filename
Renames the current file to filename.

11
:w filename
Writes to file filename.

12
:e filename
Opens another file with filename.

13
:cd dirname
Changes the current working directory to dirname.

14
:e #
Toggles between two open files.

15
:n
In case you open multiple files using vi, use :n to go to the next file in
the series.

16
:p
In case you open multiple files using vi, use :p to go to the previous file
in the series.

17
:N
In case you open multiple files using vi, use :N to go to the previous file
in the series.
18
:r file
Reads file and inserts it after the current line.

19
:nr file
Reads file and inserts it after the line n.

Word and Character Searching


The vi editor has two kinds of searches: string and character. For a string search,
the / and ? commands are used. When you start these commands, the command just
typed will be shown on the last line of the screen, where you type the particular string
to look for.
These two commands differ only in the direction where the search takes place −
 The / command searches forwards (downwards) in the file.
 The ? command searches backwards (upwards) in the file.
The n and N commands repeat the previous search command in the same or the
opposite direction, respectively. Some characters have special meanings. These
characters must be preceded by a backslash (\) to be included as part of the search
expression.

Sr.No. Character &Description

1 ^
Searches at the beginning of the line (Use at the beginning
of a search expression).

2 .
Matches a single character.

3 *
Matches zero or more of the previous character.

4 $
End of the line (Use at the end of the search expression).
5 [
Starts a set of matching or non-matching expressions.

6 <
This is put in an expression escaped with the backslash to
find the ending or the beginning of a word.

7 >
This helps see the '<' character description above.

The character search searches within one line to find a character entered after the
command. The f and F commands search for a character on the current line
only. f searches forwards and F searches backwards and the cursor moves to the
position of the found character.
The t and T commands search for a character on the current line only, but for t, the
cursor moves to the position before the character, and T searches the line backwards
to the position after the character.

Set Commands
You can change the look and feel of your vi screen using the
following :set commands. Once you are in the command mode, type :set followed by
any of the following commands.

Sr.No. Command & Description

1 :set ic
Ignores the case when searching

2 :set ai
Sets autoindent

3 :set noai
Unsets autoindent

4 :set nu
Displays lines with line numbers on the left side
5 :set sw
Sets the width of a software tabstop. For example, you would set a
shift width of 4 with this command — :set sw = 4

6 :set ws
If wrapscan is set, and the word is not found at the bottom of the file,
it will try searching for it at the beginning

7 :set wm
If this option has a value greater than zero, the editor will automatically
"word wrap". For example, to set the wrap margin to two characters,
you would type this: :set wm = 2

8 :set ro
Changes file type to "read only"

9 :set term
Prints terminal type

10 :set bf
Discards control characters from input

Running Commands
The vi has the capability to run commands from within the editor. To run a command,
you only need to go to the command mode and type :! command.
For example, if you want to check whether a file exists before you try to save your file
with that filename, you can type :! ls and you will see the output of ls on the screen.
You can press any key (or the command's escape sequence) to return to your vi
session.

Replacing Text
The substitution command (:s/) enables you to quickly replace words or groups of
words within your files. Following is the syntax to replace text −
:s/search/replace/g
The g stands for globally. The result of this command is that all occurrences on the
cursor's line are changed.
Important Points to Note
The following points will add to your success with vi −
 You must be in command mode to use the commands. (Press Esc twice at any
time to ensure that you are in command mode.)
 You must be careful with the commands. These are case-sensitive.
 You must be in insert mode to enter text.

How to use diff command in linux

The diff command analyzes line by line and displays a list of changes between two
files. As a special case, diff compares a copy of standard input to itself. This article
describes “How to use diff command in Linux.

Futures of “diff” Command –


 Recognize the changes between one version of a file
 Compare two configuration or program files
 Create a patch file which can be applied with the Linux / Unix program patch
How does “diff” Command Works
For example, we have two files as file.txt and file1.txt. The data has been inserted into
file.txt as shown below –

I need to buy apples.


I need to run the laundry.
I need to wash the dog.
I need to get the car detailed.

file1.txt contains the data as shown below

I need to buy apples.


I need to do the laundry.
I need to wash the car.
I need to get the dog detailed.

Use the diff command to compare both the files as shown below –
linux@linux:~$ diff /home/linux/Desktop/file.txt
/home/linux/Desktop/file1.txt

The above command should give the result as shown below –

linux@linux:~$ diff /home/linux/Desktop/file.txt


/home/linux/Desktop/file1.txt
2,4c2,4
< I need to run the laundry.
< I need to wash the dog.
< I need to get the car detailed. --- > I need to do the laundry.
> I need to wash the car.
> I need to get the dog detailed.

The options of the result should be like this –


a -Added the text to file
c -Changes are made in the file
d -Deletion operation is performed
< Lines from the first file
> Lines from the second file
From the output, 2,4c2,4 means “Lines 2 through 4 in the first file needs to be changed
in order to match lines 2 through 4 in the second file”
Let’s look at another example, two text files should be like this-
file.txt

I need to go to the store.


I need to buy some apples.
When I get home, I'll wash the dog.

file1.txt

I need to go to the store.


I need to buy some apples.
Oh yeah, I also need to buy grated cheese.
When I get home, I'll wash the dog.

Use the diff command to compare both files. The command should be like this-

$ diff /home/linux/Desktop/file.txt /home/linux/Desktop/file1.txt

The above command should give the result as shown below –

2a3
> Oh yeah, I also need to buy grated cheese.

From the output, 2a3 means “After line 2 in the first file, a line needs to be added: line
3 from the second file”.
cmp Command in Linux with examples
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 .

The syntax of cmp command is quite simple to understand. If we are comparing two files
then obviously we will need their names as arguments (i.e as FILE1 & FILE2 in syntax). In
addition to this, the optional SKIP1 and SKIP2 specify the number of bytes to skip at the
beginning of each file which is zero by default and OPTION refers to the options
compatible with this command about which we will discuss later on.
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


1. If the files are not identical : the output of the above command will be :
2. $cmp file1.txt file2.txt
3. file1.txt file2.txt differ: byte 9, line 2
4.
5. /*indicating that the first mismatch found in
two files at byte 20 in second line*/
6. If the files are identical : you will see something like this on your screen:
7. $cmp file1.txt file2.txt
8. $ _
/*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.

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 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 example
comm compare two sorted files line by line and write to standard output; the lines that
are common and the lines that are unique.
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
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. – -nocheck-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
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:
//...suppressing multiple columns...//

$comm -12 file1.txt file2.txt


Apaar
Hemant

/* using -12 together suppressed both first


and second columns */
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
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

/*+ before content indicates content of


second column and ++ before content
indicates content of third column*/

The Metacharacters
Unix Shell provides various metacharacters which have special meaning while using
them in any Shell Script and causes termination of a word unless quoted.
For example, ? matches with a single character while listing files in a directory and
an * matches more than one character. Here is a list of most of the shell special
characters (also called metacharacters) −
* ? [ ] ' " \ $ ; & ( ) | ^ < > new-line space tab
A character may be quoted (i.e., made to stand for itself) by preceding it with a \.
Example
Following example shows how to print a * or a ? −

#!/bin/sh
echo Hello; Word
Upon execution, you will receive the following result −
Hello
./test.sh: line 2: Word: command not found

shell returned 127


Let us now try using a quoted character −

#!/bin/sh

echo Hello\; Word


Upon execution, you will receive the following result −
Hello; Word
The $ sign is one of the metacharacters, so it must be quoted to avoid special
handling by the shell −

#!/bin/sh

echo "I have \$1200"


Upon execution, you will receive the following result −
I have $1200
The following table lists the four forms of quoting −

Sr.No. Quoting & Description

1
Single quote
All special characters between these quotes lose their special meaning.

2
Double quote
Most special characters between these quotes lose their special
meaning with these exceptions −

 $
 `
 \$
 \'
 \"
 \\

3
Backslash
Any character immediately following the backslash loses its special
meaning.

4
Back quote
Anything in between back quotes would be treated as a command and
would be executed.

The Single Quotes


Consider an echo command that contains many special shell characters −
echo <-$1500.**>; (update?) [y|n]
Putting a backslash in front of each special character is tedious and makes the line
difficult to read −
echo \<-\$1500.\*\*\>\; \(update\?\) \[y\|n\]
There is an easy way to quote a large group of characters. Put a single quote (') at
the beginning and at the end of the string −
echo '<-$1500.**>; (update?) [y|n]'
Characters within single quotes are quoted just as if a backslash is in front of each
character. With this, the echo command displays in a proper way.
If a single quote appears within a string to be output, you should not put the whole
string within single quotes instead you should precede that using a backslash (\) as
follows −
echo 'It\'s Shell Programming

The Double Quotes


Try to execute the following shell script. This shell script makes use of single quote −
VAR=ZARA
echo '$VAR owes <-$1500.**>; [ as of (`date +%m/%d`) ]'
Upon execution, you will receive the following result −
$VAR owes <-$1500.**>; [ as of (`date +%m/%d`) ]
This is not what had to be displayed. It is obvious that single quotes prevent variable
substitution. If you want to substitute variable values and to make inverted commas
work as expected, then you would need to put your commands in double quotes as
follows −

VAR=ZARA
echo "$VAR owes <-\$1500.**>; [ as of (`date +%m/%d`) ]"
Upon execution, you will receive the following result −
ZARA owes <-$1500.**>; [ as of (07/02) ]
Double quotes take away the special meaning of all characters except the following

 $ for parameter substitution
 Backquotes for command substitution
 \$ to enable literal dollar signs
 \` to enable literal backquotes
 \" to enable embedded double quotes
 \\ to enable embedded backslashes
 All other \ characters are literal (not special)
Characters within single quotes are quoted just as if a backslash is in front of each
character. This helps the echo command display properly.
If a single quote appears within a string to be output, you should not put the whole
string within single quotes instead you should precede that using a backslash (\) as
follows −
echo 'It\'s Shell Programming'

The Backquotes
Putting any Shell command in between backquotes executes the command.
Syntax
Here is the simple syntax to put any Shell command in between backquotes −
var=`command`
Example
The date command is executed in the following example and the produced result is
stored in DATA variable.
DATE=`date`

echo "Current Date: $DATE"


Upon execution, you will receive the following result −
Current Date: Thu Jul 2 05:28:45 MST 2020

You might also like