I am trying to understand the linux diff command on two files whose lines are just permutation of each other but not able to grok the output that it generates. Consider the three commands below:
[myPrompt]$ cat file1
apples
oranges
[myPrompt]$ cat file2
oranges
apples
[myPrompt]$ diff file1 file2
1d0
< apples
2a2
> apples
Can someone explain the above cryptic output from diff.
- Why there is no mention of "oranges" at all in the output?
- What does
1d0
and2a2
mean?
I understand from this answer that :
"<" means the line is missing in file2 and ">" means the line is missing in
file1
BUT that doesn't explain why oranges is missing in the output.
Best Answer
To understand the report, remember that
diff
is prescriptive, describing what changes need to be made to the first file (file1
) to make it the same as the second file (file2
).Specifically, the
d
in1d0
means delete and thea
in2a2
means add.Thus:
1d0
means line 1 must be deleted infile1
(apples
).0
in1d0
means line 0 is where they would have appeared in the second file (file2
) had they not been deleted. That means when changingfile2
tofile1
(backwards) append line 1 offile1
after line 0 offile2
.2a2
means append the second line (oranges
) fromfile2
to the now second line offile1
(after deleting the first line infile1
,oranges
switched to line 1)