Product SiteDocumentation Site

7.4. Comparing Multiple Files

The above example contained a relatively simple patch. In general, patches you encounter are likely to be much larger -- representing bigger changes to the code such as the addition of a new feature, or a bug or security fix that may touch multiple files within multiple directories of the code. These larger patches are still generated in the same manner by using the diff command. Instead of comparing two files, diff can be used to compare two directories and all of the files contained within.
Extend the example above by working with a directory, hello/, that contains two files: the hello.c program you started with in the first example plus a README file that describes the program. The directory contents and the README file contents are shown below:
$ ls hello/
hello.c  README
$ cat hello/README
This is a simple program which outputs "hello" to the screen.
You are going to be making changes to each of these files, so instead of making a backup of each file within the hello/ directory, copy the entire directory:
$ cp -r hello hello.orig
Now edit the files in the hello/ directory to include the exclamation point in the printf statement in hello.c and to update the README file to contain:
This is a simple program which outputs "hello" to the screen in an enthusiastic manner.
To create a patch file showing the changes, pass it the directory names rather than the file names:
$ diff -u hello.orig hello 
diff -u hello.orig/hello.c hello/hello.c 
--- hello.orig/hello.c  2010-03-11 09:11:29.612888467 -0800 
+++ hello/hello.c   2010-03-11 09:14:39.406763357 -0800 
@@ -5,6 +5,6 @@ 
 #include <stdio.h> 
 
 int main() { 
-    printf("Hello, World.\n"); 
+    printf("Hello, World!\n"); 
     return 0; 
 } 
diff -u hello.orig/README hello/README 
--- hello.orig/README   2010-03-11 09:11:29.612888467 -0800 
+++ hello/README    2010-03-11 09:14:58.175763807 -0800 
@@ -1 +1 @@ 
-This is a simple program which outputs "hello" to the screen. 
+This is a simple program which outputs "hello" to the screen in an enthusiastic manner.
The output is similar to the previous example, however there are now multiple hunks shown -- one for each change. In this manner, you can create large changes that touch multiple files and encapsulate all of those changes in a single patch file to easily share with others.
$ diff -u hello.orig hello > hello-excitement-exercise-X.Y.patch