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?


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 !.*!!

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
  • I ended up doing the latter just because in a hurry, but good to know the former exists. – xenoterracide Nov 5 '10 at 11:26
  • Would it be possible to use a command that doesn't require explicitly naming the stripped component (e.g. wordpress)? I need this when I'm extracting GitHub created tarballs which contain the git revision in the stripped component which I don't know in advance. – tjanez Dec 15 '14 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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