How to compare two directories residing in two different servers

unix-shell

I have one requirement like…I need to write a shell script which will compare two directories residing in two different servers (server A and server B) and list out the discrepancies if found any. Ideally the file names, counts, size should be same for the directory in two servers. So the script should find out the discrepancies if any. Could anyone please help me?
Thanks in advance and best regards,
Prasenjit

Best Answer

I like to use rsync for this purpose.

For example, on ServerA run:

rsync -avnc --delete /path/to/dir/ serverB:/path/to/dir/

You can remove the -c switch if you don't need to do a checksum comparison of the files. Without it rsync will assume they are the same if they have the same size and timestamps.

Note the trailing slashes on each of the paths.

Very important: Make sure you have the -n switch otherwise rsync will start changing the contents of ServerB

Related Topic