The Wayback Machine - https://web.archive.org/web/20240717043915/https://www.geeksforgeeks.org/cmp-command-in-linux-with-examples/
Open In App

cmp Command in Linux with examples

Last Updated : 25 May, 2022
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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 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 :
$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*/
  1. 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. 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.

?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L


Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
comm command in Linux with examples
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 so
5 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
df Command in Linux with examples
There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e df command that displays the amount of disk space available on the fi
8 min read
help Command in Linux with examples
If you are new to LINUX operating system and having trouble dealing with the command-line utilities provided by LINUX then you really need to know first of all about the help command which as its name says help you to learn about any built-in command. help command as told before just displays information about shell built-in commands. Here's the sy
3 min read
fgrep command in Linux with examples
The fgrep filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings which contain lots of regular expression metacharacters, such as "^", "$", etc. Syntax: fgrep [options] [ -e pattern_list] [pattern] [file] Options with Description
3 min read
rcp Command in Linux with examples
There comes a time while using LINUX when there is a need to copy some information stored in a file to another computer. This can be done simply using rcp command line utility . Obviously there exists some other methods to complete the above mentioned task which are more secure (like scp or rsync) but this command lets you do this in the simple way
4 min read
three90RightbarBannerImg