How to View the Content of a Tar File?

A tar file, short for tape archive file, is a type of archive file that is commonly used on Linux and Unix operating systems. The importance of tar files in linux is mainly to archive and compress multiple files, folders, and directories and to save space and transfer files more efficiently.

This article will explain the method to visualize the content of a tar file, along with various examples.

How to View the Content aTar File?

To view the contents of a tar file, the general syntax is provided below:

$ tar -tvf <File_Name>

The syntax description is given below:

  • The “File_Name” specifies the tar file which the user wants to visualize.
  • The option “t” stands for “contents list of an archive”.
  • The option “v” is for “verbose,” which will show more detailed information about the files in the archive.
  • The option “f” specifies the archive file name you want to view.

Example 1: View the Contents of a Tar File

To view the contents of a tar file, the “tar” command is utilized with the  “tvf” option, whose description is provided above. In our case, specify the file name as “file.tar”:

$ tar -tvf file.tar

The output shows that the content of “file.tar” has been seen in the terminal.

Example 2: Display the Contents of a Compressed Tar File

To view the contents of a compressed tar file, define the file name and the extension name. To do so, the “compress.tar.gz” is utilized with the “tzvf” option:

$ tar -tzvf compress.tar.gz

The output shows the content of “compress.tar.gz” along with the date and time.

Example 3: View Only the Files Names in the Archive

To view only the names of the files in the archive, use the “tar” command with the “tf” option as below:

$ tar -tf file.tar

The output displays only the name of files “test.sh” and “emp.txt”.

Example 4: Display the Long Format of the File

To view the contents of a tar file along with the long format, the “tar” command is utilized with the “l” option:

$ tar -l -tvf file.tar

The detailed information of “file.tar” including “file names”, “date and time”, “file location” “read and write permission” is displayed in the terminal.

Example 5: Display Content of Other Tar Formats

To display the content of other formats of the tar files such as “.tar.gz”, “.tar.bz2” files, the “tvf” option is utilized by specifying the file name. In our case, the “.tar.bz2” tar file is carried out to visualize the content:

$ tar -tvf file.tar.bz2

The output shows that the content of “file.tar.bz2” has been displayed in the terminal.

Conclusion

Linux offers the “tar” command with the “tvf” option to view the tar file contents without opening it. Using this command, you can visualize the “file names”, “date and time”, “file location,” “read and write permission,” and many more in the terminal. This article has explained the general syntax and different examples to view the content of a tar file.