1

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

Prasenjit Patra
  • 61
  • 2
  • 3
  • 4

4 Answers4

8

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

Kamil Kisiel
  • 11,946
  • 7
  • 46
  • 68
1

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.

sybreon
  • 7,357
  • 1
  • 19
  • 19
1

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)
Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
1

I suggest tree:

ssh ServerA "tree -s -f <directory>" > /tmp/out1 && ssh ServerB "tree -s -f <directory>" > /tmp/out2 && diff /tmp/out1 /tmp/out2
Greeblesnort
  • 1,739
  • 8
  • 10