I'm connected on a host via ssh and I'd like to compare (let's say with diff) a certain config file against its counterpart on an another host, also accessible via ssh, without having to manually download the remote file first before running the diff.
- 4,925
- 4
- 35
- 39
-
You could mount the remote directory using `SSHFS`, and then access it as if it's local. – Barmar Jul 16 '14 at 20:32
5 Answers
ssh user@remote_host "cat remote_file.txt" | diff - local_file.txt
-
1Won't the pipe just download the results of `cat`, which are in fact just the original file? – Stijn de Witt Jul 14 '14 at 18:32
-
1the pipe will push the result of the remote `ssh` command to the `diff` which is waiting to compare `stdin` with localfile. – fduff Jul 14 '14 at 18:52
-
1Maybe I'm misinterpreting the 'without having to download the remote file' requirement. I guess OP means 'without having to manually download the remote file'. :) EDIT: Ah, you are the OP. :) – Stijn de Witt Jul 14 '14 at 19:12
-
@StijndeWitt, yes I wanted to avoid having to scp the remote file first and then do the diff.. – fduff Jul 14 '14 at 19:48
-
Try:
diff local_file <(ssh user@server 'cat remote_file')
or using vimdiff:
vimdiff local_file scp://server//path/to/remote_file
- 150,973
- 38
- 327
- 406
-
Does `diff local_file <(ssh user@server 'cat remote_file')` work only for passwordless SSH? I don't seem to be able to get a password prompt with it... – sdbbs Jun 30 '16 at 08:41
-
1@sdbbs: Process substitution ran in background, and standard output is written to the named pipe (or anonymous named pipe), so you don't get the password prompt. Try `cat <(ssh user@server 'cat remote_file')` and `cat < <(ssh user@server 'cat remote_file')` to see the differences. – cuonglm Jun 30 '16 at 11:51
-
vimdiff password prompt is ok while password prompt with diff leads to empty remote side – monok May 15 '20 at 15:25
-
-
vimdiff doesn't work for me. What is the exact command? I tried `vimdiff test1 scp:[email protected]://home/ckim/bin/test1`. The right side is empty. Of course if I do `scp [email protected]://home/ckim/bin/test1 test2`, the file is copied to test2 ok. – Chan Kim Aug 08 '23 at 08:54
Maybe this is not helpful in your exact case, but I often simply use
sum -r
on each machine, and manually compare the checksums. That allows for comparisons of files even when they are not reachable via an ssh connection. Of course, all it answers is "are they identical", but often that is sufficient for my needs. It also makes it easy to verify that a single file is the same on 3 or 4 or more machines.
- 231
- 1
- 3
-
3I can imagine that for large files, or slow connections, this might be very useful. It saves the download of the entire file for when files are actually different. A better Bash scripter than myself can probably combine this answer with the above answers involving a remote `cat` to only actually run the `cat` command when it has been determined that the files differ. – Stijn de Witt Jul 14 '14 at 18:34
-
2Commanding `info coreutils 'sum invocation'` says, inter alia: "'sum' is provided for compatibility; the 'cksum' program (see next section) is preferable in new applications." – Teemu Leisti Nov 22 '18 at 07:46
If you'd prefer a more visual approach in you terminal session. The midnight commander has a remote file system option (SFTP link... option in the menu) and visual file compare option. It is not installed by default on most Linux systems but is available in most base repositories. Steps:
- Start midnight commander (command:
mc) in the folder containing the file to be compared - On other side (for example the Right menu) create a sftp link to the other server at the same folder (
sftp://<user>@<servername>/<path>). - Using the
inskey select/highlight the files to be compared. (tabchanges between left and right panel). - In the Command menu, select Compare files
Note: The midnight commander menu can usually be access using the mouse in your terminal session. It that doesn't work F9 gets you in the menu also.
- 153
- 5
-
Does not work: `"/sftp://live_pr~d/asset/static" is a directory` – Anton Duzenko Apr 17 '23 at 09:47
ssh user@remote_host "cat remote_file.txt" | cmp local_file.txt
or if you don't want to show result, can run in silent mode
ssh user@remote_host "cat remote_file.txt" | cmp -s local_file.txt
- 11
- 1
-
`cmp` does a quick diff and stops as soon as a difference is detected and won't show what exactly changed. So it's not very useful in this scenario. – fduff Oct 06 '20 at 14:40