cmp command in Linux is used to compare the content of two files byte by byte and reports the first byte and line number where they differ.

Syntax of cmp command

cmp [options] file1 file2

Here is a list and contents of text files, used for cmp command in this tutorial.

Compare two files

cmp command is used to compare the contents of two files byte by byte. To compare the contents of two files byte by byte use the following command.

cmp file1 file2

Comparing two files using cmp command where the contents of two files are different from the first line.

cmp file1 file2

Comparing two files using cmp command where the contents of two files are identical (no difference). If no difference is found, cmp command displays nothing and simply returns the prompt.

cmp file1 file2 both files are identical

Comparing two files using cmp command where the contents of two files are identical in first two lines and different from third line.

cmp file1 file2 two lines are indentical

 

cmp -l file1 file2

cmp -l flle1 file2

Display differing bytes

Use -b option along with cmp command to display the differing bytes in the output, when two files are compared.

cmp -b file1 file2

cmp -b file1 file2

Limit the number of bytes to compare

Use -n option along with cmp command if you do not wants to compare the entire contents of two files and only want to compare first few bytes of two files.

cmp -n INT file1 file2

cmp -n file1 file2

cmp -n 30 1.txt 4.txt
// since no message in output, indicating files are identical for first bytes.

cmp -n f1 f2

Skipping a particular number of bytes from both files

cmp -i INT file1 file2

cmp -i INT file1 file2

cmp -i INT1:INT2 file1 file2

cmp -i INT1 INT2 file1 file2

Sharing is Caring
Scroll to Top