List Contents of Tar Gz File: 5 Easy Ways, Avoid Errors
To list contents of tar gz file without extracting it, run tar -tvf archive.tar.gz. The old version of this post gave that command, a -j variant for bzip2 and a grep for one file, with output copied from the links web browser source. This rewrite runs the commands on GNU tar 1.35 in a Debian 13 container with a small test archive (a folder links-2.12 with five files, a sub folder and a symbolic link) in four formats, so every output below is from this test.
Contents
- List contents of tar gz file: the basic command
- List contents of tar gz file or a bz2, xz and zst archive
- Find one file when you list contents of tar gz file
- Filter the list of a tar gz file before you extract
- List contents of tar gz file from a pipe, and check a damaged archive
- Common mistakes when you list contents of tar gz file
- A short checklist to list contents of tar gz file safely
- FAQ
List contents of tar gz file: the basic command
The options mean: t list the contents, v verbose, f the archive file name. The verbose listing printed permissions, owner and group, size, date and name for each member:
tar -tvf links-2.12.tar.gz
drwxr-xr-x root/root 0 2026-11-01 06:28 links-2.12/
-rwxr-xr-x root/root 19 2026-11-01 06:28 links-2.12/rebuild
lrwxrwxrwx root/root 0 2026-11-01 06:28 links-2.12/README -> INSTALL
(Three of the nine lines are shown.)
A symbolic link is shown with its target. Without v, tar -tf printed nine names, one per line, which is easier to process; tar -tf links-2.12.tar.gz | wc -l printed 9. The letter order is flexible: tar tzf without a dash and tar -ztvf also worked. Listing does not extract anything: after tar -tf on a machine where the folder had been removed, ls still reported that links-2.12 does not exist. The numeric form --numeric-owner replaced root/root with 0/0.
List contents of tar gz file or a bz2, xz and zst archive
The old post used tar -jtvf for .tar.bz2. It works, and so does the same command without -j: GNU tar 1.35 detected the compression by itself, for bz2 and also for .tar.xz and .tar.zst (the zstd tool was installed for the test).
Writing the wrong letter is an error, not a guess: tar -jtvf links-2.12.tar.gz printed bzip2: (stdin) is not a bzip2 file. followed by tar: Child returned status 2. If you do not know the format, run file archive first: it answered gzip compressed data and bzip2 compressed data, block size = 900k. For a plain list of contents, tar -tf without any compression letter is the easiest habit.

Find one file when you list contents of tar gz file
The old post filtered the list with tar -tvf archive | grep links_16x16_1.xpm, which printed the matching line with its details. You can also give the member name to tar, but it must be the full path in the archive: tar -tvf links-2.12.tar.gz links-2.12/sub/links_16x16_1.xpm printed the line, while the short name links_16x16_1.xpm failed with tar: links_16x16_1.xpm: Not found in archive and exit code 2. For patterns, add --wildcards: tar -tf links-2.12.tar.gz --wildcards "*.xpm" printed the two xpm files. The pipe to grep stays the simplest choice when you remember only part of the name. The options are described in the GNU tar manual.
Filter the list of a tar gz file before you extract
Because the listing is plain text, ordinary filters work on it. A second sample archive, pkg.tar.gz, held files of 1000, 2000 and 3000 bytes in a folder with a sub folder. Sorting the verbose list on the size column, tar -tvf pkg.tar.gz | sort -k3 -rn | head -3, put the 3000-byte file first. Counting the entries that end with a slash, tar -tf pkg.tar.gz | grep -c "/$", gave 2 directories.
The most useful check is the top level: tar -tf archive | cut -d/ -f1 | sort -u printed one name, pkg, for a well-made archive, and two names, b and c, for an archive that was created without a top folder. Such an archive would spill its files into the current directory when you extract it, so look at this list before you run the extraction.
List contents of tar gz file from a pipe, and check a damaged archive
tar reads standard input when the file name is a dash: cat links-2.12.tar.gz | tar -tzf - printed the first member. This is useful with ssh host cat file | tar -tzf -, which was not tested here.
A cut-off file shows how errors look: a copy of the first 200 bytes printed gzip: stdin: unexpected end of file after a few names, then tar: Child returned status 1 and tar: Error is not recoverable: exiting now, with exit code 2. A listing that ends with such lines means that the download or copy is incomplete. You can use that in a check before an extract. To process the list further in a script, see the wc command post for counting lines.
Common mistakes when you list contents of tar gz file
- Using
-jor-zfor the wrong format; omit the letter and let tar detect it. - Giving only the file name instead of the full path of the member.
- Assuming that the listing is safe for a corrupt archive: read the error lines.
- Using the listing as a check of the content; it shows names and sizes, not that the files are intact.
A short checklist to list contents of tar gz file safely
- Run
file archiveif the name or the source is unclear. - List the names with
tar -tfand look at the top level withcut -d/ -f1 | sort -u. - Read the verbose list with
tar -tvffor sizes, owners and links; check that nothing points outside the folder you expect. - Watch the last lines for gzip or tar errors, which mean that the archive is cut off.
- Only then extract, into an empty folder given with
-C.
The steps cost a few seconds, and in the tests they exposed the missing top folder and the truncated file. Use them every time you list contents of tar gz file that you downloaded.
FAQ
Can I list contents of tar gz file with less? Yes, pipe the listing, as in tar -tvf file | less; only the plain pipe form was tested.
Does the same command to list contents of tar gz file work for .tgz files? The extension does not matter to GNU tar, which reads the compression from the data. A .tgz file was not part of the test.
Comments
Post a Comment