Linux Commands

How to View the Content of a Tar File?

Tar is a widely used utility to collect files and creating archives out of them. It was designed to create archives to store data on tapes, thus called “Tape ARchive.” The utility was first included in UNIX version 7 in 1979 and now available on multiple platforms.

Tar is a prominent Linux utility and comes with various functions such as creating archives, extracting them, extracting them to a specific directory, adding more files to the existing archive, etc. But can I view the content of the Tar file specifically while using the terminal? The answer is Yes! This multi-feature utility also allows viewing the content of the archived files, especially when working on the server and no graphical tool is available. Viewing the tar file content can also be quite handy when the file is quite large, and you only want to find a specific and extract it.

We have already discussed many features of the Tar utility. This guide focuses on a lesser-known feature of Tar utility and displays or listing the content of the tar file. So, let’s begin:

How to view the content of a tar file
You need to view the content of a tar file as it collects many files and ensures if a specific file is present. The syntax of the command to view the content of a tar file without extracting it is mentioned below:

$tar -tf [file_name.tar]

Alternatively:

$tar --list --file=[file_name.tar]

Where:

  • “-t”/ “–list”: Used to list the content of the tar file
  • “-f”/ “–file”: Commanding the utility to use the file mentioned in the following argument

Moreover, you can use the “-v” flag or “–verbose” option to get detailed standard output. Let’s understand it by an example:

$tar -tf my_doc_file.tar

With verbose flag:

$tar -tvf my_doc_file.tar

The long command would be:

$tar --list --file=my_doc_file.tar

Or:

$tar --list --verbose --file=my_doc_file.tar

In the above command, I am viewing the content of a “my_doc_file.tar” file.

How to view the content of tar.gz or tgz files
As it is known that a tar file does not compress the files, it simply collects. To compress it, we need another utility. A commonly used utility to compress tar files is “gzip.” The command to view the content of a “tar.gz” or “tgz” file is mentioned below:

$tar -ztvf my_doc_file.tar.gz

The long command would be:

$tar --gzip --list --verbose --file=my_doc_file.tar.gz

Where “-z” and “–gzip” options are used to handle the “tgz” or “tar.gz” files.

How to view the content of tar.bz, tar.bz2,tbz, or tbz2 files
The “tzip” is another utility for the compression of tar files. To view the content of files with extension “tar.bz”, “tar.bz2”, “tbz” or “tbz2”, follow the below-mentioned command:

$tar -jtvf my_doc_file.tar.bz2

And the long command would be:

$tar --bzip2 --list --verbose --file=my_doc_file.tar.bz2

The “-j” flag is used to handle “bzip” files.

How to view the content of tar.xz files
The “tar.xz” files are the tar files that are compressed using the “LZMA/LZMA2” algorithm. To view the content of files with “tar.xz” files, use:

$tar -Jtvf my_doc_file.tar.xz

Or

$tar --xz --list --verbose --file=my_doc_file.tar.xz

The “-J” and “–xz” options are used to handle “tar.xz” files.

Conclusion

Tar is a feature-rich Linux utility that allows viewing the content of various tar archive files without extracting it. Regarding the content of a tar file in the terminal can be helpful in many situations. In this guide, we explore how to view the content of tar files and compressed tar files in the terminal using different flags and long commands. To get more about Tar utility, run “man tar” in the terminal.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.