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