The Linux diff command analyses a file line by line and gives an output of a list of changes made between two files.
The command diff is short for difference, which in essence gives the differences between two files.
Unlike the cmp
(compare) and comm
commands, diff highlights the specific line that needs to be changed to match the other files.
One thing Linux administrators cannot forget is that diff uses symbols and instructions to make it more effective.
The specific symbols instruct the command to change the file in a particular way to match the second.
Special Symbols in the diff command output:
- a: add
- c: change
- d: delete
Linux diff Command Syntax
diff [options] File1 File2
Let’s have a look at diff command example usages.
Case 1: Change
Assume we have two files: x.txt and y.txt
x.txt contains the following content.
California
Miami
Ohio
Kansas
Texas
y.txt contains the following content.
California
Nevada
Georgia
Kansas
Texas
Now, use the diff command to make the comparison between the two files as shown.
$ diff x.txt y.txt
Sample output
Let’s examine the output in more detail.
The 2,3c2,3
line implies that lines 2 and 3 from the first file need to be changed in order to match lines 2 and 3 from the second text file.
diff Command Output Symbols explained
Let’s briefly look at the various symbols encountered in the output.
- The less than symbol indicates lines from the first line
- The greater than symbol indicates lines from the second line
a - Denotes that text was added to the file
c - Denotes that changes were made in the file
d - Indicates that the line was expunged or deleted
Let’s take a look at another example.
Case 2: Adding
We have two text files:
The file file 1.txt has the following content.
Linux is a great operating system
It's free and opensource
It's light and stable
I would highly recommend it
file2.txt has the following content.
Linux is a great operating system
It's free and opensource
It's light and stable
Oh ! It can be installed on almost any PC hardware
I would highly recommend it
Use the diff command to compare both files.
$ diff file1.txt file2.txt
Sample output
From the output above, 3a4
implies that after line 3 from the first file, another line needs to be added to match line 4 from the second file.
Case 3: Deletion
Consider two files as below.
file3.txt with the following content.
Apples
Oranges
Mangoes
Peaches
Bananas
Grapes
And file4.txt which has the following content.
Apples
Oranges
Mangoes
Bananas
Grapes
Again, use the diff command to compare both files.
$ diff file3.txt file4.txt
Sample output
Here the output 4d3 means that delete the 4th line from the first file to sync up with the second file at line number 3.
Conclusion
Linux diff command is a simple utility to compare two files. It’s useful in checking for the changes between two versions of a text file.
Thank you. That was most helpful. I suspect that there is much more to know about ‘diff’, but I now have the basic understanding that would allow me to learn how to use more advance features.
One another matter, I have written a small bash utility as a wrap-around to ‘ls’. It’s called ‘lsi’ (for ‘ls Informative’). This is to enable me to search a directory tree, for example, that beneath ${HOME}/.mozilla/firefox to find, with the help of ‘sort’ which file was updated most recently, or else which is the largest. As an example:
$ cd ${HOME}/.mozilla/firefox/manjaro.default
$ find . -type f -exec lsi {} \; | sort -rn -k 2 >~/dsave/search/foundFireFoxFiles_21sep19
$ head -5 ~/dsave/search/foundFireFoxFiles_21sep19
20190922005104 20971520 places.sqlite
20190920084437 15499264 3870112724rsegmnoittet-es.sqlite
20190921213758 12976128 favicons.sqlite
20190922004341 12582912 webappsstore.sqlite
20190128075056 4540621 61
From this we know that the largest file within ${HOME}/.mozilla/firefox/manjaro.default/ is places.sqlite with a size of 4,540,621 bytes. It was last updated at 00:51:05 in the early morning of 22 September 2019.
I think it’s a shame that the Linux ‘ls’ command does not have the ability, with command-line parameters, to do what my ‘lsi’ script, used in the above ‘find’ command, does.
The script ‘lsi’, used above, is:
#!/bin/bash
# lsi = LiSt (display) useful file Information: the update date/time,
# the file size and the (base) file name.
examinedFile=$1
examinedFileBasename=`basename $examinedFile`
# Array has to be used, because, ‘cut’ will not display the fields in any
# order other than the original order output by ‘ls -l’:
allFileDataFields=(`ls -l –time-style=+%Y%m%d%H%M%S “$examinedFile”`)
echo “${allFileDataFields[5]} ${allFileDataFields[4]} ${examinedFileBasename}”