Finding Differences in Files with diff

Finding Differences in Files with diff

In addition to using cmp to find out how files differ, you can also use diff. This command tells you specifically where two files differ, not just that they differ and at which point the differences start (see Code Listing 6.15).

Code Listing 6.15. diff tells you all you ever wanted to know about the differences between two files but not in an easily readable manner.

[ejr@hobbes manipulate]$ diff dearliza    dearhenry 2,3c2,3 < Dear Liza, < There's a hole in my bucket, dear Liza,    dear Liza, dear Liza. > Dear Henry, > Please fix it dear Henry, dear Henry,    dear 'Henry. 5,6c5,6 < Henry < > Liza > PS, you forgot your toolbox last time. [ejr@hobbes manipulate]$ 

To find differences with diff:

  • diff dearliza dearhenry

    Type diff, followed by both filenames. The diff output, as in Code Listing 6.15, shows lines that appear only in one file or the other. The lines from file 1 are indicated with <, while the lines from file 2 are indicated with >.

    Above each line are the affected line numbers in the first file, then d, a, or c, then the corresponding line numbers from the second file:

    • d means that the line would have to be deleted from file 1 to make it match file 2.

    • a means that text would have to be added to file 1 to match file 2.

    • c means that changes would have to be made to the line for the two files to match.


  • If you're comparing email messages or other less-structured documents, you might consider adding the flags -i (case insensitive), -b (ignore blank lines), or even -w (ignore spaces and tabs) to avoid cluttering your results with unimportant differences. For example, you could use diff -ibw file1 file2 to find all differences between two files except those involving blank lines, spaces, tabs, or lowercase/uppercase letters.

Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: