compare contents of two directories on remote server using unix

You can use rsync with the -n flag to find out if the files are in sync, without actually doing a sync.

For example, from server1:

rsync -n -avrc /abc/home/sample1/* server2:/abc/home/sample2/

This will print the names of all files (recursive, with the -r flag) that differ between server1:/abc/home/sample1/ and server2:/abc/home/sample2/

rsync used parameters explanation

-n, --dry-run - perform a trial run with no changes made

-a, --archive - archive mode; equals -rlptgoD (no -H,-A,-X)

-v, --verbose - increase verbosity

-r, --recursive - recurse into directories

-c, --checksum - skip based on checksum, not mod-time & size


On server1:

cd /abc/home/sample1/ && diff --side-by-side --suppress-common-lines <(find . -type f|xargs stat --printf "%s\t%n\n"|sort -n) <(ssh server2 "cd /abc/home/sample2/ && find . -type f|xargs stat --printf \"%s\t%n\n\"|sort -n")

This is pretty fast but probably not as accurate as rsync since it uses file size instead of hash of content. Also, the rsync from kielni's answer will not show anything if server2 has more files then server1. Instead try this:

rsync -n -avr --size-only --delete /abc/home/sample1/ server2:/abc/home/sample2/

I always use rsync -avcn for such tasks - I am a CLI fan. The idea is to do a "dry run" (-n), which shows what would have been done without actually copying any files or making any changes. Using the -c switch is really important as it uses the checksum rather than the modification date or file size to determine if the files are different. -a makes sure that the comparison is more thorough and it forces the program to dive into the subdirectories. Finally, -v increases the level of verbosity, which is useful in case different files are found.

So a possible solution goes something like this:

rsync -avcn source_dir/ dest_dir/

and rsync will print the names of the files that are different between the two directories. Note the trailing slashes - they are essential.

However:

  1. if there are many different files, the list soon fills up the terminal screen and becomes hard to read. In this case it is better to redirect the output to a file: rsync -avcn source_dir/ dest_dir/ > diffs.txt
  2. this might sound silly but remember to escape the spaces if the source or destination directory has them. Alternatively, you can enclose them in quotation marks. rsync -avcn /src/My Dir/ dest/My dir/ is not what you want; rather, you should use rsync -avcn /src/My\ Dir/ dest/My\ Dir/ or perhaps rsync -avcn /src/"My Dir"/ dest/"My Dir"/. This point is especially valid when dealing with Windows mounts.