3

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
  • 359
  • 3
  • 7
  • 21

2 Answers2

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.

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.