How to Extract a Single File from a .tar.gz Archive in Linux

When downloading software or server backups in Linux, they almost always arrive as massive .tar.gz archives. Usually, administrators extract the entire archive using tar -xzf archive.tar.gz. However, if the archive is 50GB in size and you only need a single tiny configuration file from inside it, extracting the entire thing wastes massive amounts of time and disk space. Thankfully, the tar command allows you to extract specific files.

Step 1: Find the Exact File Path

Before you can extract a single file, you need to know its exact path inside the archive. You cannot just guess the name; tar requires the full internal path.

To list the contents of the archive without extracting it, run:

tar -tzf backup.tar.gz

If the archive contains thousands of files, pipe the output into grep to search for the file you need. For example, to find an nginx configuration file:

tar -tzf backup.tar.gz | grep "nginx.conf"

This will output the exact internal path, such as etc/nginx/nginx.conf.

Step 2: Extract the Specific File

Once you have the exact path, use the standard extract command but append the path at the very end.

tar -xzf backup.tar.gz etc/nginx/nginx.conf

Important: Do not include a leading slash (/etc/) unless the tar -tzf command explicitly showed one. The paths must match perfectly.

Where Does the File Go?

When the command finishes, tar will recreate that exact folder structure in your current working directory. So in the example above, you will now see a new folder named etc/ in your current directory, containing an nginx/ folder, which holds your single extracted file. Your massive 50GB archive remains untouched, and you saved yourself an hour of processing time!

Related posts

  1. How to Install and Configure Apache Web Server on Ubuntu
  2. How to Check Disk Space on Ubuntu via the Command Line
  3. How to Compress and Extract Files using Tar on Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.