Compare files in two directories based on filename and size

Posted on in category Web Development tagged command line

I recently was copying a large number of files between two disks and wanted to verify that the copy succeeded, and did not miss any files.

I first tried a recursive diff, but this was too slow given the number of files to compare, and relatively slow I/O due to one of the disks being attached remotely.

What did end up working was using rsync in “dry-run” mode, and telling it to look only at file sizes.

rsync -rvn --size-only /src_dir/  /dst_dir/

The ‘-r’ makes it recursive, ‘-v’ verbose and ‘-n’ is for a dry run, so no files are actually copied.

If you want the files to actually be compared byte-by-byte, use diff:

diff -rq /dst_dir/ /src_dir/

The ‘-r’ makes it recursive, and the ‘-q’ gives you brief output. If you want diff to report on identical files, add ‘-s’.

diff -rqs /dst_dir/ /src_dir/