Fastest way to tell if two files have the same contents in Unix/Linux


Let’s say that we have two files inside a directory called dir1, and at first both these files are different. Different in the sense that the text they contain isn’t the same.

The files in the folder −

immukul@192 dir1 % ls -ltr
total 16
-rw-r--r-- 1 immukul staff 7 Jul 7 10:37 2.txt
-rw-r--r-- 1 immukul staff 8 Jul 8 19:05 3.txt

The contents inside the first file(2.txt) looks something like this −

immukul@192 dir1 % cat 2.txt
orange

The contents inside the second file(2.txt) looks something like this −

immukul@192 dir1 % cat 3.txt
uorange

We can easily make use of the diff command to check if they have something different. Consider the command shown below −

diff 2.txt 3.txt

Output

1c1
< orange
---
> uorange

But in case where the contents of the file are exactly the same, then the diff command won’t return any output.

In that case it is recommended to make use of the cmp command. The cmp command is a Linux utility command that is used to compare two files.

Command

cmp --silent 2.txt 3.txt || echo "Difference in Files"

Output

immukul@192 dir1 % cmp --silent 2.txt 3.txt || echo "Difference in Files"
Difference in Files

Updated on: 29-Jul-2021

537 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements