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
4 Answers
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

- 12,184
- 7
- 48
- 69
One easy way of doing it on Linux would be to:
- Use
find
orls
to list out all the files in each directory and pipe the results into different log files. Usingls
would be better for your purpose as it can display various information, including the permissions, dates and sizes. - Compare the log files using something like
diff
to identify any differences.
Hope this can help you get started.

- 7,405
- 1
- 21
- 20
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)

- 62,149
- 16
- 116
- 151
I suggest tree:
ssh ServerA "tree -s -f <directory>" > /tmp/out1 && ssh ServerB "tree -s -f <directory>" > /tmp/out2 && diff /tmp/out1 /tmp/out2

- 1,759
- 8
- 10