Ubuntu – Simple diff command output

diff()

I've seen many examples of the use of the diff command, but none detailing its basic use. Here is the content of the two files I would like to use:

cat -A file1.txt
a$
b$
c$
d$

cat -A file2.txt
b$
c$
d$
e$

If I use the diff command like so:

diff file1.txt file2.txt

I get:

1d0
< a
4a4
> e

What I would like to know is what do they mean by lines 1 and 0 in 1d0, and 4 and 4 in 4a4, in addition to the less than and greater than signs. More generally, how come there is a less than sign before the a as opposed to a greater than sign? What's the difference?

Best Answer

From the diff manual:

lar
Add the lines in range r of the second file after line l of the first file. For example, ‘8a12,15’ means append lines 12–15 of file 2 after line 8 of file 1; or, if changing file 2 into file 1, delete lines 12–15 of file 2.

fct
Replace the lines in range f of the first file with lines in range t of the second file. This is like a combined add and delete, but more compact. For example, ‘5,7c8,10’ means change lines 5–7 of file 1 to read as lines 8–10 of file 2; or, if changing file 2 into file 1, change lines 8–10 of file 2 to read as lines 5–7 of file 1.

rdl
Delete the lines in range r from the first file; line l is where they would have appeared in the second file had they not been deleted. For example, ‘5,7d3’ means delete lines 5–7 of file 1; or, if changing file 2 into file 1, append lines 5–7 of file 1 after line 3 of file 2.

The > and < make sense if you look at the side-by-side output format:

<
The files differ and only the first file contains the line.

>
The files differ and only the second file contains the line.

Example outputs from the manual:

  • side-by-side:

    The Way that can be told of is n   <
    The name that can be named is no   <
    The Nameless is the origin of He        The Nameless is the origin of He
    The Named is the mother of all t   |    The named is the mother of all t
                                       >
    Therefore let there always be no        Therefore let there always be no
      so we may see their subtlety,           so we may see their subtlety,
    And let there always be being,          And let there always be being,
      so we may see their outcome.            so we may see their outcome.
    The two are the same,                   The two are the same,
    But after they are produced,            But after they are produced,
      they have different names.              they have different names.
                                       >    They both may be called deep and
                                       >    Deeper and more profound,
                                       >    The door of all subtleties!
    
  • normal:

    1,2d0
    < The Way that can be told of is not the eternal Way;
    < The name that can be named is not the eternal name.
    4c2,3
    < The Named is the mother of all things.
    ---
    > The named is the mother of all things.
    > 
    11a11,13
    > They both may be called deep and profound.
    > Deeper and more profound,
    > The door of all subtleties!