Next 16: Programming Up 15: File Utilities Prev 15.H: Sort Files with sort Contents


Specialized

§ 15.I: Comparing Files with diff and cmp


The diff command is used to compare two text files. So, to compare sample.doc with vi.doc

If you only changed the name and date without moving any lines, your output should be similar to

Here the first line indicates the line numbers (or group of line numbers) which differ betweeen the files. The c between the numbers indicates that line 18 (in sample.doc) must be changed to match line 18 (in vi.doc) if the two documents are to match. The lines starting with the symbol < are in the first file. The lines starting with > are in the second file. The command diff will continue comparing lines until it gets to the end of the files.

Now as a quiz, use diff to compare the original sample-homepage.html to index.html.

Do the indicated differences make sense?

The command cmp is used to compare files. Because it gives a "yes" or "no" answer, it can be used for binary files. If the two files are identical, it gives no response (Unix, again playing the strong, silent type). If the two files are different, it gives the filename and byte of the first difference. For instance, try

No response. Okay. So now try

Now diff responds with

So now we know that the files are different.

One of the strengths of Unix is its variety of file utilities. What you have seen in this tutorial barely scratches the surface. To learn more about file utilities, you may wish to consult "Coping With Unix" or some some Unix book. Use man -k (or aprops) to search the man pages for the action you wish to perform. If you may find yourself thinking "I wish there was an easy way to ...", a little investigating usually tells you what it is.


Next 16: Programming Up 15: File Utilities Prev 15.H: Sort Files with sort Contents


Comments and questions to CP-unix@physics.orst.edu.