- 1Linux Topics Index
- 2How to Create a File in Linux
- 3How to Rename a File in Linux
- 4How to Copy a File in Linux
- 5How to Move a File in Linux
- 6How to Delete a File in Linux
- 7How to Create a Directory in Linux
- 8How to Delete a Directory in Linux
- 9How to Copy Directories Recursively in Linux
- 10Delete Directories Recursively in Linux
- 11How to View Hidden Files in Linux
- 12How to Create a Hidden File in Linux
- 13How to Create a Hidden Directory in Linux
- 14How to Find Files in Linux Using find Command
- 15Find Files in Linux Using locate Command
- 16How to View File Contents Using cat in Linux
- 17How to View File Contents Using less in Linux
- 18View File Contents Using the more Command in Linux
- 19Compare Files Using diff Command in Linux
- 20Compare Files in Linux Using cmp Command
- 21Check File Type in Linux with file Command
- 22Create Symbolic Links with ln -s in Linux
- 23How to Archive Files Using tar Command in Linux
- 24Compress Files with gzip in Linux – Beginner Tutorial
- 25How to Compress Files Using bzip2 in Linux
- 26Compress Files in Linux Using zip Command
- 27Extract Compressed Files using tar in Linux
- 28Extract ZIP Files on Linux with unzip
- 29How to Extract .gz Files using gunzip in Linux

- 1Linux Topics Index
- 2How to Create a File in Linux
- 3How to Rename a File in Linux
- 4How to Copy a File in Linux
- 5How to Move a File in Linux
- 6How to Delete a File in Linux
- 7How to Create a Directory in Linux
- 8How to Delete a Directory in Linux
- 9How to Copy Directories Recursively in Linux
- 10Delete Directories Recursively in Linux
- 11How to View Hidden Files in Linux
- 12How to Create a Hidden File in Linux
- 13How to Create a Hidden Directory in Linux
- 14How to Find Files in Linux Using find Command
- 15Find Files in Linux Using locate Command
- 16How to View File Contents Using cat in Linux
- 17How to View File Contents Using less in Linux
- 18View File Contents Using the more Command in Linux
- 19Compare Files Using diff Command in Linux
- 20Compare Files in Linux Using cmp Command
- 21Check File Type in Linux with file Command
- 22Create Symbolic Links with ln -s in Linux
- 23How to Archive Files Using tar Command in Linux
- 24Compress Files with gzip in Linux – Beginner Tutorial
- 25How to Compress Files Using bzip2 in Linux
- 26Compress Files in Linux Using zip Command
- 27Extract Compressed Files using tar in Linux
- 28Extract ZIP Files on Linux with unzip
- 29How to Extract .gz Files using gunzip in Linux

- 1How to Add a New User in Linux
- 2Modify Existing User in Linux
- 3Delete a User in Linux — Step-by-Step for Beginners
- 4Create a User Group in Linux - Step-by-Step Tutorial
- 5Linux: Add User to Group
- 6How to Remove a User from a Group in Linux
- 7Delete a User Group in Linux - Beginner Friendly Tutorial
- 8Linux su Command Tutorial – Switch Users Easily
- 9Linux sudo Command - Execute as Another User
- 10Change a User's Password in Linux Using passwd
- 11Set Password Expiry in Linux using chage
- 12Lock a User Account in Linux
- 13How to Unlock a User Account in Linux
- 14Configure User Login Shell in Linux
- 15How to Configure User Environment Variables in Linux
- 16Edit .bashrc and .profile in Linux - User Startup Files Tutorial
How to Compare Files Using cmp
Next Topic ⮕Check File Type in Linux with file Command
How to Compare Files Using cmp
Hey there! 👋 Welcome to this beginner-friendly Linux tutorial from ProgramGuru.org. Today, we're going to learn how to compare two files using a simple but powerful command called cmp
.
If you're wondering whether two files are identical, or if there's even a small difference between them, cmp
is the tool you need. Let's break this down slowly and clearly so that anyone—even if you're new to Linux—can follow along!
🔧 What is cmp
?
cmp
stands for "compare." It compares two files byte by byte and tells you the first point where they differ. If the files are identical, it says nothing. That’s right—it stays quiet. Silence means success!
📌 Basic Syntax
cmp file1 file2
Let’s try it out with some examples!
📁 Step 1: Create Two Files
First, let’s create two simple text files for comparison:
echo "Hello world!" > fileA.txt
echo "Hello world!" > fileB.txt
Now let’s compare them:
cmp fileA.txt fileB.txt
Output:
(no output — which means they are exactly the same!)
✏️ Step 2: Change One File
Now, let’s make a small change to fileB.txt
:
echo "Hello world!!!" > fileB.txt
Try the compare command again:
cmp fileA.txt fileB.txt
Output:
fileA.txt fileB.txt differ: byte 13, line 1
This tells us that the difference starts at byte 13, on line 1. Simple and precise!
🔍 Use -l
to Show Byte Differences
If you want to see what exactly is different in terms of byte values, use the -l
option:
cmp -l fileA.txt fileB.txt
Output:
13 41 41
14 0 41
15 0 41
This tells you the byte position and the differing byte values in octal.
📄 Use -s
for Silent Mode
If you just want to know whether the files differ, and don’t care where, use the silent flag:
cmp -s fileA.txt fileB.txt
This will give no output if the files are the same, and exit silently. But you can check the result using:
echo $?
Output:
0 (means files are the same)
1 (means files are different)
✅ Summary
cmp file1 file2
— Basic comparisoncmp -l
— Show byte-by-byte differencescmp -s
— Silent mode for scripting
That’s it! 🎉 You now know how to compare files in Linux using cmp
. This is especially useful when working with scripts, config files, or debugging subtle file differences. Keep experimenting and you’ll get the hang of it!
See you in the next tutorial on ProgramGuru.org!