AIXLinux

How to Compare Contents of Two Directories

After you copied one directory to another like this:
$ cp -rp /path/to/source/directory /path/to/target/
or mirrored the source directory to the target one.
$ rsync -av /path/to/source/directory/ /path/to/target/directory/
You might want to confirm your job is done. Let’s see what we can compare between the two.

Total Size

$ du -sb /path/to/source/directory; du -sb /path/to/target/directory
du usually returns a proximate result which is not accurate, but -b can count the actual bytes of the directories, which means that it will return the apparent size in bytes. Please note that, some Unix do not support -b option.

File List

We are going to list both directories to text files, and then use diff to compare the two outputs.
$ ls -rtl /path/to/source/directory > source.lst; ls -rtl /path/to/target/directory > target.lst; diff source.lst target.lst
This command will not only compare the file name, but also compare the permission, file size, modified date, etc.

File Checksum

We take advantage of rsync to do the checksum.
$ rsync -avnc /path/to/source/directory/ /path/to/target/directory/
The option -n instructs rsync to do a dry-run which will list all different files but transfer nothing in fact. Another option -c means the comparison should base on the checksum of source and target files.

I have to note this down. This command of doing checksum is quite easy but it might be expensive in terms of execution time for a large-scaled, say, 100 Giga-byte or above degree directories.

Leave a Reply

Your email address will not be published. Required fields are marked *