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. But what if input files located on two different hosts? You can actually diff remote files over ssh. Here is how.

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

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

The above command line will prompt you for a 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')

These commands will fail if the remote hosts require you to enter ssh password for access.

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.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. 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 *

Current ye@r *