Wednesday, January 26, 2011

TAR command to extract a single file from a .tar.gz

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 Google'd 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.

Thanks.

  • 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

    egorgry : +1 if this doesn't work it's not a tar.gz file.
    Kyle Brandt : 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.
    Dr. DOT : 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!)
    From Warner

0 comments:

Post a Comment