Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 7 down vote accepted

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 !.*!!
share|improve this answer

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
share|improve this answer
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 posting your answer, you agree to the privacy policy and terms of service.

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