Comparing Files
Normally, several new files are created in during the course of updating older files. Several versions of the similar file with slight differences in the content are maintained. At a few point of time, it becomes extremely hard to keep track of the files and it becomes essential to cleanup the directories through removing the unwanted older versions.
There are utilities in UNIX which can be used to compare the contents of two files to see if they are similar. The nature of the difference can be determined if they are not.
cmp
This command is used to compare two files. That compares two files and shows the first example where the files differ. cmp returns no output if there is no difference.
Syntax
cmp file1 file2
In the given syntax file1 and file2 are the files to be compared.
Practice 1
The following instance displays the usage of the cmp command.
# cat > myfile
This is a description of cmp command
^D
# cat > yourfile
This is the description of cmp command
^D
# cmp myfile yourfile
myfile yourfile differ: char 9, line 1
In the given example the cmp command reports that the two files differ in the ninth character of the document, located in line 1.
Note: cmp indicates only the first character in which the files differ. cmp reports the difference on a character through character basis.