Introduction to Unix |
|
![]() |
![]() |
3.4. Comparing Files¶
Linux for Programmers and Users, Section 4.4
Sometimes, we may wonder if too files are actually the same file or we may want to what the differences are between text files.
3.4.1. cmp¶
SYNOPSIS
cmp file1 file2 [skip1 [skip2]]
DESCRIPTION
The cmp utility compares two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same; if they differ, the byte and line number at which the first difference occurred is reported.
The optional arguments skip1 and skip2 are the byte offsets from the beginning of file1 and file2, respectively, where the comparison will begin.
3.4.2. diff¶
SYNOPSIS
diff [options] from-file to-file
DESCRIPTION
In the simplest case, diff compares the contents of the two files from- file and to-file.
-b
¶
Ignore changes in amount of white space.
-B
¶
Ignore changes that just insert or delete blank lines.
--brief
¶
Report only whether the files differ, not the details of the differences.
-e
,
--ed
¶
Make output that is a valid ed script.
-i
¶
Ignore changes in case; consider upper- and lower-case letters equivalent.
-s
,
--report-identical-files
¶
Report when two files are the same.
-w
¶
Ignore white space when comparing lines.
-y
¶
Use the side by side output format.
3.4.3. ed¶
SYNOPSIS
ed [-] [file]
DESCRIPTION
ed is a line-oriented text editor. It is used to create, display, modify and otherwise manipulate text files.
If invoked with a file argument, then a copy of file is read into the editor’s buffer. Changes are made to this copy and not directly to file itself. Upon quitting ed, any changes not explicitly saved with a ‘w’ command are lost.
Editing is done in two distinct modes: command and input. When first invoked, ed is in command mode. In this mode commands are read from the standard input and executed to manipulate the contents of the editor buffer. A typical command might look like:
,s/old/new/gwhich replaces all occurrences of the string old with new.
3.4.4. Patching Files with diff and ed¶
Here is an exercise to demonstrate how diff and ed can be used together to propagate edits to a file. This usage of these commands allows one to transmit only the changes to files to others so that they can keep their files current. It can also have application for making incremental backups of text files.
First make copy of any text file. (file1, file2)
Make several arbitrary changes to file1.
Now, create a file describing the differences in a format that ed can read:
$ diff -e file2 file1 > diff_script
View the contents of the
diff_script
file and edit the file to append two lines containing a w and a q, which are ed commands for write and quit.Now use the ed to apply the changes to file2:
$ ed file2 < diff_script
Verify that the two files are now the same:
$ diff file1 file2
3.4.5. sum¶
SYNOPSIS
sum [FILE]…
DESCRIPTION
Print checksum and block counts for each FILE. With no FILE, or when FILE is -, read standard input.
3.4.6. comm¶
SYNOPSIS
comm [OPTION]… FILE1 FILE2
DESCRIPTION
Compare sorted files FILE1 and FILE2 line by line.
With no options, produce three-column output. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files.
-1
¶
suppress lines unique to FILE1
-2
¶
suppress lines unique to FILE2
-3
¶
suppress lines that appear in both files