How do I extract a specific directory from a tarball? and strip a leading directory?
I want to extract a specific directory from the wordpress tarball. specifically wp-includes
to .
. It appears that the directory structure inside the tarball is wordpress/wp-includes
but I just need./wp-includes
once it’s been extracted, no leading wordpress directory. How would I do this?
Assuming you have GNU tar, you can use --strip-components
:
$ tar xaf tarball.tar.gz --strip=1 wordpress/wp-includes
I believe current versions of BSD tar also support --strip-components
. In the worst case, you could do:
$ tar xzf tarball.tar.gz wordpress/wp-includes
$ mv wordpress/wp-includes .
$ rmdir wordpress
To extract a specific directory (and its contents, recursively), just pass it as an extra argument on the command line. With GNU tar, you can strip a leading directory with the --strip-components
option (and more generally transform the file names with --transform
). On non-Linux systems, you can use pax (it’s in POSIX, but some Linux distributions omit it from their default installation) and its -s
option.
tar xf foo.tar --strip-components=1 wordpress/wp-includes #GNU tar
pax -r <foo.tar -pp -s '!^wordpress/!!' wordpress/wp-includes
You can merge the inclusion list with the rewriting rules by appending a rule to rewrite everything to the empty name (which means “don’t extract”; the rule only applies if the previous rules didn’t match).
pax -r <foo.tar -pp -s '!^wordpress/(wp-includes/)!1!' -s !.*!!