tar – extract discarding directory structure

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'
                 # or 's#^.+/##x' for discarding all paths

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.


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

pax can do it:

pax -v -r -s '/.*\///p' < archive.tar

or

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

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