unzip has a nifty option -j, whereby the directory structure of the archive is discarded, and all files are extracted into the same directory.

Is there a way of making tar work in the same way? Nothing in the man page seems to indicate so.

So, is there an alternative, preferably Free Software, tool that will do that?

link|improve this question
feedback

4 Answers

up vote 5 down vote accepted

You can do it fairly easily in two steps. Adapt as necessary:

$ mkdir /tmp/dirtree
$ tar xfz /path/to/archive -C /tmp/dirtree
$ find /tmp/dirtree -type f -exec mv -i {} . \;
$ rm -rf /tmp/dirtree
link|improve this answer
this is how i'd do it (using find -exec mv -i instead of find | xargs).... simple steps i can repeat on any system without having to carry around a script to do it for me. – quack quixote Nov 18 '09 at 5:04
Very nice. Learnt a little something about the unixy way of living from this answer. Thanks. – Benji XVI Feb 19 '10 at 1:41
feedback

GNU tar lives on featuritis, so naturally also has some options for that.
http://www.gnu.org/software/tar/manual/html_node/transform.html

If you just want to remove a few path segments, then --strip-components=n or --strip=n will often do:

 tar xvzf tgz --strip=1

But it's also possible to regex-rewrite the files to be extracted (flags are --transform or --xform and accept ereg with the /x modifer):

 tar xvzf tgz --xform='s#^[^/]+#.#x'

For listing a tar you need the additional --show-transformed option:

 tar tvzf tgz --show-transformed --strip=1 --xform='s/abc/xyz/x'

I believe the rewriting options also work for packing, not just for extracting. But pax has obviously a nicer syntax.

link|improve this answer
feedback

pax can do it:

pax -v -r -s '/.*\///p' 

or

zcat archive.tar.gz | pax -v -r -s '/.*\///p'

You can check the name replacement operation first by omitting the -r option.

link|improve this answer
+1 for pax solution – Felipe Alvarez Sep 27 '11 at 5:26
feedback

A possible solution that doesn't require installing anything.

  1. use a tar tvf to grab all the files from the tarball
  2. Extract those files individually - have tar extract to stdout & redirect to $filename

    tar -tvf $1 | grep -v "^d" | \
                  awk '{for(i=6;i<NF+1;i++) {printf "%s ",$i};print ""}' |\
                  while read filename
                  do
                     tar -O -xf $1 "$filename" > `basename "$filename"`
                  done
    

save as extract.sh and run as extract.sh myfile.tar. It will also overwrite any duplicate filenames encountered in the directories pulled from the tarball.

link|improve this answer
Clever! Between that method and programmatically the behaviour isn’t hard to achieve. I’m accepting yours because it’s the more “superuser-y” answer. – Benji XVI Nov 17 '09 at 23:49
Actually I just tested it on some a dir tree at home & It fails on files containing spaces. I'll see if I can't post an update. – DaveParillo Nov 18 '09 at 0:24
Fixed it, but it's not nearly as pretty as it was. – DaveParillo Nov 18 '09 at 0:39
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.