Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .