11

I want to compare two directories, one is local and the other is on another machine.

How can I do that? Can I do it with diff?

Eng Al-Rawabdeh
  • 111
  • 1
  • 1
  • 5

5 Answers5

5

I can think of two ways here..

  1. Mount your remote directory locally, and then use diff as you would do on a local machine.

  2. Use rsync:

rsync -avz --dry-run remote-user@remote-machine:remote-dir local-dir

This will show you the files that differ, but will not show you the actual diff. I think Unison supports diff also, but I have never used it and it does not seem to be under development any more.

Hari Menon
  • 173
  • 5
  • 1
    the problem in rsync , it is work in just one direction . i use unison before , i search how i can do `diff` by using unison but the problem on unison it is can't do `diff` without sync folder –  Oct 02 '11 at 07:37
  • If you swap the order of directories in rsync, it will work in the other way. – Hari Menon Oct 02 '11 at 07:40
  • 3
    I don't understand the comment about it working in only one direction. Being identical is commutative - if A is identical to B, then B is identical to A; if A is different from B, then B is different from A. If you're just looking for whether they're the same or not, there is no directionality. – Dave Sherohman Oct 02 '11 at 11:16
  • Dave , I mean it the comparison done based on one file example ( comparison needed between A and B , if rsync take "A" as reference and compare B reference to "A" even though A is older than B – Eng Al-Rawabdeh Jun 26 '13 at 07:27
  • `rsync` with the options above `-avz` won't give output about files on the second machine (`local-dir`) that are not in the local directory. This is probably what @EngAl-Rawabdeh means by 'one direction'. You can use the `--delete` option to make it look at files that exist only in `local-dir`. – JellicleCat Mar 21 '15 at 17:14
  • 1
    In my `rsync` (comes with Ubuntu 14.04), the output of `rsync -v` does not correlate to the files that differ but rather holds all the files that shall be compared. – JellicleCat Mar 21 '15 at 17:15
3

If you just want to determine whether they're identical or not, something like

cd <directory>
find . -type f | sort | xargs sha1sum | sha1sum

...should give you a single checksum over the entire contents (except empty subdirectories). So you can run this on both machines and compare the outputs.

  • It is possible for output of sha1sum to be the same for files that differ. While that is unlikely to occur, I would not trust Production code to use (only) this factor if determination of uniqueness is truly critical (such as determining whether to delete some archive that _seems_ to duplicate another). – Dennis Apr 15 '23 at 21:09
1

Note that using diff will require the entire contents of the remote directory to be transferred over the network, which could become prohibitively slow if there are large files or large numbers of files involved.

My recommendation would be to first determine which files differ between the two machines (by using rsync -n --delete (-n causes it to only tell you what it would do, but without actually doing it; --delete will tell you if any files exist in the destination, but not in the source, because it will want to delete them) or by comparing md5sums. If you're looking for how they're different (rather than just if they're different), I would then use diff only on those specific files to see what the differences are.

Dave Sherohman
  • 5,383
  • 1
  • 23
  • 31
0

Yes, you can do it with diff! User space applications such as "diff" don't know (and don't need to know) if a directory is local or remote.

jman
  • 396
  • 2
  • 7
0

I found a tool called " UNISON " . it is a very good tool and have many options .

Eng Al-Rawabdeh
  • 111
  • 1
  • 1
  • 5