Using diff/patch

Jeremy Praissman


Table of Contents

Using diff
Using patch

Using diff

  1. cd to the directory you wish to work in.

    [jeremyp@vespers jeremyp]$ cd patch
    [jeremyp@vespers patch]$ 
              
  2. Create a symlink to the directory containing the original version(s) of the file(s).

    [jeremyp@vespers patch]$ ln -s /etc/xinetd.d
    [jeremyp@vespers patch]$ 
              
  3. Create a directory to contain the new version(s) of the file(s). If you are creating a patch for a single file, this step may be skipped.

  4. Create/place the new version(s) of the file(s) into the directory you just created. If you are creating a patch for a single file, the new version may simply be placed into the directory you are currently in.

    [jeremyp@vespers patch]$ mv ~/tftp .
    [jeremyp@vespers patch]$ 
              
  5. Run diff, specifying either specific files or directories. If run against directories, diff as invoked below will list all differences between the directories, their subdirectories, etc. This includes treating files present below one directory but absent below the other directory as empty (see the example below). Some of the options used in both command lines below have no effect when diff is run on a single/specific file, however, it's easiest to simply use the same options every time. The output of diff should generally be redirected into a file that can be used by patch. The last example illustrates this.

    Note

    Do not use any extraneous slashes in file/directory names.

    In general, it's best to have the same number of lead directory components for both the old and new versions of the file. This makes it easier to use the generated patch.

    Example 1. specific files

    [jeremyp@vespers patch]$ diff -Naur xinetd.d/tftp ./tftp 
    --- xinetd.d/tftp       2003-12-17 13:11:20.000000000 -0500
    +++ ./tftp      2004-01-22 11:46:14.479497688 -0500
    @@ -10,7 +10,7 @@
            wait                    = yes
            user                    = root
            server                  = /usr/sbin/in.tftpd
    -       server_args             = -s /tftpboot
    +       server_args             = -p -u tftpd -s /tftpboot
            disable                 = yes
            per_source              = 11
            cps                     = 100 2
    [jeremyp@vespers patch]$ 
              

    Example 2. directories

    [jeremyp@vespers patch]$ ls example1
    bar  foo
    [jeremyp@vespers patch]$ ls example2
    foo
    [jeremyp@vespers patch]$ cat example1/bar
    arrrr
    [jeremyp@vespers patch]$ cat example1/foo
    foobar
    [jeremyp@vespers patch]$ cat example2/foo 
    barfoo
    [jeremyp@vespers patch]$ diff -Naur example2 example1
    diff -Naur example2/bar example1/bar
    --- example2/bar        1969-12-31 19:00:00.000000000 -0500
    +++ example1/bar        2003-12-17 12:48:48.000000000 -0500
    @@ -0,0 +1 @@
    +arrrr
    diff -Naur example2/foo example1/foo
    --- example2/foo        2003-12-17 12:46:26.000000000 -0500
    +++ example1/foo        2003-12-17 12:46:13.000000000 -0500
    @@ -1 +1 @@
    -barfoo
    +foobar
    [jeremyp@vespers patch]$ 
              

    Example 3. creating a patch

    [jeremyp@vespers patch]$ diff -Naur xinetd.d/tftp ./tftp > tftp.patch
    [jeremyp@vespers patch]$ 
              

Using patch

The following procedure applies to using a patch created as above.

  1. cd to the directory containing the file you wish to patch. Alternatively, you might use the “-d” option if your version of patch supports it.

    [root@vespers root]# cd /etc/xinetd.d
    [root@vespers xinetd.d]# 
              
  2. Run patch as follows.

    [root@vespers xinetd.d]# patch -Np1 < ~jeremyp/patch/tftp.patch 
    patching file tftp
    [root@vespers xinetd.d]#