Diff command in Linux 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. Diff command can also be used to compare the contents of two directories.
Syntax of diff command:
# diff <options> file1 file2
Output of diff command can be in following format:
- Normal (default)
- Context
- Unified
Symbols used in diff command output are:
- 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 output is 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 article, we will learn Linux diff command with different practical examples,
Example:1) Compare two files with diff command
Let’s assume we have two files aachen.txt and sydney.txt and following are contents 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.
Example:2) Compare two files and produce output in context format (-c)
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.
Example:3) Compare two files and produce output in unified format (-u)
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.
Example:4) Compare files with diff command using case in-sensitive (-i)
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.
Example:5) Report files are identical with diff command (-s)
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 ~]#
Example:6) Ignore White Space While Comparing files with diff command (-b)
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
Example:7) Ignore White Space at line end during comparison (-Z)
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
Example:8) Compare two directories with diff command
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
Example: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 ~]#
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.
from Linuxtechi https://ift.tt/2trciu8
0 Comments