When working in the Linux terminal, downloading and managing compressed archives is a daily occurrence. The most common format you will encounter is the .tar.gz file (often called a tarball). Before extracting an unknown tarball—which could potentially scatter hundreds of files across your current directory or overwrite existing data—it is always best practice to peek inside and see exactly what it contains.
You can easily view the entire directory structure and file list of a .tar.gz archive without actually extracting a single byte of data to your hard drive. This is done using the standard tar command with a specific combination of flags.
This guide explains how to list the contents of a tarball in the Ubuntu terminal safely.
The Standard Command
To view the contents of a .tar.gz file, you will use the tar command with the -t (list), -z (filter through gzip), and -f (file) flags.
- Open your Ubuntu terminal.
- Navigate to the directory containing your archive using the
cdcommand. - Type the following command, replacing
filename.tar.gzwith the actual name of your file:tar -tzf filename.tar.gz - Press Enter.
The terminal will instantly output a list of every file and folder contained within the archive. If all the files are contained neatly within a single parent folder (e.g., project-v1.0/), it is safe to extract normally.
Viewing Detailed File Information (Verbose Mode)
Sometimes just seeing the filenames isn’t enough. If you need to verify file sizes, permissions, or ownership data before extracting, you can add the -v (verbose) flag.
- Type the following command:
tar -tvzf filename.tar.gz - Press Enter.
The output will now look very similar to running an ls -l command. You will see the read/write permissions, the owner/group, the exact file size in bytes, the modification date, and finally the file path.
Managing Large Archives
If the .tar.gz file contains thousands of files, the output will rapidly scroll past your screen, making it impossible to read. To solve this, you can pipe the output into a pager tool like less.
- Type the command and append
| lessto the end:tar -tzf filename.tar.gz | less - Press Enter.
- You can now use the Up and Down arrow keys to scroll through the list at your own pace. Press the Q key to exit the viewer when you are finished.
Alternatively, if you are looking for a specific file (like a configuration file or a readme), you can pipe the output into grep to search the archive.
tar -tzf filename.tar.gz | grep "readme.txt"