tar extract FAQ: How do I extract one file (or multiple files) from a tar archive without extracting the entire archive (i.e., how do I un-tar files from a tar archive)?
First, if you really want to extract a tar archive completely (un-tar a tar archive), I've written about that before in my How to extract a tar archive tutorial and my Linux tar command examples, so I won't repeat those tutorials much, other than to say that this command is common to un-tar an uncompressed tar archive:
tar xvf your-archive.tar
and this command is common to un-tar a compressed tar archive:
tar xzvf your-archive.tgz
Extract (un-tar) one file from a tar archive
Now, to answer the question, if you want to un-tar one file named "my-desired-file" from a tar archive named "my-archive.tgz", and assuming the archive is compressed as most are these days, you'd use a command like this:
tar xzvf my-archive.tgz my-desired-file
The secret here is that you need to specify your filename just as it is in the tar archive. For instance, if your file is named "foo" and it's in the root directory of the archive, you'd use this command:
tar xzvf my-archive.tgz foo
However, if the file was in a sub-directory named bar, you'd want to specify your un-tar command like this:
tar xzvf my-archive.tgz bar/foo
Beware absolute paths
One thing to be careful about here is to see if files are in the archive with an absolute path. By "absolute path", I mean any file whose path begins with the root directory "/". For instance, if a file in the tar archive has this path:
and I then extract that file from the tar archive, it will clobber the current .bash_profile file in my home directory. Unless you're making backups on your own Linux system, absolute paths are usually a no-no, and even if you are making your own backups, they're usually a no-no, as they don't give you much flexibility during the restore (un-tar, extract) operation.
Extract (un-tar) a directory from a tar archive
One more note before I go: I just wanted to extract one directory from a much larger tar archive, and to extract that directory from my tgz file I used this tar command:
tar xzvf drupalsite.tgz ./sites/all/themes/marinelli
In this case my tar archive was named drupalsite.tgz, and I wanted to extract the "marinelli" subdirectory of that archive, which, if you're familiar with Drupal, is found in the "sites/all/themes" folder.
Extracting this tar directory like this actually extracts the marinelli folder under the sites/all/themes directory on my computer, so I end up with a
folder, filled with all of its subdirectories, but no other subdirectories under the top level "sites" directory are extracted.
How to extract (un-tar) a file from a tar archive
I hope this short tutorial on how to extract (un-tar) a file from a tar archive has been helpful. For more tar command examples, see my Linux tar command examples and tutorial.