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.

Is there a way to extract a single file from a tar file to a specific directory? In fact, I am dealing with a .tgz file so, I am attempting something like this :

gunzip -c mytargzfile.tgz | tar xvf - path/to/myfile -C /tmp

In order to extract a file entry called path/to/myfile in mytargzfile.tgz to /tmp directory.

But this command fails as tar complains saying it can not find file named -C and /tmp in the archive. I tried switching -C option before xvf and it did not help either.

Note that I am using AIX, and KSH

share|improve this question
    
can't you just use the -z file on tar and skip the gunzip step? -z tells tar to treat it as a compressed archive. So you'd have tar -xzvf mytargzfile.tgz -T path/to/myfile /tmp –  peelman Oct 15 '10 at 17:37
    
the version of tar that comes standard on AIX does not have the -z option. –  ring bearer Oct 15 '10 at 18:56
add comment

2 Answers

up vote 1 down vote accepted

I think The C option to tar should occur earlier in the command

tar -cvf - -C /tmp path/to.myfile

I imagine you could download, compile and install GNU tar into your ~/bin, which might make things a bit easier.

share|improve this answer
    
That, plus you need to use -c to use standard mode options. With c, the command line is parsed in BSD mode which doesn't support -C. –  Gilles Oct 15 '10 at 20:12
add comment

That command worked for me fine as you said you tried with the -C first: tar -C /tmp -xvf. Maybe it's a bug in your tar version?

A little clunky, but there's always:

cd /tmp
gunzip -c /path/to/mytargzfile.tgz | tar xvf - path/to/myfile

I recommend installing GNU tar for yourself. It's worth it just for the -j and -z options if nothing else, and is one of the first things I always do in a new Unix account.

share|improve this answer
add comment

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.