Ensuring Two Directories Contain the Same Files
This is a quick and dirty method
I was in the process of copying a rather large folder from my laptop to an external harddrive when the external got unplugged, halting the copying process. I was lucky enough that the bulk of the files were located in five folders in the top level of the directory I was copying. Quickly scanning through the contents of each of those five folders, I decided that there were only two that looked sparse. After re-copying those two folders into the top directory, I wanted to ensure that the original directory and the copied one contained everything exactly. This is what I did (If dir1 was copied to dir2):
ls -R dir1 > list1; ls -R dir2 > list2; md5sum list1 list2; rm list1; rm list2
In case it’s not obvious, this lists the files/folders recursively in dir1, storing the result in list1. The same is done for dir2. The results are then compared — If the result from the md5sum is the same, then the directories are exactly the same (or more technically, they have the same structure in terms of file/folder names). The “rm” commands remove the unnecessary lists.
This is a superficial way to compare directory equivalence.
Million ways to skin a cat:
You could do the same with rsync
rsync -avvz /source/dir/ /dest/dir/
It calculates what’s different between the two and syncs it.
-P
The rsync solution by Patrick is much better I guess.
I’d recommend to use the -n option (–dry-run), so no files will be moved:
rsync -avn /dir1 /dir2
Remember that you can also transfer via multiple host, like f.e.:
rsync -avn /dir1 user@somehost:/path/to/dir
see: man rsync
And anyway, why doing it so complicated with writing into files using the solution with ls? Just do:
ls -lR /dir1 | md5sum; ls -lR /dir2 | md5sum