Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App

cmp Command in Linux with examples

Last Updated : 03 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with Linux or UNIX systems, you may often need to compare files to check for differences. The 'cmp' command is a powerful tool that allows you to compare two files byte by byte, making it a crucial utility for developers, system administrators, and anyone needing precise file comparisons.

What is the 'cmp' Command?

'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 a 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]]] 
  • 'FILE1' and 'FILE2': The names of the files you want to compare.
  • 'SKIP1' and 'SKIP2': Optional arguments specifying the number of bytes to skip at the beginning of each file.
  • 'OPTION': Various command-line options to modify the behavior of the cmp command.

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 Command Example in Linux

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*/

2. 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 the '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: 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.

  • Exit status 0: Files are identical.
  • Exit status 1: Files are different.
  • Exit status 2: An error occurred.
//...cmp command used with -s option...//  $cmp -s file1.txt file.txt  1 

This compares only the first 50 bytes of both files, and since there is no output, they are identical within those 50 bytes.

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.

cmp --version

9. '--help' option

This displays a help message and exits.

cmp --help

Conclusion

The 'cmp' command in Linux is an efficient tool for comparing files byte by byte. Its simplicity, combined with a range of options, makes it suitable for various file comparison needs, from basic text files to complex binaries. 'cmp' users can easily identify differences, validate file integrity, and troubleshoot issues related to file modifications.



K

Kartik Thakral
Improve
Article Tags :
  • Misc
  • Linux-Unix
  • linux-command
  • Linux-file-commands
Practice Tags :
  • Misc

Similar Reads

    chage command in Linux with examples
    The 'chage' command in Linux is a powerful tool used to manage user password expiry and account aging information. It is particularly useful in environments where user access needs to be controlled over time, such as when login access is time-bound or when it’s necessary to enforce regular password
    4 min read
    chattr and lsattr commands in Linux with examples
    In the world of Linux, managing file permissions and attributes is crucial for maintaining a secure and organized system. Two powerful commands that help control file and directory attributes are 'chattr' and 'lsattr'. These commands are essential for administrators and advanced users who need to pr
    8 min read
    chfn command in Linux with examples
    'chfn' command in Linux allows you to change a user's name and other details easily. 'chfn' stands for Change finger. Basically, it is used to modify your finger information on Linux system. This information is generally stored in the file '/etc/passwd' that includes user's original name, work phone
    3 min read
    chgrp command in Linux with Examples
    The `chgrp` command in Linux is used to change the group ownership of a file or directory. All files in Linux belong to an owner and a group. You can set the owner by using “chown” command, and the group by the "chgrp" command. Syntax of `chgrp` command in Linuxchgrp [OPTION]… GROUP FILE… chgrp [OPT
    3 min read
    chkconfig command in Linux with Examples
    'chkconfig' command is used to list all available services and view or update their run level settings. In simple words it is used to list current startup information of services or any particular service, update runlevel settings of service, and adding or removing service from management. Here we’l
    3 min read
    How to Make Script Executable in Linux | chmod Command
    In Unix operating systems, the chmod command is used to change the access mode of a file. The name is an abbreviation of change mode. Which states that every file and directory has a set of permissions that control the permissions like who can read, write or execute the file. In this the permissions
    7 min read
    How to Change File Ownership in Linux | chown Command
    In the Linux operating system, file ownership is a crucial aspect of system security and user management. The chown command, short for "change owner," is a powerful tool that allows users to change owner of file in Linux. This command is particularly useful in scenarios where administrators need to
    9 min read
    chpasswd command in Linux with examples
    chpasswd command is used to change password although passwd command can also do same. But it changes the password of one user at a time so for multiple users chpasswd is used. Below figure shows the use of passwd command. Using passwd we are changing the password of the guest user. Here first you ha
    2 min read
    chroot command in Linux with examples
    The 'chroot' command in Linux and Unix-like systems is used to change the root directory for the current running process and its child processes. This change creates a restricted environment, often referred to as a "chroot jail" or "jailed directory," where processes are limited to accessing only fi
    3 min read
    chrt command in Linux with examples
    'chrt' command in Linux is known for manipulating the real-time attributes of a process. It sets or retrieves the real-time scheduling attributes of an existing PID, or runs the command with the given attributes. 'chrt' can help optimize process management in a Linux system, especially for applicati
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences