How to Extract a Single File from a .tar or .tar.gz Archive

In this post, we are going to learn how to use the tar command to extract a single or multiple files from a tar archive.

How to Extract a Single File from a .tar or .tar.gz Archive

Tar archives are one of the most popular file archiving format. It is used in packaging a large set of files as a single collection for distribution purposes. Files in a tar archive are stored in an uncompressed format along with the archive metadata. However, you can compress files in a tar archive using tools such as gzip, xz, bzip2, lzma, lzip, etc.

In some cases, you may come across an instance where you need to extract a single or a set of files from a large tar archive. Luckily, tar allows us to extract specific files from an archive without extracting everything.

Extract Single File

Suppose we have a tar archive with files as shown:

total 16660
drwxrwxr-x 2 debian debian     4096 Oct 30  2017 .
drwxr-xr-x 3 debian debian     4096 Sep 29 17:30 ..
-rw-rw-r-- 1 debian debian  1042592 Aug 16  2017 file_example_JPG_1MB.jpg
-rw-rw-r-- 1 debian debian   236789 Sep 25  2017 file_example_ODS_5000.ods
-rw-rw-r-- 1 debian debian  1042157 Aug 11  2017 file-example_PDF_1MB.pdf
-rw-rw-r-- 1 debian debian  2507971 Aug 16  2017 file_example_PNG_2500kB.jpg
-rw-rw-r-- 1 debian debian  1028608 Aug 10  2017 file_example_PPT_1MB.ppt
-rw-rw-r-- 1 debian debian 10151438 Oct 24  2017 file_example_TIFF_10MB.tiff
-rw-rw-r-- 1 debian debian  1027072 Aug  2  2017 file-sample_1MB.doc

To extract the file-example_PDF.1M.pdf, we can run the command:

tar --extract --file=sample.tar file-example_PDF_1MB.pdf

This command will extract the specified file name and save it in the current working directory.

If a file is nested inside a set of folders, you can specify the file path as shown in the command syntax below:

tar --extract --file=<file.tar> /path/to/file

You can also use the short version of the command by replacing the --extract and --file parameters with the -xf.


tar -xf file.tar /path/to/file

Extract File with a Specific Extension

To extract files ending with a specific extension, we can use a wildcard pattern as shown:

tar -xvf <file.tar> --wildcards '*.ext'

Where *.ext is the file extension you wish to extract. Example to extract pdf files, run the command:

tar -fvx <file.tar> --wildcards '*.pdf'

You can also use a pattern to etract files matching a specific pattern.

$ tar -xvf file.tar --wildcards --no-anchored 'log*'

The command above extracts files starting with log-*.


Thank you for tuning in to our tutorials. We hope you enjoyed this tutorial, if you did, leave us a comment and share with your friends.

Join us to get notified when new articles arrive.

Your's truly, the GeekBits admin.

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.