Try this command:
diff -B <(sshpass -p 'pa$$word1' ssh ubuntu1@images.server1.com "find /home/www/images/test_images -type f | sed 's/\/home\/www\/images\/test_images\///g'" | sort -n) <(sshpass -p 'pa$$word2' ssh ubuntu2@images.server2.com "find /var/www/site/images/test_images -type f | sed 's/\/var\/www\/site\/images\/test_images\///g'" | sort -n) | grep ">" | awk '{print $2}'
Explanation:
You can use diff -B <() <()
for taking the diff between two streams. The command first uses sshpass
to ssh into the two servers without having to enter your passwords interactively.
Each parameter for diff -B
uses find
command to recursively list all your images in the specified directory and uses sed
to remove the root path of the files (because they are different for two servers - and to make it work for the diff
command); and the sort
command to sort them.
Since the output of the diff
command returns either >
or <
, grep
is used to filter out only the diffs from your Server 2. Last, awk
prints out only the second column (removes the >
column from the output).
NOTE: You need to install sshpass
first. Use apt-get
to install it as follows:
sudo apt-get install sshpass
You can extend this by piping other commands like rm
. Hope this works for you.