Jump to Navigation

232 - diff and patch for Linux

The commands diff and patch form a powerful combination. They are widely used to get differences between original files and updated files in such
a way that other people who only have the original files can turn them into the updated files with just a single patch file that contains only the differences.

1. Create a patch

The most simple way of using diff is getting the differences between two files, an original file and an updated file.

# diff [originalfile] [updatedfile] > patchfile.patch

2. Apply patch

Copy the original file and the patchfile to an other place, and go to that place.

# patch [originalfile] -i patchfile.patch -o [updatedfile]

3. Check the update

You can check this using diff's -s option

# diff -s updatedfile <path to the original updatedfile>/originalfile

4. Patching to rpm package

# cd /usr/src/redhat/BUILD

# cp -R ./cups-1.5.2 ./cups-1.5.2p

* Modify any change for specific files in ./cups-1.5.2p :

# diff -uNr cups-1.5.2/ cups-1.5.2/ > ../SOURCES/package-1.0-my.patch

 Run diff with "-u" to get the unified diff format that is standard for patches.
 "-N" to include any new files from your modified source.
 "-r" to operate recursively.

The top of the patchfile will look something like this :

diff -uNr cups-1.5.2/config.h.in cups-1.5.2p/config.h.in
--- cups-1.5.2/config.h.in      2011-06-16 13:12:16.000000000 -0700
+++ cups-1.5.2p/config.h.in     2012-04-20 10:33:24.000000000 -0700

* Add to %_topdir/SPECS/cups.spec, in the top section where the name, version, and source lines are :

Summary: CUPS
Name: cups
Version: 1.5.2
Release: 1
Epoch: 1
License: GPL
Group: System Environment/Daemons
Source: http://ftp.easysw.com/pub/cups/1.5.2/cups-1.5.2-source.tar.gz
Url: http://www.cups.org
Packager: Anonymous <anonymous@foo.com>
Vendor: Apple Inc.
Patch0: package-1.0-my.patch

there will be a section that deals with preparing for the build. Add a patch command that corresponds with the patch line above :

%patch0 -p1


 Specify how much of preceeding pathname to strip. A num of 0 strips everything,
 leaving just the filename. 1 strips the leading /. Each higher number after that strips another directory from the left.

* Rebuild binary package :

cd /usr/src/redhat
rpmbuild -ba SPECS/cups.spec

You can find all packages created in like /usr/src/redhat/RPMS/x86_64


Shell Script:

Main menu 2

Story | by Dr. Radut