Section 4.4. Comparing Files: cmp and diff


[Page 116 (continued)]

4.4. Comparing Files: cmp and diff

There are two utilities that allow you to compare the contents of two files:

  • cmp, which finds the first byte that differs between two files

  • diff, which displays all the differences and similarities between two files

The next few subsections describe these utilities.

4.4.1. Testing for Sameness: cmp

The cmp utility determines whether two files are the same (Figure 4-8).

Figure 4-8. Description of the cmp command.

Utility: cmp -ls fileName1 fileName2 [offset1] [offset2]

cmp is a utility that tests whether two files are identical. If fileName1 and fileName2 are exactly equal, then cmp returns the exit code 0 and displays nothing; otherwise, it returns the exit code 1 and displays the offset and line number of the first mismatched byte. If one file is a prefix of the other, then the EOF message is displayed for the file that is shorter. The -l option displays the offset and values of all mismatched bytes. The -s option causes all output to be inhibited. The optional values offset1 and offset2 specify the starting offset in fileName1 and fileName2, respectively, that the comparison should begin.



[Page 117]

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: diff

The 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).

Figure 4-9. Description of the diff command.

Utility: diff -i -dflag fileName1 fileName2

diff is a utility that compares two files and outputs a description of their differences. See the rest of this section for information on the format of this output. The -i flag makes diff ignore the case of the lines.



[Page 118]

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.

Figure 4-10. The meaning of output produced by diff.

Additions

firstStart a secondStart, secondStop

> lines from the second file to add to the first file

Deletions

firstStart, firstStop d lineCount

< lines from the first file to delete

Changes

firstStart, firstStop c secondStart, secondStop

< lines in the first file to be replaced

--

> lines in the second file to be used for the replacement


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 
[Page 119]
< And all those loving words you say are right. -- > And everything you say to me is right. $ diff lady2 lady3 ...compare lady2 and lady3. 3a4,6 > It makes me feel, > I'm so in love with you. > Even in the dark I see your light. $ diff lady3 lady4 ...compare lady3 and lady4. 2,4d1 < I hold you close to me, < And everything you say to me is right. < It makes me feel, $ _





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

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