Are looking for an easy method to compare two files and directories in Linux from command line?
The following examples on this page will show you how to compare files and directories in Linux with diff command.
Diff command is used to compare the content of two files line by line and if the difference is found then it will also list differences along with line numbers. Apart from this it can also be used to compare the contents of two directories.
Diff command plays an important in shell script where we want to run command based on the comparison of two files.
diff Command Syntax
$ diff <options> file1 file2
Output of diff command can be in following format:
- Normal (default)
- Context
- Unified
Different symbols of diff command output:
- a -> it indicates that something has been added
- c -> it indicates that some text has been changed
- d -> it indicates some text has been deleted
By default, diff command displays the output in normal format, it means when contents of two files are identical then it will not produce any output but we will get the prompt.
In this post, we will discuss diff command with practical examples.
1) Compare two files with diff command
Let’s assume we have two files aachen.txt and sydney.txt and following is the content of these files,
[root@linuxtechi ~]# cat aachen.txt Linux Rocks in the open source world, followings are the linux distributions: CentOS Red Hat Ubuntu Debian Linux Mint OpenSUSE Fedora [root@linuxtechi ~]# cat sydney.txt Linux Rocks in the open source world, followings are the linux distributions: CentOS Red Hat Ubuntu Debian Linux Mint OpenSUSE Fedora [root@linuxtechi ~]#
Let’s compare the content of these files using diff command,
[root@linuxtechi ~]# diff aachen.txt sydney.txt [root@linuxtechi ~]#
Above output confirms that both the files are identical. Let’s make some changes in aachen.txt, re-write open source as “open-source”.
Now re-run the diff command,
[root@linuxtechi ~]# diff aachen.txt sydney.txt
Output,
In the output, 1c1 indicates that 1st line of the first file needs to be changed to make both the files identical.
Let’s roll-back the above change and add a new line “Arch Linux” in sydeny.txt at end of file and run the diff command again,
Output of diff command “7a8” indicates that after the 7th line in the first file, we need to add another line to match 8th line of the second file.
Let’s remove “Arch Linux” and “Fedora” from sydney.txt and then try to compare these files,
Output of diff command “7d6” indicates that we need to delete 7th line in the first file to sync with the second file at line number 6.
2) Diff Command Output in Context Format
Use ‘-c’ option in diff command to produce the diff command output in context format. Example is shown below,
[root@linuxtechi ~]# diff -c aachen.txt sydney.txt
First two lines represent file names along with their modification date and time and three asterisk symbol (“***“) indicates the first file and three hyphen symbol (“—“) indicates the second file.
Only single hyphen “-“ indicates that line needs to removed and plus symbol “+” indicates that line needs to added in the file. If any line which doesn’t require any change then it is prefixed by two spaces.
3) Diff Command Output in Unified Format
Use ‘-u’ option in diff command if you want to produce its output in unified format, example is illustrated below,
[root@linuxtechi ~]# diff -u aachen.txt sydney.txt
Above output is somewhat similar to context format but unified format displays the output in concise way, here first two lines indicates the file names along with their modification date and time..
Three hyphens (“—“) represents the first file and three plus symbol (“+++”) represents second file, after two @ sign “-1,7” indicates the lines from 1st file and “+1,7” represents lines range from 2nd file. Plus, symbol (+Core OS) indicates that this line needs to be added in first file and symbol hyphen (-Fedora) indicates that this line needs to be removed from first file to make it identical to 2nd file.
4) Ignore Case Sensitive While Comparing Files
By default, diff command is case sensitive and if you want to ignore case sensitive in diff command then use ‘-i‘ option, example is shown below,
Let’s assume we have the following content of two files
Run the diff command without any option,
[root@linuxtechi ~]# diff aachen.txt sydney.txt 5,6c5,6 < Ubuntu debian < Linux mint --- > Ubuntu Debian > Linux Mint [root@linuxtechi ~]#
Now run the diff command with -i option,
[root@linuxtechi ~]# diff -i aachen.txt sydney.txt [root@linuxtechi ~]#
As we can see in above output, diff command has ignored case sensitive feature when we use -i option.
5) Report Files are Identical with diff Command
There can be some situations where we are comparing the files using diff command and if content is identical then we want display the message that file are identical, this can be accomplished by passing ‘-s’ option in diff command,
[root@linuxtechi ~]# cat filex.txt DevOPs Engineer Cloud Stack Engineer Linux System Admin Monitoring Team [root@linuxtechi ~]# cat filey.txt DevOPs Engineer Cloud Stack Engineer Linux System Admin Monitoring Team [root@linuxtechi ~]# diff -s filex.txt filey.txt Files filex.txt and filey.txt are identical [root@linuxtechi ~]#
6) Ignore White Space While Comparing Files
Let’s assume we have two files filea.txt and fileb.txt where white spaces are not consistent, so if we use diff command without any option in that it will show difference of white space even though contents are identical, so in such cases we can ignore inconsistent white spaces in diff command by using -b option , example is shown below,
[root@linuxtechi ~]# cat filea.txt Hi, LinuxTechi is a Linux based blog. It contains linux HowTo's and Tips & Tricks. [root@linuxtechi ~]# cat fileb.txt Hi, LinuxTechi is a Linux based blog. It contains linux HowTo's and Tips & Tricks. [root@linuxtechi ~]#
Compare the files without any option with diff command,
[root@linuxtechi ~]# diff filea.txt fileb.txt 1c1 < Hi, LinuxTechi is a Linux based blog. It contains linux HowTo's and Tips & Tricks. --- > Hi, LinuxTechi is a Linux based blog. It contains linux HowTo's and Tips & Tricks. [root@linuxtechi ~]#
Now use -b option in above diff command
[root@linuxtechi ~]# diff -b filea.txt fileb.txt [root@linuxtechi ~]#
If you want to ignore all the white spaces during comparison using diff command, then use “-w” option
[root@linuxtechi ~]# diff -w filea.txt fileb.txt
7) Ignore White Space at line end During Comparison
If you want to Ignore trialing space at the line end during the comparison, then use “-Z” option
[root@linuxtechi ~]# diff -Z filea.txt fileb.txt
Use “-B” in diff command to ignore all blank lines during the comparison,
[root@linuxtechi ~]# diff -B filea.txt fileb.txt
8) Compare Two Directories
Let’s assume we have two directories with the name lab01 and lab02, these have following files and sub-directories
[root@linuxtechi ~]# ls -l lab01/ total 0 -rw-r--r--. 1 root root 0 Feb 1 17:49 filea.txt -rw-r--r--. 1 root root 0 Feb 1 17:49 fileb.txt -rw-r--r--. 1 root root 0 Feb 1 17:49 filec.txt -rw-r--r--. 1 root root 0 Feb 1 17:49 filed.txt drwxr-xr-x. 2 root root 23 Feb 1 17:50 sitea [root@linuxtechi ~]# ls -l lab02/ total 0 -rw-r--r--. 1 root root 0 Feb 1 17:49 filea.txt -rw-r--r--. 1 root root 0 Feb 1 17:49 fileb.txt -rw-r--r--. 1 root root 0 Feb 1 17:49 filec.txt -rw-r--r--. 1 root root 0 Feb 1 17:49 filed.txt drwxr-xr-x. 2 root root 23 Feb 1 17:50 sitea [root@linuxtechi ~]#
Let’s compare these two directories using below diff command,
[root@linuxtechi ~]# diff -qr lab01/ lab02/ [root@linuxtechi ~]#
Where:
- -q -> it instructs diff command to report only when files are different
- -r -> it instructs diff command to look for difference in sub-directories recursively
As we get blank output, it means both the directories are identical. Let’s delete filed.txt from lab02 and then try to compare,
[root@linuxtechi ~]# cd lab02/ [root@linuxtechi lab02]# rm -f filed.txt [root@linuxtechi lab02]# cd [root@linuxtechi ~]# diff -qr lab01/ lab02/ Only in lab01/: filed.txt [root@linuxtechi ~]#
Above output says that filed.txt is only present in lab01 directory
9) Redirecting diff Command Output to a File
Diff command output can be redirected to a file by using symbol “>”. It becomes very useful in shell scripts where we want to perform a task only if output of diff command contains differences.
[root@linuxtechi ~]# diff aachen.txt sydney.txt 9,10d8 < SLES < Puppy Linux [root@linuxtechi ~]# diff aachen.txt sydney.txt > diff.txt [root@linuxtechi ~]# cat diff.txt 9,10d8 < SLES < Puppy Linux [root@linuxtechi ~]#
Additional example
Compare Two Files on Terminal Side by Side
When we use ‘-y‘ option while comparing two files using diff command then it shows both the files on terminal side by side.
[root@linuxtechi ~]# diff -y /etc/fstab /tmp/fstab
output
Above output clearly shows difference of two files.
That’s all from this article, I hope now you have better understanding of diff command by going through above examples. Please do not hesitate to share your feedback and comments.
Also Read : 16 Echo Command Examples in Linux
Also Read : 8 Head Command Examples in Linux
A favorite of mine is ‘diff -y’ – it shows the two file outputs side by side in the terminal.