How to create and use a patch in Linux

[diff: ] http://linux.about.com/library/cmd/blcmdl1_diff.htm
[patch:] http://linux.about.com/od/commands/l/blcmdl1_patch.htm

Creating a Patch File:

(Creating a patch file by single file)

# ls old/
myfile.c
# ls new/
myfile.c

# diff -Naur old/myfile.c new/myfile.c > my-patch

# more my-patch
--- old/myfile.c 2006-01-01 17:09:19.000000000 +0900
+++ new/myfile.c 2007-01-01 14:04:11.000000000 +0900
@@ -1,6 +1,6 @@
#include

-#define perror(s1, s2) printf("%s: %d \n", (s1), (s2))
+#define perror(s1, s2) printf("%s: %s \n", (s1), (s2))

(Creating a patch file by folder)

# ls -ls
8 drwxr-xr-x 2 root root 4096 Aug 8 14:04 new
8 drwxr-xr-x 2 root root 4096 Aug 8 14:25 old

# diff -Naur old new > my-patch-byfolder

# more my-patch-byfolder
diff -Naur old/myfile.c new/myfile.c
--- old/myfile.c 2006-01-01 17:09:19.000000000 +0900
+++ new/myfile.c 2007-01-01 14:04:11.000000000 +0900
@@ -1,6 +1,6 @@
#include

-#define perror(s1, s2) printf("%s: %d \n", (s1), (s2))
+#define perror(s1, s2) printf("%s: %s \n", (s1), (s2))

The first symbol of each line (+, -, and a blank):

(+) would indicate that this line is to be added.
(-) would indicate that this line is to be removed.

************************

Applying a Patch:

Just change to the correct directory and give the patch command.

# patch -p0

Levels (-p0,-p1, ...etc) in the Patch Command:

# patch -p0

The -p option will optionally strip off directory levels from the patch-file.

- Using a -p0 will expect, from your current working directory, to find a subdirectory called "new", then the "myfile.c" file below that.
- Using a -p1 will strip off the 1st level from the path and expect to find "myfile.c" in the current working directory.
Patch will ignore the "new" directory mentioned in the header of the patch-file.
- Using a -p2 in this example, patch probably would not patch anything.

No comments: