Extract 1 file from tar.gz with bash Extract 1 file from tar.gz with bash bash bash

Extract 1 file from tar.gz with bash


From the man page, to extract blah.txt from foo.tar.gz:

tar -xzf foo.tar.gz blah.txt

(And this goes on superuser, of course, but hey, prompt answers are nice too.)


I echo Jefromi's answer, with the addition of including the path to the file if you have directories in the tar file (this may seem obvious to some, but it wasn't initially clear to me how to specify the directory structure).

For example, if you did the tar at the src/ directory, and blah.txt was under release1/shared/, you would go back to the src/ directory (if you want it untarred at the same place)

tar -xzf tar.gz release1/shared/blah.txt

If you don't remember the directory structure of your tar file (I'm a little disorganized and sometimes forget where I did the tar), you can always

tar -tzf tar.gz

to see the contents, canceling out (Ctrl+C) once you get an idea of your directory structure.