如何在 Linux 中存储 diff 的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7732545/
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-05 06:37:59  来源:igfitidea点击:

How to store result of diff in Linux

linuxfilediff

提问by nitin

How to get the result on another file after applying diff to file A.txt and B.txt.

如何在对文件 A.txt 和 B.txt 应用差异后在另一个文件上获得结果。

Suppose File A.txt has:

假设文件 A.txt 有:

a
b
c

File B.txt has:

文件 B.txt 有:

a
b

on running

跑步时

diff A.txt B.txt It gives result as c, but how to store it in a file C.txt?

diff A.txt B.txt 它给出的结果是c,但是如何将其存储在文件C.txt 中?

采纳答案by Kusalananda

The diffutility produces its output on standard output (usually the console). Like any UNIX utility that does this, its output may very simply be redirected into a file like this:

diff实用程序在标准输出(通常是控制台)上生成其输出。像任何执行此操作的 UNIX 实用程序一样,其输出可能会非常简单地重定向到如下文件中:

diff A.txt B.txt >C.txt

This means "execute the command diffwith two arguments (the files A.txtand B.txt) and put everything that would otherwise be displayed on the console into the file C.txt". Error messages will still go to the console.

这意味着“执行diff带有两个参数(文件A.txtB.txt)的命令,并将原本会在控制台上显示的所有内容放入文件中C.txt”。错误消息仍将发送到控制台。

To save the output of diffto a file and alsosend it to the terminal, use teelike so:

要的输出保存diff到一个文件,将其发送到终端,使用tee像这样:

diff A.txt B.txt | tee C.txt

tee will duplicate the data to all named files (only C.txthere) and also to standard output (most likely the terminal).

tee 会将数据复制到所有命名文件(仅C.txt在此处)以及标准输出(很可能是终端)。

回答by ath88

Using >you can redirect output to a file. Eg:

使用>您可以将输出重定向到文件。例如:

    diff A.txt B.txt > C.txt

This will result in the output from the diff command being saved in a file called C.txt.

这将导致 diff 命令的输出保存在名为 C.txt 的文件中。

回答by hfactor

Use Output Redirection.

使用输出重定向。

diff file1 file2 > output

差异文件 1 文件 2 > 输出

will store the diff of file1 and file2 to output

将存储 file1 和 file2 的差异以输出

回答by cdprince

There are some files that diffmay not do well with the output, like block special files, character special files, and broken links. The output due to differences with these may go to standard error.

有些文件diff可能无法很好地处理输出,例如块特殊文件、字符特殊文件和断开的链接。由于与这些差异的输出可能会出现标准错误。

Interestingly, when I redirected standard error, I still missed some things:

有趣的是,当我重定向标准错误时,我仍然错过了一些东西:

diff -qr <DirA> <DirB> 2>&1 > mydiff.txt

The only way to see the results of everything was to:

查看所有结果的唯一方法是:

diff -qr <DirA> <DirB> |tee mydiff.txt

I was comparing the results of a live-cd mounted directory after copying to an external HD

复制到外部 HD 后,我正在比较 live-cd 挂载目录的结果