Diff over SSH

Readers of this blog will know that I’m a big fan or rsync. We use it almost exclusively for doing our site deployments. Generally I do an rsync dry-run to ensure I’ve gotten the correct folders and have a reasonable idea that the files I’m about to launch live are really what should be going up.

Sometimes though I’ll see a file and I’m unsure as to why it’s going to be pushed up to the live server. I’m not always the one who’s edited the files so I’d like to see the differences between the live and the dev server. I know looking at the diff in CVS or SVN is one way, but mid deployment sometimes it’s just easier to see the differences between the two files.

So use this to see the differences between a local file and one on a remote server:
ssh user@ "cat /var/www/html/remote_file_to_compare.php" | diff - "/var/www/html/local_file_to_compare"

** Use the proper IP address and file names of course.

You can add switches to the diff command but this gives you the basic syntax. The nice think about this solution is that it’s transferable to any command. I’m doing a diff here, but you can run any remote server command in this way. For example:
ssh user@ "free -m"
ssh user@ "df -h"

This simple trick will save you some time of logging into the server with a new session.