<!DOCTYPE html>
<html>
<head>
  
  <title>Using diff/patch</title>
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  <div class="article" lang="en">
    <div class="titlepage">
      <div>
        <div>
          <h1 class="title"><a name="id2817675" id="id2817675"></a>Using diff/patch</h1>
        </div>
        <div>
          <div class="author">
            <h3 class="author"><span class="firstname">Jeremy</span> <span class="surname">Praissman</span></h3>
          </div>
        </div>
      </div>
      <div></div>
      <hr>
    </div>
    <div class="toc">
      <p><b>Table of Contents</b></p>
      <dl>
        <dt>
          <a href="#id3031110">Using diff</a>
        </dt>
        <dt>
          <a href="#id3033680">Using patch</a>
        </dt>
      </dl>
    </div>
    <div class="sect1" lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both"><a name="id3031110" id="id3031110"></a>Using diff</h2>
          </div>
        </div>
        <div></div>
      </div>
      <div class="procedure">
        <ol type="1">
          <li>
            <p><b class="command">cd</b> to the directory you wish to work in.</p>
            <div class="informalexample">
              <pre class="screen">
[jeremyp@vespers jeremyp]$ cd patch
[jeremyp@vespers patch]$ 
          </pre>
            </div>
          </li>
          <li>
            <p>Create a symlink to the directory containing the original version(s) of the file(s).</p>
            <div class="informalexample">
              <pre class="screen">
[jeremyp@vespers patch]$ ln -s /etc/xinetd.d
[jeremyp@vespers patch]$ 
          </pre>
            </div>
          </li>
          <li>
            <p>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.</p>
          </li>
          <li>
            <p>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.</p>
            <div class="informalexample">
              <pre class="screen">
[jeremyp@vespers patch]$ mv ~/tftp .
[jeremyp@vespers patch]$ 
          </pre>
            </div>
          </li>
          <li>
            <p>Run <b class="command">diff</b>, specifying either specific files or directories. If run against directories, <b class="command">diff</b> 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 <b class="command">diff</b> is run on a single/specific file, however, it's easiest to simply use the same options every time. The output of <b class="command">diff</b> should generally be redirected into a file that can be used by <b class="command">patch</b>. The last example illustrates this.</p>
            <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
              <h3 class="title">Note</h3>
              <p>Do not use any extraneous slashes in file/directory names.</p>
              <p>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.</p>
            </div>
            <div class="example">
              <a name="id3033608" id="id3033608"></a>
              <p class="title"><b>Example 1. specific files</b></p>
              <pre class="screen">
[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]$ 
          </pre>
            </div>
            <div class="example">
              <a name="id3033633" id="id3033633"></a>
              <p class="title"><b>Example 2. directories</b></p>
              <pre class="screen">
[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]$ 
          </pre>
            </div>
            <div class="example">
              <a name="id3033660" id="id3033660"></a>
              <p class="title"><b>Example 3. creating a patch</b></p>
              <pre class="screen">
[jeremyp@vespers patch]$ diff -Naur xinetd.d/tftp ./tftp &gt; tftp.patch
[jeremyp@vespers patch]$ 
          </pre>
            </div>
          </li>
        </ol>
      </div>
    </div>
    <div class="sect1" lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both"><a name="id3033680" id="id3033680"></a>Using patch</h2>
          </div>
        </div>
        <div></div>
      </div>
      <p>The following procedure applies to using a patch created as above.</p>
      <div class="procedure">
        <ol type="1">
          <li>
            <p><b class="command">cd</b> to the directory containing the file you wish to patch. Alternatively, you might use the &#8220;<span class="quote">-d</span>&#8221; option if your version of <b class="command">patch</b> supports it.</p>
            <div class="informalexample">
              <pre class="screen">
[root@vespers root]# cd /etc/xinetd.d
[root@vespers xinetd.d]# 
          </pre>
            </div>
          </li>
          <li>
            <p>Run <b class="command">patch</b> as follows.</p>
            <div class="informalexample">
              <pre class="screen">
[root@vespers xinetd.d]# patch -Np1 &lt; ~jeremyp/patch/tftp.patch 
patching file tftp
[root@vespers xinetd.d]# 
          </pre>
            </div>
          </li>
        </ol>
      </div>
    </div>
  </div>
</body>
</html>
