How to Extract Specific Files and Directories from tar.gz

Deepak Prasad
Tested on Red Hat Enterprise Linux 10.2 (lab VM)
Package tar (GNU tar) 1.35
Applies to RHEL, Rocky Linux, AlmaLinux, CentOS Stream, Fedora, Debian, Ubuntu, and other GNU/Linux systems with GNU tar
Privilege Normal user (read archives you can open; write where you have permission)
Scope List archive members, extract one file, multiple files, a directory, wildcard matches, or a symlink; extract to another path with -C; strip parent directories; read one member to stdout; fix Not found in archive errors
Related guides tar command
ungzip and untar tar.gz
tar --strip-components
Create a symbolic link
grep command

You do not need to extract an entire .tar.gz archive when you need only one file or directory. List the archive first, then pass the exact member name to tar -xf:

bash
tar -tf archive.tar.gz

Once you have the stored member path, extract only that entry:

bash
tar -xf archive.tar.gz path/to/file.txt

The member name must match the path stored inside the archive, not merely the file's basename. For broader tar options and archive basics, see the tar command cheat sheet.


Quick reference: extract specific content from tar.gz

Task Command
List archive members tar -tf archive.tar.gz
Extract one file tar -xf archive.tar.gz path/file.txt
Extract multiple files tar -xf archive.tar.gz path/a.txt path/b.txt
Extract one directory tar -xf archive.tar.gz path/directory/
Extract using wildcard tar -xf archive.tar.gz --wildcards '*/report*.txt'
Extract into another directory tar -xf archive.tar.gz -C /destination path/file.txt
Remove parent directories tar -xf archive.tar.gz --strip-components=N path/file.txt
Print file to stdout tar -xOf archive.tar.gz path/file.txt
Verbose extraction tar -xvf archive.tar.gz path/file.txt

GNU tar often detects gzip compression when you read a .tar.gz file, so -z is optional on modern systems. This article uses tar -xf and tar -tf consistently; adding -z is equally valid when you want to state the compression format explicitly.


1. List files before extracting

tar works with member names stored in the archive index, not filenames on disk. Start with a plain list:

bash
tar -tf demo-archive.tar.gz
output
demo-archive/
demo-archive/reports/
demo-archive/reports/report1.txt
demo-archive/reports/report2.txt
demo-archive/reports/report3.txt
demo-archive/file1.txt
demo-archive/file2.txt
demo-archive/link_to_file1.txt

If the listing shows demo-archive/reports/report1.txt, extracting report1.txt alone fails because that basename is not a member name. Use the full stored path instead.

Use verbose listing only when you need permissions, sizes, or symlink targets:

bash
tar -tvf demo-archive.tar.gz
output
drwxr-xr-x root/root         0 2026-08-15 17:36 demo-archive/
drwxr-xr-x root/root         0 2026-08-15 17:36 demo-archive/reports/
-rw-r--r-- root/root         8 2026-08-15 17:36 demo-archive/reports/report1.txt
-rw-r--r-- root/root         8 2026-08-15 17:36 demo-archive/reports/report2.txt
-rw-r--r-- root/root         8 2026-08-15 17:36 demo-archive/reports/report3.txt
-rw-r--r-- root/root         9 2026-08-15 17:36 demo-archive/file1.txt
-rw-r--r-- root/root         9 2026-08-15 17:36 demo-archive/file2.txt
lrwxrwxrwx root/root         0 2026-08-15 17:36 demo-archive/link_to_file1.txt -> file1.txt

The symlink line shows the stored target (file1.txt). For finding names in a long list, pipe to grep — but use tar -tf output for extraction paths, not parsed tar -tvf columns.


2. Extract one specific file

Pass one member name after the archive filename:

bash
tar -xf demo-archive.tar.gz demo-archive/file2.txt

Add -v when you want tar to print each member as it is extracted:

bash
tar -xvf demo-archive.tar.gz demo-archive/file2.txt
output
demo-archive/file2.txt

Tar recreates the member's stored directory structure. Extracting demo-archive/file2.txt normally creates ./demo-archive/file2.txt in the current directory, not ./file2.txt. To drop parent directories, use --strip-components in section 8.


3. Extract multiple specific files

List every member you need in one command. Tar extracts only those entries and any parent directories they require:

bash
tar -xvf demo-archive.tar.gz demo-archive/file2.txt demo-archive/reports/report3.txt
output
demo-archive/file2.txt
demo-archive/reports/report3.txt

You do not need separate tar runs for each file when the member names are already known.


4. Extract a specific directory and its contents

Pass a directory member to pull that tree from the archive:

bash
tar -xvf demo-archive.tar.gz demo-archive/reports/
output
demo-archive/reports/
demo-archive/reports/report1.txt
demo-archive/reports/report2.txt
demo-archive/reports/report3.txt

Selecting a directory member extracts that directory and the archive members stored beneath it.


5. Extract files with wildcards

GNU tar matches member names with --wildcards. Quote the pattern so the shell does not expand it before tar sees it:

bash
tar -xvf demo-archive.tar.gz --wildcards 'demo-archive/reports/report*.txt'
output
demo-archive/reports/report1.txt
demo-archive/reports/report2.txt
demo-archive/reports/report3.txt

A leading */ pattern matches the same files when the top-level folder name varies:

bash
tar -xvf demo-archive.tar.gz --wildcards '*/reports/*.txt'
output
demo-archive/reports/report1.txt
demo-archive/reports/report2.txt
demo-archive/reports/report3.txt

Do not pipe tar -tvf through grep, awk, and xargs for normal wildcard extraction. GNU tar's --wildcards is simpler and avoids breaking on spaces in member names.


Archives can store symlinks separately from their targets. Extract only the link member:

bash
tar -xvf demo-archive.tar.gz demo-archive/link_to_file1.txt
output
demo-archive/link_to_file1.txt

Tar recreates the symlink with the target path stored in the archive (file1.txt in this demo). If that target file is not present at the expected location, the link is dangling until you restore the target.

Check what tar extracted:

bash
ls -l demo-archive/
output
lrwxrwxrwx. 1 root root 9 Aug 15 17:36 link_to_file1.txt -> file1.txt

When you need the link to resolve immediately, extract the target file as well:

bash
tar -xvf demo-archive.tar.gz demo-archive/file1.txt demo-archive/link_to_file1.txt
output
demo-archive/file1.txt
demo-archive/link_to_file1.txt

Confirm both the file and the link resolve in the restored tree:

bash
ls -l demo-archive/
output
-rw-r--r--. 1 root root 9 Aug 15 17:36 file1.txt
lrwxrwxrwx. 1 root root 9 Aug 15 17:36 link_to_file1.txt -> file1.txt

Tar does not require extracting the target, but a symlink without its target remains broken until the target exists. See create a symbolic link for how links are created on disk.


7. Extract a file into another directory

-C changes where extracted files land. Create the destination first:

bash
mkdir -p /tmp/extracted

Then extract one member into that directory:

bash
tar -xvf demo-archive.tar.gz -C /tmp/extracted demo-archive/reports/report1.txt
output
demo-archive/reports/report1.txt

Tar still preserves the stored path under the destination, so the file appears at /tmp/extracted/demo-archive/reports/report1.txt. To place only report1.txt directly under /tmp/extracted, combine -C with --strip-components in the next section.


8. Extract without parent directories

--strip-components removes leading path components from each member before writing files. For demo-archive/reports/report1.txt:

  • component 1 = demo-archive
  • component 2 = reports

Strip two components to leave only report1.txt:

bash
tar -xvf demo-archive.tar.gz --strip-components=2 demo-archive/reports/report1.txt
output
demo-archive/reports/report1.txt

The verbose line still shows the member name inside the archive; on disk you get report1.txt in the current directory.

Combine with -C to drop parents and choose the output folder:

bash
mkdir -p /tmp/extracted

Run extract with both -C and --strip-components=2 so only the basename lands under /tmp/extracted:

bash
tar -xvf demo-archive.tar.gz -C /tmp/extracted --strip-components=2 demo-archive/reports/report1.txt
output
demo-archive/reports/report1.txt

The file lands at /tmp/extracted/report1.txt. For more strip examples and zip equivalents, see tar --strip-components.


9. Read a file without extracting it

-O (long form --to-stdout) sends one member to standard output without creating a file:

bash
tar -xOf demo-archive.tar.gz demo-archive/file1.txt
output
content1

That is useful for quick inspection of configs, logs, manifests, or small scripts. Redirect binary members to a file instead of the terminal:

bash
tar -xOf archive.tar.gz path/file.bin > file.bin

10. Fix Not found in archive

When the member name does not match exactly, GNU tar exits with:

output
tar: report1.txt: Not found in archive
tar: Exiting with failure status due to previous errors

List members and search for the basename you expected:

bash
tar -tf demo-archive.tar.gz | grep 'report1.txt'
output
demo-archive/reports/report1.txt

Extract using that full path:

bash
tar -xf demo-archive.tar.gz demo-archive/reports/report1.txt

Some archives store members with a leading ./ (for example ./reports/report1.txt). Use the name exactly as tar -tf prints it, or match with an appropriate --wildcards pattern. When you do not know the full path, --no-anchored lets the pattern match anywhere in the member name:

bash
tar -xvf demo-archive.tar.gz --wildcards --no-anchored '*report1.txt'
output
demo-archive/reports/report1.txt

Exact member names remain the safest default; pattern flags are for cases where listing first is impractical.


Summary

Selective extraction from .tar.gz follows one workflow: list members with tar -tf, identify the stored path, then pass that path to tar -xf. One command can pull a single file, several named files, an entire directory tree, or wildcard matches when you quote --wildcards patterns for GNU tar.

The usual mistake is passing a basename or a partial path. Tar recreates the directory layout stored in the archive, so a member under demo-archive/reports/ lands under that tree unless you use -C and --strip-components. Use -O to read text members without writing them to disk, and extract symlinks alone when you only need the link entry — they resolve only after their stored target exists on disk.

For full-archive workflows, see ungzip and untar tar.gz. For day-to-day flags and examples, keep the tar command cheat sheet nearby.


References


Frequently Asked Questions

1. How do I extract one file from a tar.gz without unpacking everything?

List members with tar -tf archive.tar.gz, copy the exact stored path, then run tar -xf archive.tar.gz path/to/file.txt. The member name must match what tar lists, not only the basename.

2. Can GNU tar extract files with wildcards from tar.gz?

Yes. Use tar -xf archive.tar.gz --wildcards 'path/pattern*.txt' with the pattern quoted so the shell does not expand it first. Do not pipe tar -tvf through grep and xargs for normal wildcard extraction.

3. Why does tar say Not found in archive?

The path you passed does not exactly match a stored member name. Run tar -tf archive.tar.gz and grep for the basename, then pass the full member path tar printed. Some archives store paths with a leading ./ prefix.

4. How do I extract a tar.gz file to a specific directory?

Use -C before the member names: tar -xf archive.tar.gz -C /destination path/in/archive/file.txt. Tar still recreates the stored directory tree under that destination unless you add --strip-components.
Omer Cakmak

Linux Administrator

Highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware.

  • Debian
  • Ubuntu
  • Linux
  • Red Hat Enterprise Linux
  • Shell Script
  • System Administration