
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Questions & Answers
- Fastest Way to multiply two Numbers
- How to know if two arrays have the same values in JavaScript?
- Check if two String objects have the same value in C#
- How to search contents of multiple pdf files on Linux?
- Simple way to find if two different lists contain exactly the same elements in Java
- The best way to compress and extract files using the tar command on linux
- Accessing The Unix/Linux password database (pwd)
- How to find the files in Linux that have been changed in the last 24 hours?
- Difference between Linux and Unix
- Set two Matplotlib imshow plots to have the same colormap scale
- How to make two histograms have the same bin width in Matplotlib?
- How to compare two sorted files line by line in the Linux system?
- Merge contents of two files into a third file using C
- Fastest way to count number of rows in MySQL table?
- Fastest way to store easily editable config data in PHP?
Advertisements