tar command - how to extract one file to specific directory

3

1

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

ring bearer

Posted 2010-10-15T17:23:48.807

Reputation: 329

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 – 2010-10-15T17:37:47.590

the version of tar that comes standard on AIX does not have the -z option. – ring bearer – 2010-10-15T18:56:24.730

Answers

1

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.

RedGrittyBrick

Posted 2010-10-15T17:23:48.807

Reputation: 70 632

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 'SO- stop being evil' – 2010-10-15T20:12:18.640

1

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.

Karl Bielefeldt

Posted 2010-10-15T17:23:48.807

Reputation: 1 050