Open Source

Linux command: Side by side diff

November 26, 2008, 12:46 PM — 

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.

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

I like it!
Comments

Solaris has always (?) had sdiff

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
| reply

tkdiff

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

Mac OS X Users

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.
| reply
peer-to-peer

jfruh
Apple syncing patent can't come soon enough

pasmith
New Twitter features borrow from 3rd party clients

Esther Schindler
Open Source Changes the Software Acquisition Process

mikelgan
How to set up continuous podcast play on the new iTunes

David Strom
Five important Windows 7 mobility features

sjvn
Guard your Wi-Fi for your own sake                        

Sandra Henry-Stocker
Grepping on Whole Words

 

Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace