How to extract a single folder and its subfolders from a "tarball" (.tar.gz)
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.
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?
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