home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 28.10 Problems with diff and Tabstops Chapter 28
Comparing Files
Next: 28.12 Comparing Two Files with comm
 

28.11 cmp and diff

cmp
cmp is another program for comparing files. (There's also a GNU version on the CD-ROM.) It's a lot simpler than diff ( 28.1 ) ; it tells you whether the files are equivalent, and the byte offset at which the first difference occurs. You don't get a detailed analysis of where the two files differ. For this reason, cmp is often faster, particularly when you're comparing ASCII ( 51.3 ) files: it doesn't have to generate a long report summarizing the differences. If all you want to know is whether two files are different, it's the right tool for the job.

It's worth noting, though, that cmp isn't always faster. Some versions of diff make some simple checks first, like comparing file length. If two binary files have different lengths, they are obviously different; some diff implementations will tell you so without doing any further processing.

Both diff and cmp return an exit status ( 44.7 ) cmp that shows what they found:

Exit Status Meaning
0 The files were the same.
1 The files differed.
2 An error occurred.
Within a shell script, the exit status from diff and cmp is often more important than their actual output.

- ML


Previous: 28.10 Problems with diff and Tabstops UNIX Power Tools Next: 28.12 Comparing Two Files with comm
28.10 Problems with diff and Tabstops Book Index 28.12 Comparing Two Files with comm

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System