Diff, Patch, and Friends
When someone changes a file that other people have copies of (source code, documentation, or just about any other text file), they often send patches instead of (or in addition to) making the entire new file available. If you have the old file and the patches, you might wish that you could have a program apply the patches. You might think that normal diff format, which was made to look like input to the ed program, would be the best way to accomplish this.
As it turns out, this is not true.
A program called patch has been written which is specifically designed to apply patches to files (change the files as specified in the patch). It correctly recognizes all the formats of patches and applies them. With unified and context diffs, patch can usually apply patches, even if lines have been added or removed from the file, by looking for unchanged context lines. Only if the context lines have themselves been changed is patch likely to fail.
To apply patches with patch, you normally have a file containing the patch (we'll call it patchfile), and then run patch:
patch < patchfile
Patch is very verbose. If it gets confused by anything, it stops and asks you in English (it was written by a linguist, not a computer scientist) what you want to do. If you want to learn more about patch, the man page is unusually readable.
If you read the RCS article in the May issue (Take Command: Keeping Track of Change, LJ #25, May 1996), you may have noticed that the article talked a bit about a program called rcsdiff. rcsdiff is really just a front end to diff. That is, it looks for arguments that it understands (such as revision numbers and the filename) and prepares two files representing the two versions of the file you are examining. It then calls diff with the remaining options. The RCS article used -u to get the unified format without explaining what it meant, but you can use -c to get context diffs, or use -U lines to choose the amount of context you get in a unified diff, or use any other diff options you like.
You may notice that rcsdiff produces more verbose output than normal diff. From the RCS article:
rcsdiff -u -r1.3 -r1.6 foo ============================================== RCS file: foo,v retrieving revision 1.3 retrieving revision 1.6 diff -u -r1.3 -r1.6 --- foo 1996/02/01 00:34:15 1.3 +++ foo 1996/02/01 01:05:28 1.6 -1,2 +1,6 This is a test of the emergency -RCS system. This is only a test. +RCS version control system. +This is only a test. + +I'm now adding a few lines for +the next version.
It looks just like a normal unified diff except for the first 5 lines.
This doesn't prevent you from sending patches to people. The patch program is extremely good about ignoring extraneous information. It can even ignore news or mail headers, extra comments written in a file outside a patch, and people's signatures following patches. Patch tells you when it is determining whether text is part of a patch or not by saying “Hmm...”
If you don't care how two files differ, but just want to know whether they differ, the cmp program will tell you. It works not only for text files, but also for binary files. In this example, the files 5 and 6 are different; 2 and 4 are the same:
cmp 5 6 5 6 differ: char 159, line 4 cmp 2 4
Notice that when two files are the same, cmp doesn't say anything at all. It only tells you explicitly if the files have been changed. For use in writing shell scripts, cmp also returns true if the files are the same and false if they don't, as shown by this shell session:
if cmp 5 6 ; then echo "same" else echo "different" fi 5 6 differ: char 159, line 4 different if cmp 2 4 ; then echo "same" else echo "different" fi same
There are several other programs with related functionality. In particular, diff3 can be used to merge together two different files that have both been edited from a common ancestor file. That common ancestor must exist in order for diff3 to work correctly.
The info pages which are shipped with diff are probably installed on your system. If you want to learn more about diff, try the command info diff or use info mode from within emacs or jed.
diff, wdiff, patch, and emacs are available via ftp from the canonical GNU ftp archive, prep.ai.mit.edu, in the directory /pub/gnu/
Michael K. Johnson His wife Kim likes A. A. Milne and briefly studied Latin (unlike Michael, whose experience with Latin was limited to singing in choir), which is why she owns Winnie Ille Pu as well as Tela Charlottae (Charlotte's Web).
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Home, My Backup Data Center
- Readers' Choice Awards
- What's the tweeting protocol?
- New Products
- RSS Feeds
- Dart: a New Web Programming Experience
- Reply to comment | Linux Journal
8 hours 38 min ago - Reply to comment | Linux Journal
11 hours 10 min ago - Reply to comment | Linux Journal
12 hours 27 min ago - great post
13 hours 2 min ago - Google Docs
13 hours 25 min ago - Reply to comment | Linux Journal
18 hours 13 min ago - Reply to comment | Linux Journal
19 hours 35 sec ago - Web Hosting IQ
20 hours 34 min ago - Thanks for taking the time to
22 hours 11 min ago - Linux is good
1 day 8 min ago




Comments
The first comment asked for
The first comment asked for reasons. Here are a few.
1. Years of fighting with graphical tools can lead one to learn command line tools.
2. A major difference between the two is the former can be used in scripts.
3. Another significant difference is robustness. It's generally much easier to crash a graphical tool than a well-designed command line one.
4. Lastly, command line tools most often contain less code and require less system resources to operate, often making them better suited to work faster and more efficiently than graphical tools.
Smaller, faster and more reliable. Built for automation. A higher learning curve may be a trade off for higher performance.
Why?
Why in the world would I want to fight with command line tools when trying to compare and merge versions? Graphical diff and merge tools have existed for decades.
Now if you can show me a diff and merge tool that understands the syntax of the file being compared and I'll be far more interested. I have seen merge tools eat a brace way too often.
I think the images for figure
I think the images for figures 1 and 2 are broken. When I follow the links to view these figures, the images don't show up in my browser window.
using diff and patch
If I use diff -Naur to generate a patch symbolic links are not respected. e.g. see below;
How can I get patch/diff to respect symbolic links?
>---------------------------------------------------<
$ ll foo*
foo:
total 4
lrwxrwxrwx 1 foo users 5 Jun 9 13:04 link -> stuff
-rw-r--r-- 1 foo users 12 Jun 9 13:04 stuff
foo2:
total 4
lrwxrwxrwx 1 foo users 5 Jun 9 13:04 link -> stuff
-rw-r--r-- 1 foo users 12 Jun 9 13:04 stuff
$ diff -Naur foo foo2 > patch
$ mv foo2 foo2.orig
$ patch -p0 < patch
patching file foo/link
patching file foo/stuff
$ ll foo
total 8
-rw-r--r-- 1 foo users 27 Jun 9 13:08 link
-rw-r--r-- 1 foo users 27 Jun 9 13:08 stuff
>---------------------------------------------------<
Working with directories
This article is missing info on patching multiple files.
See here: http://lists.gnu.org/archive/html/help-gnu-utils/2004-06/msg00024.html for examlpe.
Re: Diff, Patch, and Friends
Nice article! I now link to it from
my "Howto contribute to an open source project" tutorial,
www.kegel.com/academy/opensource.html
Re: Diff, Patch, and Friends
You may want to link to the manpages for the free versions
of diff and patch too, instead of only the GNU versions:
http://mirbsd.bsdadvocacy.org/man1/diff.htm
http://mirbsd.bsdadvocacy.org/man1/patch.htm
To the editor: ed(1) is by no means obsolete; I'm actually
faster with ed than with vi (whose modus operandi is
cruelly to a wordstar-compatible editor user like me).
http://mirbsd.bsdadvocacy.org/man1/ed.htm
You didn't mention diff3 either, did you?