cmp Command in Linux

Learn via video courses
Topics Covered

Overview

In the realm of Linux, the cmp command is a versatile and powerful tool, designed to compare two files byte by byte. The cmp command in Linux is primarily used to find out whether two files are identical or not. However, it can also provide information about where and how they differ.

Syntax of cmp Command in Linux

The basic syntax of the cmp command in Linux is as follows:

Where:

  • OPTIONS: These are the flags or parameters that can be used with the cmp command to modify its behavior.
  • FILE1 and FILE2: These are the two files that you want to compare.
  • SKIP1 and SKIP2: These (optional) arguments specify the number of bytes to skip at the beginning of each file.

Options in cmp Command in Linux

  1. -b or --print-bytes: This option prints differing bytes.

    For example -

    Output:

    This output indicates that the files differ at byte 10, where file1 has the byte 'A' (65 in ASCII) and file2 has the byte 'B' (66 in ASCII).

  2. -l or --verbose: This option outputs byte numbers and differing byte values.

    For example -

    Output:

    The output shows that file1 and file2 differ at byte 10, with file1 having byte value 65 (ASCII 'A') and file2 having byte value 66 (ASCII 'B').

  3. -s or --quiet: This option suppresses all normal output, making cmp silent.

    For example -

    Output:

    If the files are identical, there will be no output from cmp, and the echo command will execute, printing 'Files are identical'. If the files are not identical, the echo command will not execute.

Example Usages

  • Compare two files without any options.:

    Output:

    Explanation: This indicates that the two files differ at byte 10 on line 1.

  • Compare two files while ignoring initial bytes.:

    Output:

    Explanation: This command ignores the first 5 bytes of each file before starting the comparison.

Tips

  • To simply check if two files are identical without knowing the details of differences, use the -s option with the cmp command in Linux.

  • You can use the cmp command in Linux in conjunction with other commands and scripts for automated file comparisons.

  • The cmp command returns an exit status of 0 if the files are identical, 1 if they differ, and 2 if an error occurs. This can be useful in script handling.

Advanced Use Cases of cmp Command in Linux

  • Comparing two binary files.:

    Output:

    Explanation: This command compares two binary files and outputs the first byte where they differ along with the byte value in each file.

  • Skipping initial bytes of files for comparison.:

    Explanation: This command will skip the first 10 bytes of each file before starting the comparison. If no output is returned, it means the files are identical beyond the first 10 bytes.

  • Using cmp in a bash script to handle file comparison results.:

    Output:

    Explanation: This bash script compares two files silently and uses the exit status of the cmp command to determine the output message. If the files are identical, it prints 'Files are identical'; otherwise, it prints 'Files are different'.

Conclusion

  • The cmp command in Linux is a highly versatile tool for comparing two files byte by byte.

  • It comes with several options that allow users to customize their file comparisons.

  • While it's primarily used to check if two files are identical or not, the cmp command also provides details on where and how the files differ.

  • The cmp command can be combined with other commands and used within scripts for automated tasks.