how to compare two directories residing in two different servers

13,928

Solution 1

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

Solution 2

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)

Solution 3

I suggest tree:

ssh ServerA "tree -s -f <directory>" > /tmp/out1 && ssh ServerB "tree -s -f <directory>" > /tmp/out2 && diff /tmp/out1 /tmp/out2

Solution 4

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:
13,928

Related videos on Youtube

Prasenjit Patra
Author by

Prasenjit Patra

Updated on September 17, 2022

Comments

  • Prasenjit Patra
    Prasenjit Patra almost 2 years

    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