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

17,924

Solution 1

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?

Solution 2

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:
17,924

Related videos on Youtube

mikey1980
Author by

mikey1980

Updated on September 17, 2022

Comments

  • mikey1980
    mikey1980 almost 2 years

    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.

    • matthias krull
      matthias krull about 14 years
      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
  • mikey1980
    mikey1980 about 14 years
    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
  • coneslayer
    coneslayer about 14 years
    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.
  • matthias krull
    matthias krull about 14 years
    yep, as simple as that.
  • msw
    msw about 14 years
    @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.
  • mikey1980
    mikey1980 about 14 years
    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
  • coneslayer
    coneslayer about 14 years
    @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.
  • mikey1980
    mikey1980 about 14 years
    @coneslayer it's extracting inside tmp (I think?!?)--now to wait, fingers crossed... can't thank you enough!