title: Remove directory structure when extracting tar archive
body: <p>Suppose I have created a tar archive on Server A using <code>tar -czvf subdir.tgz subdirectory/*</code>. If I have a parallel structure on Server B to which I've copied this tarball and I want to now clobber over that same subdirectory - including removing any files in this subdirectory that are not present in the tar archive - I <em>could</em> <code>cd</code> into the parent directory on Server B, <code>rm -rf subdirectory</code> and then <code>tar -xzvf subdir.tgz</code>. </p>
  
  <p>Is there a way to do this without having to first run the <code>rm</code>?</p>
  
  <p>As a test I put a dummy directory inside Server B at <code>subdirectory/dummy/</code> and then ran <code>tar -xzvf subdir.tgz --recursive-unlink</code> but to my surprise, <code>subdirectory/dummy/</code> was still there. I guess I misunderstood what <code>--recursive-unlink</code> does.</p>
  
  <p>EDIT ==========
  here's an example. I create two directories, which would represent the two "mirrored" directories on two different servers (think of them as server 'a' and server 'b'). In the destination directory ('b') I create an extra dir and some files that will not be present in the tar archive. These I'm expecting to be clobbered and removed. Essentially, all I want in directory 'b' when I'm done is the files from directory 'a'. But, as can be seen from the example, despite <code>--unlink-first</code> and <code>--recursive-unlink</code> the superfluous directory and files are still there after untar.</p>
  
  <pre><code>-bash-3.2$ mkdir tartest
  -bash-3.2$ cd tartest
  -bash-3.2$ mkdir a
  -bash-3.2$ mkdir b
  -bash-3.2$ cd a
  -bash-3.2$ mkdir xyz
  -bash-3.2$ cd xyz
  -bash-3.2$ touch test.1
  -bash-3.2$ touch test.2
  -bash-3.2$ cd ../
  -bash-3.2$ touch test.3
  -bash-3.2$ cd ../b
  -bash-3.2$ mkdir abc
  -bash-3.2$ cd abc
  -bash-3.2$ touch test.4
  -bash-3.2$ cd ../../a
  -bash-3.2$ tar cvf ../archive.tar ./*
  ./test.3
  ./xyz/
  ./xyz/test.2
  ./xyz/test.1
  -bash-3.2$ cd ../b
  -bash-3.2$ tar -xf ../archive.tar --unlink-first --recursive-unlink
  -bash-3.2$ ls
  total 16K
  drwxr-xr-x 4 iponly168797 iponly168797 4.0K Jan 27 13:58 .
  drwxr-xr-x 4 iponly168797 iponly168797 4.0K Jan 27 13:57 ..
  drwxr-xr-x 2 iponly168797 iponly168797 4.0K Jan 27 13:56 abc
  -rw-r--r-- 1 iponly168797 iponly168797    0 Jan 27 13:56 test.3
  drwxr-xr-x 2 iponly168797 iponly168797 4.0K Jan 27 13:56 xyz
  -bash-3.2$ ls xyz
  total 8.0K
  drwxr-xr-x 2 iponly168797 iponly168797 4.0K Jan 27 13:56 .
  drwxr-xr-x 4 iponly168797 iponly168797 4.0K Jan 27 13:58 ..
  -rw-r--r-- 1 iponly168797 iponly168797    0 Jan 27 13:56 test.1
  -rw-r--r-- 1 iponly168797 iponly168797    0 Jan 27 13:56 test.2
  </code></pre>