Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store result of diff in Linux

Tags:

file

linux

diff

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?

like image 281
nitin Avatar asked Oct 11 '11 21:10

nitin


People also ask

How do you save a diff?

Creating a Diff Assuming this is the case, then select File → Save Diff... This will display the Diff Options dialog (see the section called “Diff Settings” for more information on diff formats and options). After configuring these options, click the Save button and save the diff to a file with the extension .

What is the output of diff?

On Unix-like operating systems, the diff command analyzes two files and prints the lines that are different. In essence, it outputs a set of instructions for how to change one file to make it identical to the second file.

What is a diff file in Linux?

The Linux diff command is used to compare two files line by line and display the difference between them. This command-line utility lists changes you need to apply to make the files identical. Read on to learn more about the diff command and its options with easy-to-follow examples.


2 Answers

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).

like image 112
Kusalananda Avatar answered Oct 20 '22 01:10

Kusalananda


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.

like image 33
ath88 Avatar answered Oct 20 '22 02:10

ath88