How to store result of diff in Linux

江枫思渺然 提交于 2019-12-22 01:28:42

问题


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

Suppose File A.txt has:

a
b
c

File B.txt has:

a
b

on running

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


回答1:


The diff utility 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 A.txt B.txt >C.txt

This means "execute the command diff with two arguments (the files A.txt and 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.

To save the output of diff to a file and also send it to the terminal, use tee like so:

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

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




回答2:


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.




回答3:


Use Output Redirection.

diff file1 file2 > output

will store the diff of file1 and file2 to output




回答4:


There are some files that diff may 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.

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



来源:https://stackoverflow.com/questions/7732545/how-to-store-result-of-diff-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!