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.


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

  • +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
  • 1
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.