63

How to I extract a subdirectory in a tarball into the current directory?

Example, the tarball from wordpress:

wordpress/
wordpress/wp-trackback.php
wordpress/wp-config-sample.php
wordpress/wp-settings.php
wordpress/wp-rss2.php
wordpress/readme.html
wordpress/index.php
...

How do I extract everything under wordpress/ into the current directory? In otherwords, it will not create a wordpress directory.

I've tried this with no luck:

tar xvfz latest.tar.gz wordpress -C ./

I know I can extract it normally and move it back, but I figure there has to be a way to do it in one shot.

Jason Coco
  • 77,985
  • 20
  • 184
  • 180
Cory R. King
  • 2,766
  • 1
  • 24
  • 22
  • To those -1ing and suggesting closing this question: how come? – j_random_hacker May 10 '09 at 16:31
  • 3
    I have had this exact problem come up in a programming context. Let's try to be a bit more broadminded, people -- if s/wordpress/kernel_source/g is enough to make a question "programming related", it was "programming related" to begin with. – j_random_hacker May 10 '09 at 16:36
  • 1
    @j_random_hacker - the question is not programming related. the question is basically "how do I use the tar utility on a unix system?" there is nothing programming related about that. it might go on serverfault or on a forum for using unix. – Jason Coco May 10 '09 at 16:39
  • 2
    There is no better place than here to ask such a question. If this place excludes "non-programming" questions, the scope of the website needs to be broadened to include questions such as mine. Plus, there are many similar questions about tar that were not closed on these grounds (I checked before posting this). If you are gonna close this, you should close those as well. Or should I slightly reword this question to ask "how do I extract $RANDOM_LIBRARY" into the current directory" so it sounds like it is programming related, but really me cheating the system? – Cory R. King May 10 '09 at 16:48
  • 6
    The guy's even trying to untar his php files, for pete's sake. This site isn't just about programming, it's about knowing how to use the tools that enable it. That's why we have rich sections on vim and emacs. Tar is one of those tools, and passing command line arguments is just a simple scripting language. – rampion May 10 '09 at 16:49
  • 1
    I mean, look over at the related stuff... there are a ton of "non-programming" questions that were not closed. Why single this out... I searched far and wide for an answer to this question before asking here. I mean--how is this +5 one programming related: http://stackoverflow.com/questions/223656/untar-ungz-gz-tar-how-do-you-remember-all-the-useful-options – Cory R. King May 10 '09 at 16:50
  • 1
    @Jason: Thanks for responding. I see the value in not letting SO become "diluted" with non-programming stuff, but I think an awful lot of valid questions on here are about indirectly related things. If we cut out anything that could possibly be of value to a non-programmer, a lot of high-quality content would be lost, don't you think? – j_random_hacker May 10 '09 at 16:52
  • 1
    @Cory - if I saw those, I would have voted to close them. Extracting files from an archive into the current directory is not programming related, unless you're asking about an issue *writing* a tool that extracts files into the current directory. Your question is re: how to use the *basic* tools on a unix system. As such, it belongs in a forum about *using* the basic tools on a unix system. It's nothing against you or your question. It's just not programming related no matter what directory you want to extract to or what is in the archive. – Jason Coco May 10 '09 at 17:02
  • @rampion - This site is just about programming, read the faq. It's also community based and many of the things with vi and emacs are programming vi and emacs to do specific things, or about programming tools like compilers and interpreters and such. As a community site, however, people may very well choose that this *is* programming related, even tho they'd be extremely hard pressed to make a true, logical case for why it were (except that the word php exists in the question somewhere... come on!) – Jason Coco May 10 '09 at 17:04
  • @Jason: I think you can still vote to close those other questions (I don't think there's a time limit). – j_random_hacker May 10 '09 at 17:05
  • @j_random_hacker - I think that is true in part, but there are some basics that are really a stretch, and IMHO, this is one of them. I don't know that anyone could convince me that known how to use tar to extract files in the current directory and strip the first path component of the archive was programming related and not related to administration. – Jason Coco May 10 '09 at 17:07
  • @j_random_hacker - I could but I don't go searching out questions just to close them. I read this question and felt that it was more appropriate for serverfault or a basic unix tools forum, so I voted to close it. – Jason Coco May 10 '09 at 17:08
  • @j_random_hacker - btw, changing the question from wordpress to my_prog just obfuscates his question, it certainly doesn't make the question more programming related. It's still about "how do I use tar to do administrative stuff". I rolled back to his original second revision. – Jason Coco May 10 '09 at 17:13
  • @Jason: I did check the faq, and this qualifies as "of interest to at least one other programmer somewhere". tar is a programming tool, and like I said before, knowing how to invoke it is a type of programming. Just as much as knowing how to invoke API calls. – rampion May 10 '09 at 17:18
  • @Jason: Sure, my search-and-replace did not change any material properties of the question, I was trying to make a point. Given that it makes no difference to the question, if you feel strongly enough to roll my change back, then I think it's a little inconsistent of you not to vote to close the link provided in Cory's comment. (Not suggesting you actively search for them yourself, just the +5 question with 12 answers that Cory mentioned.) – j_random_hacker May 10 '09 at 17:35
  • The close police is going way over its head lately. – kch May 10 '09 at 22:52
  • 1
    @rampion - tar is an archive tool and tar files are a useful & popular way of moving collections of files around the internet, nothing more. @j_random_hacker - your edits were factious, so i rolled them back--IMHO they didn't make any point. i looked at that link, it's from October. Had I seen it then, I probably would have voted to close it. I can't see any point in closing it now--it would just resurrect a pointless post (again, IMHO). – Jason Coco May 10 '09 at 22:58
  • Jason, I respect your dedication, but you risk turning this place into a wikipedia. Wikipedia is a cesspool of power-tripping admins with too much time on their hands. I dont think you fall into that category, but care is needed to avoid having this place fall into their trap. – Cory R. King May 11 '09 at 02:44

2 Answers2

115

Why don't you untar normally, then just:

mv wordpress/.* .
mv wordpress/* .
rmdir wordpress

But alas, there's:

tar --strip-components=1 -zxvf wordpress.tgz
kch
  • 77,385
  • 46
  • 136
  • 148
  • 4
    I was hoping there was a way to do it in one command. – Cory R. King May 10 '09 at 16:27
  • 2
    Interestingly, strip components is an option for my FreeBSD boxen, but not for some of my older linux installs. – Cory R. King May 10 '09 at 16:30
  • +1. Note that, if the tarfile contains a file or directory named wordpress/wordpress, your second mv command will fail. (But kudos for catching the hidden file case.) – j_random_hacker May 10 '09 at 16:33
  • 4
    I like --strip-components=1, very nice! –  Apr 04 '11 at 03:15
  • 7
    The first command won't work - `mv` will try to move `wordpress/.` and `wordpress/..` to `.`, which will fail. – Nathan Osman Nov 29 '11 at 05:42
  • 5
    On my flavor of `tar` (CentOS 6.3) `--strip-components` needs to be specified *after* the other arguments. Very useful option, thanks for pointing it out. – Kaivosukeltaja Mar 04 '13 at 09:14
  • --strip-components=1 does the job but I don't think it's very nice. a shorter name or a single-letter option would be better, but there probably aren't many left :-) – Steve Cohen Oct 12 '13 at 17:30
6

Surprisingly, my tar (GNU tar v1.16) doesn't have an option to strip initial pathname elements.

However, it seems that more recent versions sport a --strip-components=number parameter, which will strip that many compononents from the start of the path.

If like me you are using an older tar, and you are certain that the archive does not contain a directory or file named wordpress/wordpress, you could always just make a symlink from wordpress to ., then extract as usual:

ln -s . wordpress
tar xvfz latest.tar.gz wordpress
rm wordpress
j_random_hacker
  • 50,331
  • 10
  • 105
  • 169
  • 1
    The box I'm on doesn't have that "strip-components" param, so the symlink idea is an interesting one. I can't help but to wonder if there is something we are missing though. Unix is like that... there is usually some non-obvious way that you can only learn by asking. Hence my question! – Cory R. King May 10 '09 at 16:53
  • @Cory: Well I think the answer to that is "The tar developers realised, and introduced the --strip-components option." :) – j_random_hacker May 10 '09 at 17:00