| 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:
tar -tf archive.tar.gzOnce you have the stored member path, extract only that entry:
tar -xf archive.tar.gz path/to/file.txtThe 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:
tar -tf demo-archive.tar.gzdemo-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.txtIf 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:
tar -tvf demo-archive.tar.gzdrwxr-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.txtThe 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:
tar -xf demo-archive.tar.gz demo-archive/file2.txtAdd -v when you want tar to print each member as it is extracted:
tar -xvf demo-archive.tar.gz demo-archive/file2.txtdemo-archive/file2.txtTar 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:
tar -xvf demo-archive.tar.gz demo-archive/file2.txt demo-archive/reports/report3.txtdemo-archive/file2.txt
demo-archive/reports/report3.txtYou 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:
tar -xvf demo-archive.tar.gz demo-archive/reports/demo-archive/reports/
demo-archive/reports/report1.txt
demo-archive/reports/report2.txt
demo-archive/reports/report3.txtSelecting 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:
tar -xvf demo-archive.tar.gz --wildcards 'demo-archive/reports/report*.txt'demo-archive/reports/report1.txt
demo-archive/reports/report2.txt
demo-archive/reports/report3.txtA leading */ pattern matches the same files when the top-level folder
name varies:
tar -xvf demo-archive.tar.gz --wildcards '*/reports/*.txt'demo-archive/reports/report1.txt
demo-archive/reports/report2.txt
demo-archive/reports/report3.txtDo 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.
6. Extract a symbolic link
Archives can store symlinks separately from their targets. Extract only the link member:
tar -xvf demo-archive.tar.gz demo-archive/link_to_file1.txtdemo-archive/link_to_file1.txtTar 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:
ls -l demo-archive/lrwxrwxrwx. 1 root root 9 Aug 15 17:36 link_to_file1.txt -> file1.txtWhen you need the link to resolve immediately, extract the target file as well:
tar -xvf demo-archive.tar.gz demo-archive/file1.txt demo-archive/link_to_file1.txtdemo-archive/file1.txt
demo-archive/link_to_file1.txtConfirm both the file and the link resolve in the restored tree:
ls -l demo-archive/-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.txtTar 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:
mkdir -p /tmp/extractedThen extract one member into that directory:
tar -xvf demo-archive.tar.gz -C /tmp/extracted demo-archive/reports/report1.txtdemo-archive/reports/report1.txtTar 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:
tar -xvf demo-archive.tar.gz --strip-components=2 demo-archive/reports/report1.txtdemo-archive/reports/report1.txtThe 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:
mkdir -p /tmp/extractedRun extract with both -C and --strip-components=2 so only the basename lands under /tmp/extracted:
tar -xvf demo-archive.tar.gz -C /tmp/extracted --strip-components=2 demo-archive/reports/report1.txtdemo-archive/reports/report1.txtThe 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:
tar -xOf demo-archive.tar.gz demo-archive/file1.txtcontent1That is useful for quick inspection of configs, logs, manifests, or small scripts. Redirect binary members to a file instead of the terminal:
tar -xOf archive.tar.gz path/file.bin > file.bin10. Fix Not found in archive
When the member name does not match exactly, GNU tar exits with:
tar: report1.txt: Not found in archive
tar: Exiting with failure status due to previous errorsList members and search for the basename you expected:
tar -tf demo-archive.tar.gz | grep 'report1.txt'demo-archive/reports/report1.txtExtract using that full path:
tar -xf demo-archive.tar.gz demo-archive/reports/report1.txtSome 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:
tar -xvf demo-archive.tar.gz --wildcards --no-anchored '*report1.txt'demo-archive/reports/report1.txtExact 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
- GNU tar manual — Extracting Specific Files — exact member-name extraction and
-O - GNU tar manual — wildcard patterns
- GNU tar manual — Modifying File and Member Names —
--strip-componentsand transformed extraction paths

