Linux diff命令比较不同服务器上的文件--Unix

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8593816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:46:18  来源:igfitidea点击:

Diff command to compare files on different servers--Unix

linuxunixdiff

提问by Anusha Pachunuri

Can I use the diff command to compare files on two different servers? If not, is there any other option?

我可以使用 diff 命令来比较两个不同服务器上的文件吗?如果没有,还有其他选择吗?

采纳答案by dogbane

You can copy the file over from the other server using scpand then use diff.

您可以使用从其他服务器复制文件scp,然后使用diff.

Or sshto the remote host and diffusing a single command like this:

或者ssh到远程主机并diff使用这样的单个命令:

ssh user@remote "cat /path/to/remote/file" | diff - /path/to/local/file

回答by Corey Henderson

The "-" diffs against STDIN. You can do something like this:

“-” 与 STDIN 不同。你可以这样做:

ssh server 'cat file_to_diff' | diff -u localfile -

回答by Jeff Burdges

If your comparing multiple files, then look up rsyncand rdiff, which save you the bandwidth of copying all files.

如果您比较多个文件,则查找rsyncrdiff,这可以节省复制所有文件的带宽。

Btw, if your files are very large, then please update your question with that information.

顺便说一句,如果您的文件非常大,请使用该信息更新您的问题。

回答by Putnik

If you're troubleshooting AWS instance then it may make sense to stop inatanceA then attach its drive to instanceB you're want to compare with.

如果您正在对 AWS 实例进行故障排除,那么停止 inatanceA 然后将其驱动器附加到您要与之比较的 instanceB 可能是有意义的。

回答by Hastur

I know it's a late answer but I take the question literally, no local file and two remote files.
In bash(and not only) it's possible to use the process substitution[1,2] <(...):

我知道这是一个迟到的答案,但我从字面上理解这个问题,没有本地文件和两个远程文件
bash(不仅如此)中,可以使用进程替换[ 1, 2]<(...)

diff <(ssh Server1 'cat /path/to/file1') <(ssh Server2 'cat /path/to/file2')

The process <(list)is run asynchronously, and its input or output appears as a filename.

该进程<(list)异步运行,其输入或输出显示为文件名。

Note

笔记

  • Of course if you need only one remote file you can put the local file instead one of the <(...).
  • If both files are on the same server you can use a simpler

    ssh Server1 'diff /path/to/file1 /path/to/file2'
    
  • 当然,如果您只需要一个远程文件,您可以将本地文件放在<(...).
  • 如果两个文件在同一台服务器上,您可以使用更简单的

    ssh Server1 'diff /path/to/file1 /path/to/file2'