tar question...

andi payn andi_payn at speedymail.org
Wed Oct 29 21:33:58 PST 2003


On Wed, 2003-10-29 at 19:56, Xpression wrote:
> Hi list, the question is: can I tar a hole directory without include the
> tree ??? I mean when I tar all files in a /dir1/dir2/dir3 path, the tar file
> includes me the path too and I want to tar only the filenames in dir3: I'm
> using the syntax tar -czf /path/to/store/myfile.tgz
> /the/path/where/are/the/files, any clue ???? Thanksssss...

I'm not 100% sure I understand exactly what you want, so I'm going to be
extra careful; I apologize if it sounds like I'm talking down to you.

The short answer is to use relative paths: cd to where you want to
start, and tar from there. If that doesn't immediately answer your
question, read on.

Let's say your directory structure looks like this:

  dir1
    dir2
      dir3
        file1
        file2
        dir4
          file3

You want a tarball that contains 'dir3', 'dir3/file1', 'dir3/file2',
'dir3/dir4', and 'dir3/dir4/file3', right? If so, do this:

# cd /dir1/dir2
# tar czf /path/to/store/myfile.tgz dir3

If you instead want a tarball that contains 'file1', 'file2', 'dir4',
and 'dir4/file3', instead do this:

# cd /dir1/dir2/dir3
# tar czf /path/to/store/myfile.tgz *

You can do this all in one command with the -C parameter, but it's
easier to remember cd than how -C works (globbing gets more complicated,
-C works differently on different platforms/tars, and there's extra
stuff to worry about if you use it together with -T).

If you only want 'file1' and 'file2', and not 'dir4' or any of its
contents, do this (the 'n' means to not recurse into subdirectories):

# cd /dir1/dir2/dir3
# tar czfn /path/to/store/myfile.tgz *

If you want 'file1' and 'file2' and 'file3' but not 'dir4', there's no
simple way to do this. You'll have to do something like this (the 'h'
means to dereference symlinks and store the original file they point
to):

# pushd `mktemp -dt tar`
# ln -s `find /dir1/dir2/dir3/` ./
# tar czfh /path/to/store/myfile.tgz *
# rm -rf `pwd`
# popd

This isn't perfect (it won't work if there are too many files, and if
any of the original files are symlinks they'll be dereferenced too--and
if you plan to do this more than once you'll be better off writing a
script that wraps this stuff up), but it should give you an idea.




More information about the freebsd-questions mailing list