How to diff remote files over SSH

The diff command line utility compares contents of two files (or directories), and shows how they differ line by line. It's straightforward to use diff if both input files are located on local host. However, what if (one or both) input files are located on remote hosts? You can actually use SSH to compare such files over network. Here is how to diff remote files over SSH.

The diff utility can take one operand in the form of stdin (represented by "-"), and you can leverage this feature to diff over SSH as follows.

$ ssh user@remote_host "cat remote_file.txt" | diff - local_file.txt

The above command line will prompt you for an SSH password to the remote host. Once you enter the password correctly, the command will read the content of a remote file via SSH, and then pipe it into diff utility for comparison with a local file.

Another method for doing diff over SSH is to use input redirection explicitly. In order to use this second method, you need to first enable password-less ssh login to remote host(s). Assuming that is the case, you can do the following.

To diff a local file and a remote file:

$ diff local_file.txt <(ssh user@remote_host 'cat remote_file.txt')

To diff two remote files:

$ diff <(ssh user@remote_host 'cat remote_file.txt') <(ssh user2@remote_host2 'cat remote_file2.txt')

Note that the above commands will fail if the remote hosts require you to enter an SSH password for access. Also note that the above input direction is not supported by sh shell. Use bash shell or something compatible.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

Leave a comment

Your email address will not be published. Required fields are marked *