Tar question


Just a quick question:
Is there any argument to send to TAR so it won't include the directory structure to the files being included in an archive?

Essentially I am trying to write a quick tool like DropStuff but to archive things into a .tar and then compress it to .tgz.

I checked the man page but it isn't of any help.

I am assuming that this must be possible. Any help?



so you want to flatten the heirarchy?

what happens if you have a/b/c/foo
and a/b/x/foo ????

Wouldn't tar overwrite the first w/ the second?

You can do it by walking the tree and making a flat directory of symlinks
and giving tar the -h (?) (resolve links) option.


Defender of the Realm
I'm not sure I understand your question, but I'll take a stab at it anyway...

I think the problem is that you are giving tar absolute path names rather than relative path names.

To wit:
Let's say you have a directory:

that contains a bunch of stuff you want to archive (tar).
You could do it this way:

tar cvf foo.tar /Users/alex/foo

but in that case when you untar, the entire absolute path will be maintained; that is to say, tar will try to put everything in:

OTOH, you can do this:

cd /Users/alex
tar cvf foo.tar ./foo

In UNIX, "." is a shorthand identifier for the current directory.

Now, when you untar, it will put things in:
regardless of what "." is. In other words, everything is relative to the current directory rather than defined absolutely from "/".

A few more words about absolute vs relative paths:
You can always tell an absolute path because it starts with a "/" This makes sense, "/" is the root of the filesystem, so if you define a path starting with "/" you're starting at the top.

Let's say /Users/alex/foo has a directory called "bar" inside, which in turn contains a file called "baz". We could refer to that file as:
/Users/alex/foo/bar/baz (an absolute path)

Or, from within /Users/alex as:
foo/bar/baz (a relative path: relative to the current directory).

That pretty much covers absolute vs relative paths... it's an important concept to wrap your head around for UNIXy purposes.

One final tip:
you can give tar the "Z" (that's a capital zee) flag and it will gzip in one step:

tar cvfZ foo.tgz ./foo

I know I answered _a_ question.... I just hope it was _your_ question :)

Good luck!



Most all tar's will strip the leading '/' from an absolute path and most dont give you any choice.

Gnutar at least gives you a choice.

Local file selection:
-C, --directory=DIR change to directory DIR
-T, --files-from=NAME get names to extract or create from file NAME
--null -T reads null-terminated names, disable -C
--exclude=PATTERN exclude files, given as a globbing PATTERN
-X, --exclude-from=FILE exclude globbing patterns listed in FILE
-P, --absolute-names don't strip leading `/'s from file names