linux / Unit How to find the difference between two files? diff command

diff command is to find the difference between the two files or directory of files.

The example are based on the below file content.

bash$ cat adir/1.txt
cpan
perl
php
mozilla
Linux
Unix
VMWare
Oracle Database

bash$ cat adir/2.txt
CPAN
perl
php
mozilla
Linux
Unix
VMWare
Oracle Database
Apache
MySQL
Internet Explorer

bash3$ cat bdir/1.txt
cpan
perl
php
mozilla
Linux
Unix
VMWare
Oracle Database

bash$ cat bdir/2.txt
CPAN
perl
php
mozilla
Linux
Unix
VMWare
Oracle Database
Apache
MySQL
Internet Explorer
J2EE

1. To find difference between two files.

bash$ diff 1.txt 2.txt
2c2
< cpan
---
> CPAN
5c5
< mozilla
---
> mozilla
9c9,13
< Oracle Database
---
> Oracle Database
>
> Apache
> MySQL
> Internet Explorer

< - the line in first file removed from the second file.
> - new line or change included in the second file.

2. Ignore the whitespace changes

-b option ignores the number of white space changes.

bash-2.03$ diff -b 1.txt 2.txt
2c2
< cpan
---
> CPAN
9a10,13
>
> Apache
> MySQL
> Internet Explorer

so here the white space between Oracle database has been ignored.

3. Ignore the number of blank line changes.

-B option ignores the number of blank lines while checking for difference. Now include the blank line betwen Linux and Unix.

bash$ diff -B 1.txt 2.txt
2c2
< cpan
---
> CPAN
5c5
< mozilla
---
> mozilla
9c10,14
< Oracle Database
---
> Oracle Database
>
> Apache
> MySQL
> Internet Explorer

The line difference between Linux and Unix is ignored here.

4. Ignore the case while checking for difference.

-i option helps to ignore the case while checking the difference.

bash$ diff -i 1.txt 2.txt
5c5
< mozilla
---
> mozilla
6a7
>
9c10,14
< Oracle Database
---
> Oracle Database
>
> Apache
> MySQL
> Internet Explorer

The CPAN and cpan difference line ignored here.

5. To output in ed script format.

If you want the output in ed script format use -e option.

bash$ diff -e 1.txt 2.txt
9c
Oracle Database


Apache
MySQL
Internet Explorer
.
6a

.
5c
mozilla
.
2c
CPAN
.

6. To output difference in RCS format.

To output the difference in RCS format use -n option

bash-2.03$ diff -n 1.txt 2.txt
d2 1
a2 1
CPAN
d5 1
a5 1
mozilla
a6 1

d9 1
a9 5
Oracle Database

Apache
MySQL
Internet Explorer

7. To View difference in side by side.

-y option results the difference side-by-side.

bash$ diff -y 1.txt 2.txt

cpan | CPAN
perl perl
php php
mozilla | mozilla
Linux Linux
>
Unix Unix
VMWare VMWare
Oracle Database | Oracle Database
>
> Apache
> MySQL
> Internet Explorer

8. To view difference between two directory files.

It is same as file diff command there is no difference.

bash-2.03$ diff adir bdir
diff adir/2.txt bdir/2.txt
14a15,16
>
> J2EE

9. To exclude files using pattern.

The -x option provides way to exclude the files which match the pattern.

bash-2.03$ diff -x 2.* adir bdir

Exclude the file name 2 with any extension.

other way to exclude is to keep the pattern in file and use with -X option. It exclude files which match any of the pattern in the file.

10. Recursively find the difference between files in the directory and its sub-directories.

-r option with diff does this.



You may be interest in
 In linux / unix - find files/directories which modified today
 Linux / Unix run only one instance of the script
 Top 15 commands to know in Linux /Unix
 Linux / Unix find command with various options
 Linux / Unix Window Split in Vim
 Linux / Unix : who is currently in the system? What they are doing?
 Linux / Unix : how to see the hidden files?
 Unix or Linux How to get the previously executed command and repeat it ?
 Unix / Linux : How to get the unix version number?
 Linux / Unix : How to check the used and available space?
 Linux / Unix : How to remove ^M character in the files created in windows system?
 Linux / Unix - How to change the password?
 Linux / Unix : How to count the number of lines, words and characters in the file?
 Linux / Unix : How to see our own uid, gid and other's?
 Linux / Unix : How to get the list of users who don't have password?
 Sample C program and how to execute c program in linux / unix box?
 Linux / Unix - Display the nonprinting characters - cat -v
 Unix / Linux - Job Interview Question Papers for MNC's
 Linux / Unix : How to remove files and sub directories in the particular directory Recursively?
 Linux / Unix : What command used to see the login and logout information?
 Linux / Unix : How to get the MAC address?
 Linux / Unix : How to make the network interface as up / down?
 Linux / Unix : sqlplus in command prompt
 Linux / Unix : Difference betwen kill, pkill, kill -9 , pkill -9 command
 Linux / Unix Difference between who, whoami and who am i?
 Linux / Unix / vim : How to make the process to run in background and bring back to foreground?
 Linux/Unix FACL - File Access Control List
 Linux/Unix : Input/Ouput redirection
 How do you remove the duplicate items in the file in linux?
 How to make the command run in background linux?
 How to search pattern exist in the input files in linux / unix
 Linux : interview questions
 In linux and solaris unix, we have a command called "file" to get the type of file.
 Linux / Unix - cut command
 How to check particular php extension installed in linux?
 Javascript Unit Test Using FireUnit
 Widely used Network Commands
 Easytrieve -To find the records using two files
 Compare Two VSAM Files
 Easytrieve: Merge two files
 Snort - Network Intrusion Prevention and Detection System
 How to merge two hash?
 How to add two field in easytrieve / eztrieve / ezytrieve
 How to concatnate / join two or more cells/columns in excel?
 Javascript How to concatenate or implode two arrays in javascipt?
 How to concatenate or join two lines in vim editor?
 COMPARE TWO FILES AND WRITE THE RECORDS THAT WERE NOT PRESENT IN FIRST FILE.
 Most frequently used commands in mainframe sort
 How to hide and show files in windows using Attrib command?
 Vim - execute command inside vim
 Perl : Find and Replace in the multiple files using the Perl single line command
 RUNSTATS Commands in DB2
 Windows: How to see the system information in command prompt?
 CVS Commands Quick Reference
 DB2 : FREE command?
 Reverse search command
 use & symbol at the end of the command
 How to access the command line parameters/arguments in perl?
 find the file and delete it using unix single command throws "find: incomplete statement error" when i run inside perl Program?
 use \\ in the end of find command
 Unix: tee command and its usage
 How to find which php is used or where the php located in command line?