We help IT Professionals succeed at work.

URGENT! extract specific directory from gunzip | tar file

Julian Parker
on
All,

I need to extract a directory from a tar.gz file in solaris 9 gtar not available.

I've used gunzip < file.tar.gz | tar xf - ./directory/* but it doesnt work. I'm more used to using tar with the -z option

I need the syntax urgently.
Comment
Watch Question

Most Valuable Expert 2013
Top Expert 2013
Commented:
Hi,
how about
gunzip -c file.tar.gz | tar -xf - ./directory/*
"-c" instructs gunzip to write to stdout!
wmp

Commented:
gunzip -c /path/to/folder/filename.tar.gz | tar -xvf - path/within/archive/

-c writes the output produced (either the compressed or decompressed file) to standard output and leaves the input files unchanged. When multiple input files are specified, each file is compressed or decompressed independently before being written to the standard output. For better compression, you should concatenate all input file before compressing or decompressing them.

If tar has build-in support for gunzip you can just use:

tar -xzvf MyArchive Source_file
or
tar --extract --gunzip --verbose --file=MyArchive Source_file
Julian ParkerSenior Systems Administrator

Author

Commented:
Cheers wmp, it seemed to be a bit temperamental, the command I used worked sometimes and not others... I eventually did; gunzip < file.tar.gz | tar xf - ./directory
Most Valuable Expert 2013
Top Expert 2013

Commented:
Hi,
glad you found it!
As for the "sometimes not working ..." thing - I obviously didn't think twice about the asterisk in the directory parameter!
This will work only if the shell is able to expand it properly (i.e. to directories/files which exist locally as well as in the archive) .
So if your local ./directory is empty or non-existent, nothing will be extracted - the shell will leave the asterisk as is - and such a file /directory is not present in the archive!
Thx for the points anyway!
wmp