Linux/Unix – Comparing, finding differences between files…
There are a number of tools for comparing the contents of files in different ways; these recipes show how to use some of them. These tools are especially useful for comparing passages of text in files, but that’s not the only way you can use them.
Determining Whether Two Files Differ
Use cmp to determine whether or not two text files differ. It takes the names of two files as arguments, and if the files contain the same data, cmp outputs nothing. If, however, the files differ, cmp outputs the byte position and line number in the files where the first difference occurs.
To determine whether the files `master’ and `backup’ differ, type:
$ cmp master backup
Finding the Differences between Files
Use diff to compare two files and output a difference report (sometimes called a “diff”) containing the text that differs between two files. The difference report is formatted so that other tools can use it to make a file identical to the one it was compared with. To compare two files and output a difference report, give their names as arguments to diff.
To compare the files `manuscript.old’ and `manuscript.new’, type:
$ diff manuscript.old manuscript.new
The difference report is output to standard output; to save it to a file, redirect the output to the file to save to:
$ diff manuscript.old manuscript.new 62; manuscript.diff
In the preceding example, the difference report is saved to a file called `manuscript.diff’. The difference report is meant to be used with commands such as patch, in order to apply the differences to a file.
To better see the difference between two files, use sdiff instead of diff; instead of giving a difference report, it outputs the files in two columns, side by side, separated by spaces. Lines that differ in the files are separated by `|’; lines that appear only in the first file end with a `<‘, and lines that appear only in the second file are preceded with a `>’.
To peruse the files `laurel’ and `hardy’ side by side on the screen, with any differences indicated between columns, type:
$ sdiff laurel hardy | less
To output the difference between three separate files, use diff3. To output a difference report for files `larry’, `curly’, and `moe’, and output it in a file called `stooges’, type:
$ diff3 larry curly moe 62; stooges