img

List the contents of a tar or tar.gz file

September 8, 2019

How do I list the contents of a tar or tar.gz file on a Linux, OS X, and Unix-like system? How do I view contents of tar file without extracting it on Linux? How can I list contents of tar file on Unix?

GNU/tar or BSD/tar is an archiving program designed to store and extract files from an archive file known as a tarfile. You can create a tar file or compressed tar file tar. However, sometimes you need to list the contents of tar or tar.gz file on screen before extracting all files.

How to view contents of tar file without extracting it

To view a detailed table of contents for archive called my-data.tar.gz, use the following syntax:
tar -ztvf my-data.tar.gz
tar -tvf my-data.tar.gz
tar -tvf my-data.tar.gz 'search-patter'

Examples: List the contents of a tar or tar.gz file

Let us see some examples:

Task: List the contents of a tar file

Use the following command:
$ tar -tvf file.tar
Sample outputs:

Fig.01: List archive contents to screen

Task: List the contents of a tar.gz file

Use the following command:
$ tar -ztvf file.tar.gz

Task: List the contents of a tar.bz2 file

Use the following command:
$ tar -jtvf file.tar.bz2

Task: Search for specific files

In this example, look for *.pl (all perl files) inside a detailed table of contents for archive called projects.tar.gz:
$ tar -tvf projects.tar.bz2 '*.pl'
Where,

  • t: List the contents of an archive.
  • v: Verbosely list files processed (display detailed information).
  • z: Filter the archive through gzip so that we can open compressed (decompress) .gz tar file.
  • j: Filter archive through bzip2, use to decompress .bz2 files.
  • f filename: Use archive file called filename.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Article Tags:
Article Categories:
How To

Comments are closed.