1

This is the content of one.txt (REMARK: ignore the first line containing \\; I was unable to put blank lines at the start of code while writing the question. So the file starts from the second line, i.e. 2 blank lines, then one, then two, then 9 blanks and then three)

\\  


one
two  









three

This is the content of two.txt (REMARK: same as above, i.e. three blank lines, then one, blank line, two, blank line, three)

\\



one  

two

three  

So when I do

diff -B one.txt two.txt

I get the output

3,10d2
< one
< two
< 
< 
< 
< 
< 
< 
11a4
> one
12a6
> two

And when I do

diff -B two.txt one.txt

I get an empty output, i.e. no difference between the 2 files.
What is happening? Shouldn't diff command be symmetric?

1
diff fil1 file2  

what diff does is it prints the changes required to make file2 identical to file1.

Changes required to make file1 the same as file2 are definitely different from the changes required to do the opposite. Let's take a look at a simple example: file1 contains only one word one and file2 contains only one word two , with diff file1 file2 you get:

1c1
< one
---
> two  

and with diff file2 file1 you get:

1c1
< two
---
> one

So diff is not symmetric, it shouldn't be.

Now regarding the -B option, the manual says:

-B, --ignore-blank-lines        ignore changes where lines are all blank 

-B doesn't mean ignore blank lines from input, it means ignore changes that are blank. if you do diff two.txt one.txt you get:

3d2
<
5d3
<
6a5,12
>
>
>
>
>
>
>
>

all three changes are blank lines, so it prints nothing.

  • But the question is about -B. And how does the second file require no changes to be converted into the first one. Please explain a bit more. – Shraddheya Shendre Dec 6 '16 at 7:31
  • @ShraddheyaShendre see my edited answer. – David Dai Dec 6 '16 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.