A "tar extract multiple" tip - How to extract multiple files from a tar archive

tar command FAQ: Can you demonstrate how to extract (un-tar) multiple files from a tar archive, without extracting all files from the archive?

Sure, here are a couple of examples of how to extract multiple files from a tar archive (un-tar them), without extracting all the files in the archive.

First, if you just need to extract a couple of files from a tar archive, you can usually extract them like this, listing the filenames after the tar archive:

$ tar xzvf archive.tgz  Foo.java  Bar.java

(Note: I just added a few extra spaces to make that example easier to read; they aren't required.)

That example will extract the two files Foo.java and Bar.java from the archive, assuming (a) they're in the archive and (b) they're not in subdirectories of the archive. (I've written more about this syntax in my How to un-tar a file from a tar archive tutorial.)

Extract multiple tar files in subdirectories

If the files you want to extract are in a subdirectory of the archive named Baz, you can extract them like this:

$ tar xzvf big-archive.tgz Baz/Foo.java Baz/Bar.java

The key there is just to remember to include the subdirectory name as part of the filename.

Extract a lot of files from a tar archive

Finally, if you need to extract a lot of files from a tar archive, you can put all those filenames into a file, then use the tar command "--files-from" option to extract just the files listed in that file. That tar syntax looks like this:

$ tar xzvf big-archive.tgz --files-from files-to-extract.txt

I just did something like this because I wanted to extract dozens of files from a tar archive that contained hundreds of files. First I got a listing of the files in the tar archive, like this:

$ tar tzvf big-archive.tgz > files-to-extract.txt

Next, I edited this file, deleting all the files I didn't want, and keeping all the ones I did want:

$ vi files-to-extract.txt

Then I ran the tar extract command shown above to extract just the files I wanted from the tar archive:

$ tar xzvf big-archive.tgz --files-from files-to-extract.txt

If you ever need to extra a few files from a tar archive, or a lot of files from a tar archive, I hope this tar command tip is helpful.

Related tar command tutorials

I hope this tutorial on how to extract multiple files from a tar archive has been helpful. (Some people refer to this "tar extract" process as an un-tar process.) For more information on the Linux tar command, here are links to some related tar tutorials:

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.