OSETC TECH

How to List the Content of a Tar File in Linux

This post will guide you how to list the content of an archive tar file in your Linux system. How do I only List the contents of a tar file, tar.gz file without extracting it from Linux command line.

What is Tar File?


Tar is a computer software utility for collecting many files into one archive file, often referred to as a tarball, for distribution or backup purposes. The name is derived from (t)ape (ar)chive, as it was originally developed to write data to sequential I/O devices with no file system of their own.
GNU tar is an archiving program designed to store multiple files in a single file (an archive), and to manipulate such archives. The archive can be either a regular file or a device (e.g. a tape drive, hence the name of the program, which stands for tape archiver), which can be located either on the local or on a remote machine.

Tar command options:

-t: List the contents of an archive. Arguments are optional. When given, they specify the names of the members to list.
-v: Verbosely list files processed.
-z: Filter the archive through gzip
-j, –bzip2: Filter the archive through bzip2(1).
-J, –xz: Filter the archive through xz(1).

List Content of Tar File without Extracting it


If you want only list or view the contents of a tarball file without extracting it, and you need to use tar command with -t option. For example, you want to List content of a Tar archive file called mytest.tar, and you can issue the following command to list archive file contents. Type:

$ tar -tvf mytest.tar

Outputs:

devops@devops:~/tmp$ tar -tvf mytest.tar
-rw-rw-r-- devops/devops 0 2019-09-07 04:10 test1
-rw-rw-r-- devops/devops 0 2019-09-07 04:10 test2
-rw-rw-r-- devops/devops 0 2019-09-07 04:10 test3

List the Contents of a Tar.gz File


If you want to list the contents of a tar.gz archive file, and you can use tar command with -zt options. Type:

$ tar -ztvf mytest.tar.gz

Outputs:

devops@devops:~/tmp$ tar -ztvf mytest.tar.gz
-rw-rw-r-- devops/devops 0 2019-09-07 04:10 test1
-rw-rw-r-- devops/devops 0 2019-09-07 04:10 test2
-rw-rw-r-- devops/devops 0 2019-09-07 04:10 test3

List the Contents of a Tar.bz2 File


If you want to list the contents of a tar.bz2 file in your Linux system, and you can run the following tar command with -jt option from the command line:

$ tar -jtvf mytest.tar.bz2

Outputs:

devops@devops:~/tmp$ tar -jtvf mytest.tar.bz2
-rw-rw-r-- devops/devops 0 2019-09-07 04:10 test1
-rw-rw-r-- devops/devops 0 2019-09-07 04:10 test2
-rw-rw-r-- devops/devops 0 2019-09-07 04:10 test3

List the Contents of a Tar.xz File


If you have a archive file with tar.xz extension format, and you want to list the contents of this tar.xz archive file only, and you can use tar command with -Jt option from command line on your Linux system, type:

$ tar -Jtvf mytest.tar.xz

Outputs:

devops@devops:~/tmp$ tar -Jtvf mytest.tar.xz
-rw-rw-r-- devops/devops 0 2019-09-07 04:10 test1
-rw-rw-r-- devops/devops 0 2019-09-07 04:10 test2
-rw-rw-r-- devops/devops 0 2019-09-07 04:10 test3

Searching for Specific Files from a Tar file


If you only want to search for specific files ‘test1’ from a given tar file called mytest.tar, and you can issue the following command from command line:

$ tar -tvf mytest.tar ‘test1’

Outputs:

devops@devops:~/tmp$ tar -tvf mytest.tar 'test1'
-rw-rw-r-- devops/devops 0 2019-09-07 04:10 test1

Conclusion


You should know that how to List or display only the contents of tar, tar.gz, tar.xz or tar.bz2 file on your CentOS or RHEL or Ubuntu Linux operating system or Unix system.