How to diff directories over ssh

LinuxShell

Linux Problem Overview


I am trying to recursively compare a directory in the local host to a directory in a remote linux machine. However, when i execute the next command:

diff -r 'ssh [email protected]:/home/admin' /home/admin

it doesn't work saying:

> diff: ssh [email protected]:/home/admin: No such file or directory

what is the right way to compare recursively the directories contents?

Thanks in advance

Linux Solutions


Solution 1 - Linux

If you needn't diff the detail in file, just get the difference of dir/file name, then try this:

(Note: need set "SSH login without password" , for detail , review this URL: http://www.linuxproblem.org/art_9.html)

diff <(ssh admin@10.0.0.10 ls -R /home/admin) <(ls -R /home/admin)

Solution 2 - Linux

Try using "rsync" with the "-n" option, which just does a "dry run" and tells you what it would do.

Solution 3 - Linux

You can mount the remote directory via sshfs, then you can use diff -r to compare the two directories as you want to do it.

Alternatively you could run similar find commands on both machines to print the file trees (e. g. file names, sizes, and dates) and then compare the outputs of both using diff. This would not compare file contents, of course:

diff <(ssh host find /home/admin -printf '"%8s %P\n"') \
     <(find /home/admin -printf '%8s %P\n')

Notice the double quoting of the printf-format string in the ssh call. One layer is removed by the ssh relay.

Solution 4 - Linux

For all the examples below empty folders do not matter.

If the password is not required for ssh and you want to see the files

diff -y -W250 \
  <(ssh user@host1 "find /jbossas/modules -type f | xargs -I{} md5sum {} | sort -k2")
  <(ssh user@host2 "find /jbossas/modules -type f | xargs -I{} md5sum {} | sort -k2")

If the password is not required for ssh and you just want to know if the folders are the same

diff -y -W250 \
  <(ssh user@host1 "find /jbossas/modules -type f | xargs -I{} md5sum {} | sort | md5sum") \
  <(ssh user@host2 "find /jbossas/modules -type f | xargs -I{} md5sum {} | sort | md5sum")

If password is required for ssh

  • Install the sshpass package
  • Receive your password securely in the PASS variable (the password will be cleared in 60 seconds): echo -n "Password: "; IFS= read -rs PASS < /dev/tty; trap 'unset PASS; echo "PASS has been cleared"; trap - USR1' USR1; { sleep 60; kill -USR1 $$; } &

So...

diff -y -W250 \
  <(sshpass -p "$PASS" ssh user@host1 "find /jbossas/modules -type f | xargs -I{} md5sum {} | sort | md5sum") \
  <(sshpass -p "$PASS" ssh user@host2 "find /jbossas/modules -type f | xargs -I{} md5sum {} | sort | md5sum")

If sudo is needed on the remote host

diff -y -W250 \
  <(ssh user@host1 "echo \"$PASS\" | sudo -S find /jbossas/modules -type f | xargs -I{} md5sum {} | sort | md5sum") \
  <(ssh user@host1 "echo \"$PASS\" | sudo -S find /jbossas/modules -type f | xargs -I{} md5sum {} | sort | md5sum")

And you can merge the options.

I recommend icdiff instead of diff, but diff is enough.

Solution 5 - Linux

I find krusader very useful for such task in case you need a GUI. See https://www.linux.com/news/synchronize-directories-komparator-and-kdiff3 for more details http://krusador.org.

example folder compare

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHesham YassinView Question on Stackoverflow
Solution 1 - LinuxBMWView Answer on Stackoverflow
Solution 2 - LinuxMark SetchellView Answer on Stackoverflow
Solution 3 - LinuxAlfeView Answer on Stackoverflow
Solution 4 - LinuxMhagnumDwView Answer on Stackoverflow
Solution 5 - LinuxHanan ShteingartView Answer on Stackoverflow