4.4. Comparing Files: cmp and diffThere are two utilities that allow you to compare the contents of two files:
The next few subsections describe these utilities. 4.4.1. Testing for Sameness: cmpThe cmp utility determines whether two files are the same (Figure 4-8).
In the following example, I compared the files "lady1," "lady2," and "lady3": $ cat lady1 ...look at the first test file. Lady of the night, I hold you close to me, And all those loving words you say are right. $ cat lady2 ...look at the second test file. Lady of the night, I hold you close to me, And everything you say to me is right. $ cat lady3 ...look at the third test file. Lady of the night, I hold you close to me, And everything you say to me is right. It makes me feel, I'm so in love with you. Even in the dark I see your light. $ cmp lady1 lady2 ...files differ. lady1 lady2 differ: char 48, line 3 $ cmp lady2 lady3 ...file2 is a prefix of file3. cmp: EOF on lady2 $ cmp lady3 lady3 ...files are exactly the same. $ _ The -l option displays the byte offset and values of every byte that doesn't match: $ cmp -l lady1 lady2 ...display bytes that don't match. 48 141 145 49 154 166 ... 81 145 56 82 40 12 cmp: EOF on lady2 ...lady2 is smaller than lady1. $ _ 4.4.2. File Differences: diffThe diff utility compares two files and displays a list of editing changes that would convert the first file into the second file (Figure 4-9).
There are three kinds of editing changes: adding lines (a), changing lines (c), and deleting lines (d). Figure 4-10 shows the format that diff uses to describe each kind of edit, where firstStart and firstStop denote line numbers in the first file, and secondStart and secondStop denote line numbers in the second file.
In the following example, I compared several text files to observe their differences: $ cat lady1 ...look at the first test file. Lady of the night, I hold you close to me, And all those loving words you say are right. $ cat lady2 ...look at the second test file. Lady of the night, I hold you close to me, And everything you say to me is right. $ cat lady3 ...look at the third test file. Lady of the night, I hold you close to me, And everything you say to me is right. It makes me feel, I'm so in love with you. Even in the dark I see your light. $ cat lady4 ...look at the fourth test file. Lady of the night, I'm so in love with you. Even in the dark I see your light. $ diff lady1 lady2 ...compare lady1 and lady2. 3c3 |