16: Programming | 15: File Utilities | 15.H: Sort Files with sort |
The diff command is used to compare two
text files. So, to compare sample.doc with
vi.doc
> diff sample.doc vi.doc
If you only changed the name and date without moving any lines, your output should be similar to
18c18 < NAME DATE --- > Melanie Johnson 10-12-96
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
> cd public-html/figs
> cp osu.gif osu.bak
> diff osu.gif osu.bak
No response. Okay. So now try
> diff osu.gif osugrey.gif
Now diff responds with
diff: 0653-827 Missing newline at the end of file osu.gif
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.
16: Programming | 15: File Utilities | 15.H: Sort Files with sort |