Server Fault is a question and answer site for system and network administrators. 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

Does anyone have a command syntax for extracting 1 file from a .tar.gz that also allows me to place the extracted file in a certain directory? I have Googled this and get too many variations with a lot of forum threads stating the syntax doesn't work. Before I venture on I prefer to know the command will work because I do not want to risk overwriting files and directories already present on my server.

share|improve this question
up vote 6 down vote accepted

If you're risking overwriting files in production with uncertain results, you're doing something wrong. Test first.

tar zxvf archive.tar.gz path/to/file/in/archive -C /destination/dir

share|improve this answer
+1 if this doesn't work it's not a tar.gz file. – egorgry Apr 8 '10 at 13:40
If What egorgry says turns out to be true (it is not a gzip file), see what file foo.tar.gz thinks it is... it could be a mislabed bz2 file or something, in which case you would just replace the z argument to tar with j. – Kyle Brandt Apr 8 '10 at 13:42
Thanks Warner -- it worked. Although the -C /destination/dir part did not work I got what I needed. The file ended up in a series of directories off the directory where the .tar.gz was located so I did not overwrite anything. (BTW, going in I knew and verified I was working with a .tar.gz. That was not an issue. I was simply looking for trustworthy syntax and I always trust what I get from stackoverflow and serverfault experts!) – H. Ferrence Apr 8 '10 at 13:55

On Solaris 10 if you have a tar ball called myTarfile.tar and you want to extract only one directory called my_dir from that tar file then below command workes

tar -xvf myTarfile.tar -C my_dir

share|improve this answer

Your Answer


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.