4.6 Comparing files (diff, cmp)



There are commands which compare files. Usually, they are used for checking changes of files.

Format

diff file1 file2
cmp file1 file2


The both commands compares file1 and file2. If the contents of files is same, nothing is displayed.


Practice: Execution of diff and cmp


Let's make different files actually.


$ cp .bashrc diff1 ↵
$ cp .bashrc diff2 ↵
$ echo 'Hello' >> diff2 ↵       add a string 'Hello' at the last of diff2
$ cmp diff1 diff2 ↵
cmp: EOF on diff1             at the tail diff1 and diff2 are different.
$ diff diff1 diff2 ↵
8a9
> Hello
$


The both cmp and diff show the place where content is different. In case of cmp, it shows only that the end of file is different, it does not describe the differences detailed like diff. In case of diff, "8a9" show us that the new line was added in line 9.



Previous Next