How do I diff files/folders across machines provided that the only connectivity available is ssh?
17 Answers
You can do it with Bash's process substitution:
diff foo <(ssh myServer 'cat foo')
or the same with
ssh myServer cat foo | diff foo -
Or, if both are on remote servers:
diff <(ssh myServer1 'cat foo') <(ssh myServer2 'cat foo')
- 2,096
- 85,693
Finally I've found great solution: vimdiff
vimdiff /path/to/file scp://remotehost//path/to/file
thanks to http://linux.spiney.org/remote_diff_with_vim_and_ssh see also http://www.vim.org/scripts/script.php?script_id=1075 .
- 3,561
If you just want to see what files are different, rather than a diff of the actual files, then you can use rsync --dry-run
- 33,461
Here's another quick and dirty command line recipe. Unlike the chosen answer, it works inside of makefiles:
ssh [login]@[host] "cat [remote file]" | diff - "[local file]"
- 165
- 191
Use scp to bring the files to a common machine and diff them there?
Or, if you just want to know if the files are different or not, hash them with md5sum on each machine.
You could also look into something like SSHFS, but I don't know how well an algorithm like diff performs over that.
- 802
You can use rsync in dry run mode, as suggested briefly in another answer. It lists any files that are different.
For that, use the rvnc options (r=recursive, v=verbose, n= dry-run, c=checksum). With rsync in pull mode (rsync [OPTION...] [USER@]HOST:SRC... [DEST]), an example is:
rsync -rvnc root@182.18.158.207:/var/www/html/dev/ .
Remember, this provides no info on whether the local or remote file is newer. Just if they differ.
If you have sshfs and need to diff directories:
mkdir remote_path
sshfs user@host:/path/ remote_path
diff -r path remote_path
- 131
on your local machine, make a recursive copy of the directory you want to diff. For instance:
cp -R dir replicause rsync to replicate the remote directory over the local copy:
rsync remote:/path/to/dir replicause diff to find difference between the local directory and the local replica of the remote one:
diff dir replica
- 254
Here is how I did it.
I used SFTP to the remote server and entered my username/pwd when prompted.
Then I used the dir that was created in the .gvfs dir in my home directory in the diff command.
diff -r --brief /home/user dir/.gvfs/SFTP\ on\ freenas.local/path to dir/dir1 /path to local dir/dir2
This is a script that can help to diff local folder and remote folder.:
#!/bin/bash
LOCALFOLDER=/var/www/tee
REMOTEFOLDER=$(ssh root@111.111.111.1 'ls -lA /hfs/tee'| grep -E '^total' | cut -d " " -f 2 > remotessh.txt)
COMMAND=$(ls -lA $LOCALFOLDER | grep -E '^total' | cut -d " " -f 2 > localssh.txt)
REM=$(cat remotessh.txt)
LOCAL=$(cat localssh.txt)
echo $LOCAL
echo $REM
if [ $REM -eq $LOCAL ]
then
echo Directories are the same
else
echo Directories are differnt
fi
#diff localssh.txt remotessh.txt | grep -E '^total' | cut -d " " -f 2
http://myfedora.co.za/2012/04/diff-two-remote-file-systems/
diff <(/usr/bin/ssh user1@192.168.122.1 'ls /opt/lib/') <(/usr/bin/ssh user2@192.168.122.1 'ls /tmp/') | grep -i ">" | sed 's/> //g'
You could also try generalizing the approach by creating a bash function, possibly in your ~/.bashrc:
myrdiff() { ssh root@"$1" cat "$2" | diff -s - "$2" ; }
then invoking it with a construct like:
myrdiff vm20-x86-64 /etc/gdm/Init/Default
By invoking diff with -s, this will also report if the files are identical.
I use | cat at the end of the command for interactive password prompts and sdiff because it is easier to read than diff :
So between local and remote, type :
sdiff -s cat myfile<(ssh server1 'cat myfile') | cat
and between two servers, type :
sdiff -s <(ssh server1 'cat myfile') <(ssh server2 'cat myfile') | cat
- 439
using grep
remote ip: 172.16.98.130
remote file: /etc/ssh/keys-root/authorized_keys
local file: ~/.ssh/id_rsa.pub
Now compare the contents of remote file with the local file using grep
grep -w "$(cat ~/.ssh/id_rsa.pub)" <<< $(sshpass -p "mypassword" ssh root@172.16.98.130 'cat /etc/ssh/keys-root/authorized_keys')
check the success status of the grep command
echo $?
# 0
Using this idea to setup SSH passwordless login:
if ! grep "$(cat ~/.ssh/id_rsa.pub)" <<< $(sshpass -p "mypassword" ssh root@172.16.98.130 cat /etc/ssh/keys-root/authorized_keys) &>/dev/null; then
cat ~/.ssh/id_rsa.pub | sshpass -p "mypassword" ssh root@172.16.98.130 "cat >> /etc/ssh/keys-root/authorized_keys"
fi
ssh root@172.16.98.130
Note:
Make sure sshpass package is installed for passing password non-interactively to ssh command.
- 101