Linux/Unix – Comparing, finding differences between files…

Comparing 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 `’, type:

$ diff manuscript.old


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 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 


About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on March 15, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: