How to extract a single folder and its subfolders from a "tarball" (.tar.gz)

4

1

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.

mikey1980

Posted 2010-06-23T16:18:51.367

Reputation: 173

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 – 2010-06-23T16:49:16.473

Answers

6

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?

coneslayer

Posted 2010-06-23T16:18:51.367

Reputation: 7 494

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 – 2010-06-23T16:39:49.333

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 – 2010-06-23T16:44:18.170

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 – 2010-06-23T16:47:37.663

@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 – 2010-06-23T16:48:54.850

@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 – 2010-06-23T16:52:44.217

it's not local, our other programmer set this dedicated server up and it get's backed up daily using a .sh CRON Job to this archive – mikey1980 – 2010-06-23T16:52:44.407

@coneslayer it's extracting inside tmp (I think?!?)--now to wait, fingers crossed... can't thank you enough! – mikey1980 – 2010-06-23T16:57:52.630

3

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

Daenyth

Posted 2010-06-23T16:18:51.367

Reputation: 5 742

yep, as simple as that. – matthias krull – 2010-06-23T16:46:56.697