cmp Command in Linux

Learn via video courses
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

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.
Sharpen Your Fundamentals with Free Learning

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.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.