Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to extract a single folder and its subfolders from a tarball (.tar.gz) on a CentOS server. I haven't the slightest clue how to do it using an SSH terminal.

I tried:

gunzip -c files_20100623.0110.tar.gz | tar -xvf home/bsisplas/public_html/staging/template/*.*

However it can't seem to find the file in the archive. Also, where in the gunzip syntax do you specify where the extracted files should go? I'm a Linux terminal noob to say the least.

share|improve this question
    
use tar with the -z flag.. its way more clean than the piping. and you still would have to wait a long time since the archive will be completely unziped before extraction –  matthias krull Jun 23 '10 at 16:49

2 Answers 2

up vote 5 down vote accepted

First, when you use the -f option to tar, you need to give it an argument telling it the filename of the archive. Since you're feeding it from a pipe from gunzip, we use - to mean standard input:

gunzip -c files_20100623.0110.tar.gz | tar -xvf - home/bsisplas/public_html/staging/template/*.*

My next point would be that you only have to give the directory name. (Also: *.* is usually a DOS-ism. If you mean "all files" in Unix, just write *. If you write *.* you're saying "all files with a dot somewhere in their name" which could exclude important files without a dot, like Makefile or README):

gunzip -c files_20100623.0110.tar.gz | tar -xvf - home/bsisplas/public_html/staging/template

That should work. But you can make things a little easier by using tar's -z option, which tells it to do the gunzip itself. We use that, and replace the - input filename with the archive filename:

tar -xvzf files_20100623.0110.tar.gz home/bsisplas/public_html/staging/template

How does that work?

share|improve this answer
    
well I didn't get an error, just cursor just sat there until I hit ctrl-c... ohhh man I really don't want to download 6 GB just to get a few hundred K –  mikey1980 Jun 23 '10 at 16:39
    
Um, how long did you wait? If it's a 6 GB archive it might take a little while. You won't see anything until it gets to the files you want. –  coneslayer Jun 23 '10 at 16:44
    
if I use your bottom line, will it extract to the current folder or over the original files.. this is a production server so I need to make sure it doesn't replace the originals.. there is a folder called 'tmp' in the same dir as the archive... this where I hope to place the extracted files –  mikey1980 Jun 23 '10 at 16:47
    
@mikey: what are you downloading? you mention ssh, but it looks like your tarball is local. Unfortunately, given the structure of tar files, you have to scan through the whole thing until you find the file you want. This made a lot more sense in the days of magnetic Tape ARchives. –  msw Jun 23 '10 at 16:48
    
@mikey1980 From wherever you are working, it will put the files in home/bsisplas/..., creating those directories if necessary. If you want those directories to be created in tmp, the easiest thing would be to cd tmp before running the command, and change the archive filename to ../files_20100623.0110.tar.gz. @msw I think he's ssh'ing to a server where the 6 GB archive is stored locally, and probably wants to extract the files for subsequent transfer to another machine. That way he doesn't have to move the whole 6 GB archive to another machine. –  coneslayer Jun 23 '10 at 16:52

All you need is tar. You can get a list of files in the tarball with the -t option, and then extract as normal but with the file path as a final argument: tar zxvf foo.tar.gz filepath

share|improve this answer
    
yep, as simple as that. –  matthias krull Jun 23 '10 at 16:46

Your Answer

 
discard

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.