Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract a single file from tar to a different directory? [closed]

Tags:

unix

tar

I know that I can use following command to extract a single file to the current working directory (assume I have a tar file named test.tar and a file named testfile1 and testfile2 are inside it):

$tar xvf test.tar testfile1 

And I can use -C option to extract files to another directory:

$tar xvf test.tar -C anotherDirectory/ 

When I incorporate the above two techniques together, I suppose that I can extract a single file to another directory.

$ tar xvf test.tar testfile1 -C anotherDirectory/ 

But the result is I can only extract the testfile1 to the current working directory, rather than the anotherDirectory.

I want to know how can I extract a single file from tar to a different directory?

like image 633
MengT Avatar asked Feb 12 '12 14:02

MengT


People also ask

Which command will extract files tar to the current directory?

The most common uses of the tar command are to create and extract a tar archive. To extract an archive, use the tar -xf command followed by the archive name, and to create a new one use tar -czf followed by the archive name and the files and directories you want to add to the archive.


1 Answers

The problem is that your arguments are in incorrect order. The single file argument must be last.

E.g.

$ tar xvf test.tar -C anotherDirectory/ testfile1 

should do the trick.

PS: You should have asked this question on superuser instead of SO

like image 50
Kimvais Avatar answered Sep 23 '22 18:09

Kimvais