Last updated on August 10, 2020 by Dan Nanni
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. In this example, you are comparing remote_file.txt
against local_file.txt
.
$ 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.
This website is made possible by minimal ads and your gracious donation via PayPal or credit card
Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.
Xmodulo © 2021 ‒ About ‒ Write for Us ‒ Feed ‒ Powered by DigitalOcean