Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

4 Answers 4

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

share|improve this answer

One easy way of doing it on Linux would be to:

  1. Use find or ls to list out all the files in each directory and pipe the results into different log files. Using ls would be better for your purpose as it can display various information, including the permissions, dates and sizes.
  2. Compare the log files using something like diff to identify any differences.

Hope this can help you get started.

share|improve this answer

find can give you finer control over what you are comparing by allowing you to print only the information you need. If, for example, you want to compare files based on their names and sizes, but not their dates or owner/group information you can do:

find . -printf "%p\t%s\n"

find supports many types of file information. Here are a just a few:

%g     File’s group name, or numeric group ID if the  group  has
       no name.
%G     File’s numeric group ID.
%m     File’s  permission bits (in octal).
%M     File’s permissions (in symbolic form, as for  ls).
%n     Number of hard links to file.
%t     File’s  last  modification time in the format returned by
       the C ‘ctime’ function.

Perhaps the easiest would be if you could mount the two directories in such a way that you could do something like:

diff <(cd dir1; find . -printf "%p\t%s\n"|sort) <(cd dir2; find . -printf "%p\t%s\n"|sort)
share|improve this answer

I suggest tree:

ssh ServerA "tree -s -f <directory>" > /tmp/out1 && ssh ServerB "tree -s -f <directory>" > /tmp/out2 && diff /tmp/out1 /tmp/out2
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.