Comparing Files with cmp


Comparing Files with cmp

Suppose you've been working on the dearliza file and you want to know how it differs from the dearhenry file. Using cmp, you can compare the two files as shown in Code Listing 6.13.

Code Listing 6.13. cmp gives just the facts about the first difference between two files.

[ejr@hobbes manipulate]$ cmp dearliza  dearhenry dearliza dearhenry differ: char 20, line 2 [ejr@hobbes manipulate]$ 

Code Listing 6.14. cmp also tells you if the files matched until one ended (EOF stands for "end of file").

[ejr@hobbes manipulate]$ cmp limerick  limericks cmp: EOF on limerick [ejr@hobbes manipulate]$ 

To compare files with cmp:

  • cmp dearliza dearhenry

    At the shell prompt, type cmp followed by both filenames. As Code Listing 6.13 shows, these two files are not the same. If the files are identical, you'll find yourself back at the shell prompt with no comment from cmp. If both files are identical until one of them endsthat is, say, the first 100 lines are the same, but one continues and the other endsthen you'll see an EOF (end of file) message, as in Code Listing 6.14.

Tips

  • You can find out other ways that files differ using diff, as described in the next section, Finding Differences in Files with diff.

  • Unix provides an exit status message that you can use to get more information about how the program stopped and why. See Using Advanced Redirection in Chapter 17 for more information.

  • You can also use diff to find out which files are in one directory but not another. Just type diff followed by the names of the two directories; for example, diff /home/ejr/Directory/home/ejr/ Newdirectory

  • You might also check out the section, Finding Differences in Files with sdiff, for yet another way to compare files.





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

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net