Linux command: Side by side diff

By Sandra Henry-Stocker  5 comments

Every now and then I come across a feature of some Linux command that works so well that I have trouble understanding how I managed without it for so long. The one that I most recently discovered is the side by side option for the diff command.

Sure, the designations that diff offers to identify differences between two files are useful. But I don't find them particularly intuitive.


# diff frogs frogs2
7,8c7,8
< Canyon Treefrog
< (Hyla arenicolor)
---
> Florida Cricket Frog
> (Acris gryllus dorsalis)
28,30d27
< Western Chorus Frog
< (Pseudacris triseriata triseriata)
<
33a31,33
> Southern Chorus Frog
> (Pseudacris nigrita nigrita)
>
35a36,38
>
> Little Grass Frog
> (Pseudacris ocularis)

The standard diff output is meant to be terse. It captures the differences between two files in such a way as to make it possible to convert one file to the other if you only end up with one of them. The "7,8c7,8" string, for example, captures the fact that lines 7 and 8 are different between the two files. If you want to turn frogs into frogs2, you exchange the following two lines with two following the "---". To turn frogs2 into frogs, you do the reverse. One way to keep many generations of an important file without duplicating most of the content over and over again would to be to save the diffs.

If you are only interested in whether the files are different, you can throw away the output from the diff command (i.e., send it to /dev/null) and look at $? (the return code). If the files are different, $? will be set to 1. Alternately, you could use the simpler cmp command which reports the first difference between the two files and then stops.

The side by side equivalent of this output is more wordy, but I instantly understand the nature and extent of the differences. The vertical bar ("|") characters point out differences between the two files while the "<" and ">" characters note where content appears in only one or the other of the two files.


# diff -y -W 80 frogs frogs2
Blanchard's Cricket FrogBlanchard's Cricket Frog
(Acris crepitans blanchardi)(Acris crepitans blanchardi)

Southern Cricket FrogSouthern Cricket Frog
(Acris gryllus gryllus)(Acris gryllus gryllus)

Canyon Treefrog| Florida Cricket Frog
(Hyla arenicolor)| (Acris gryllus dorsalis)

Squirrel Tree FrogSquirrel Tree Frog
(Hyla squirella)(Hyla squirella)

Green TreefrogGreen Treefrog
(Hyla cinerea)(Hyla cinerea)

Birdvoiced TreefrogBirdvoiced Treefrog
(Hyla avivoca)(Hyla avivoca)

Gray TreefrogGray Treefrog
(Hyla versicolor - Hyla chrysocelis)(Hyla versicolor - Hyla chrysocelis)

Pine Woods TreefrogPine Woods Treefrog
(Hyla femoralis)(Hyla femoralis)

Northern Spring Peeper Northern Spring Peeper
(Pseudacris crucifer crucifer) (Pseudacris crucifer crucifer)

Western Chorus Frog <
(Pseudacris triseriata triseriata) <
<
Upland Chorus FrogUpland Chorus Frog
(Pseudacris triseriata feriarum)(Pseudacris triseriata feriarum)

> Southern Chorus Frog
> (Pseudacris nigrita nigrita)
>
Ornate Chorus FrogOrnate Chorus Frog
(Pseudacris ornata)(Pseudacris ornata)
>
> Little Grass Frog
> (Pseudacris ocularis)

Cuban TreefrogCuban Treefrog
(Osteopilus septentrionalis)(Osteopilus septentrionalis)

A diff command with the side by side feature can be added to Solaris systems via the diffutils package (diffutils-2.8.1-sol10-sparc-local.gz). When you want a human-friendly listing of file differences that doesn't give you anything you will need to decode, this is the command to use.

5 comments

    Anonymous 49 weeks ago
    There is so much that you can do with this. This is what makes this so good. Keep up the good work. accutane lawsuits
    Anonymous 3 years ago
    For those with color terminals and vim installed, "vimdiff" is quite intuitive, especially if you're used to running split screen vim.
    cemzafer
    cemzafer 3 years ago
    Great article Sandra, as always. For Mac OS X users can use opendiff command. it opens files in graphical environment and points out the differences.
    Anonymous 3 years ago
    As always I enjoyed your article Sandra. Just wanted to add I've come to really like using tkdiff, a very nice graphical wrapper around diff that is very portable. I found it here http://sourceforge.net/projects/tkdiff .
    Anonymous 3 years ago
    It is not really necessary to add diffutils to Solaris for side-by-side diff comparisons. It may be useful for consistency in scripts, but side-by-side diff is more of a human interface and not really a scripting tool.I have been using Solaris (on SPARC) for about 13 years (and SunOS before that). It has always had sdiff. I can't remember if it was in SunOS since that was a while ago. As I recall, sdiff was used under the graphical interface for WorkShop (the compiler tools).Gerard

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      Open SourceWhite Papers & Webcasts

      White Paper

      Consolidating SAP Applications to Linux on Power by IDC

      IDC studied a group of enterprises that had deployed SAP applications on IBM Power Systems servers running Linux server operating environments and had been working with those systems for several years. Learn about the results...

      White Paper

      An Interactive eGuide: Open Source

      By now, enterprises are well aware of the benefits of open-source software, which boasts a clean design, reliability, and maintainability, as well as support for standards and community values. But perhaps the biggest benefit is quality; since open-source software users have access to source code, bug fixes and enhancements come from multiple sources, often resulting in superior software.

      See more White Papers | Webcasts

      Ask a question

      Ask a Question