1

If cmp command is used to compare two directory listings (ls dir1 and ls dir2), will it compare the contents of the files in both directories or just the file names in both directories?

haruya
  • 37
  • 8
  • `cmd` only compares files. If you compare the results of two `ls` results, you'll just compare text files containing file names – Mathieu Feb 25 '22 at 08:51

1 Answers1

0

There are two commands you can use for checking the differences between files: cmp uses byte comparison, while diff uses line comparison.
As a result, diff is better for textfiles, as you can see in following examples:

Prompt> cat a.txt
aaa
ddd

Prompt> cat b.txt
aaa
bbb
ccc

cmp result:

Prompt> cmp -l a.txt b.txt
5 144 142
6 144 142
7 144 142
cmp: EOF on a.txt after byte 8

diff result:

Prompt> diff a.txt b.txt
2c2,3
< ddd
---
> bbb
> ccc

As you see, it's very clear to see which lines only exist in one or the other file.

In case you just want to check if two files are equal or not, you might check the checksum (output contains checksum, number of bytes and filename):

Prompt> cksum a.txt
1040260371 8 a.txt
Prompt> cksum b.txt
2586209216 12 b.txt
Dominique
  • 16,450
  • 15
  • 56
  • 112