List the contents of a tar or tar.gz file

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. Let us see how to display the contents of a tar / tar.gz file (tarball) on Linux or Unix-like systems.

ADVERTISEMENTS

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-pattern'

We can skip few option when try to list contents of tar too.

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

Let us see some examples and command line options:

Task: List contents of tar file

Use the following tar command to list contents of tar file called file.tar:
$ tar -tvf file.tar
Sample outputs:

Fig.01: List archive contents to screen

Fig.01: List archive contents to screen

Task: List the contents of a tar.gz file

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

Task: List the contents of a tar.bz2 file

Run:
$ tar -jtvf file.tar.bz2

Task: Search for specific files

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

Understanding tar command line options that lists contents of tar ball

So far we learned that the basic syntax to display and list contents of a tarball is:
tar [options] file.tarball
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.
  • J (capital J): Filter archive through xz, use to decompress .xz tar files.
  • f filename: Use archive file called filename.

Conclusion

In this quick tutorial, we learned how to list contents of a tar file using various command line options.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
23 comments… add one
  • Shashi Sep 2, 2008 @ 6:19

    Thanks a lot Vivek…This article is very useful for me….

  • Bolser Sep 15, 2008 @ 15:25

    Perfect!

    I don’t know why I always forget this!

    Also the ‘j’ trick is killer! I always just try to use ‘z’, and then give up when bzip won’t play. Now I just need to remember … use j in stead of z when bzip.

  • jitedar Dec 24, 2008 @ 22:37

    great and thx..

  • asd Jan 25, 2009 @ 4:28

    thanks!

  • Amit Verma Jun 3, 2009 @ 10:41

    Rule of Thumb –
    {
    Use ‘z’ while compressing/decompressing/listing the file with GZIP.
    Use ‘j’ while compressing/decompressing/listing the file with BZIP2.
    }
    – with tar command.

    Thanks
    Amit Verma

  • Jeremy Jul 16, 2009 @ 23:16

    The exact thing I was looking for, worked like a charm! :)

  • Ankur Sinha Aug 27, 2009 @ 9:57

    Good Work!!!
    Thanks!!!
    keep it up!!!
    ;)

  • Nomaun Khan Sep 22, 2009 @ 14:20

    Thanks for the helpful contests

  • kiran Oct 23, 2009 @ 13:59

    Thanks a lot for this post guys …:)

    Keep it up …

  • Chris Henley Feb 16, 2010 @ 21:22

    thanks. I’m too lazy to read the man page.

  • ode Sep 8, 2010 @ 10:11

    $ tar -tvf file.tar works for all three file formats.

    $ unzip -l works for zip files

  • Stefan Jul 17, 2011 @ 15:54

    Thanks it works fine however do you know if there is a faster way maybe? I have a 6GB gzip file and it takes ages to list the contents. For smaller files it is ok but for big files it is a pain.

  • Fixticks Jul 21, 2011 @ 21:25

    man pages are too long, thanks vivek.
    @Stefan, u cud try

    tar -ztvf file.tar.gz > files.txt

    now i assume u may be looking for a certain file in that big gzip, so

    cat files.txt | grep certain_file_name_or_part_of_it

    and u can now check for multiple names without the initial wait of listing the big gz’s files.
    I know this coz i have just done it thanks to reading this blog.thx again Vivek.

  • Liban Nov 25, 2011 @ 0:16

    I’m trying to find and display, but this isn’t working for me..

    find -name “.gz” | tar -tvf {};

    • brisohn May 1, 2012 @ 8:09

      try this:
      find -name “*.gz” -exec tar -tvf {} \;

  • Moron Mar 12, 2012 @ 21:31

    I am getting following error while looking for content of the tar file

    $ tar -ztvf
    tar: illegal option — z

    Usage: tar -{c|r|t|u|x} [ -BdDEFhilmopRsUvw ] [ -Number ] [ -f TarFile ]
    [ -b Blocks ] [ -S [ Feet ] | [ Feet@Density ] | [ Blocksb ] ]
    [ -L InputList ] [-X ExcludeFile] [ -N Blocks ] [ -C Directory ] File …
    Usage: tar {c|r|t|u|x} [ bBdDEfFhilLXmNopRsSUvw[0-9] ]
    [ Blocks ] [ TarFile ] [ InputList ] [ ExcludeFile ]
    [ [ Feet ] | [ Feet@Density ] | [ Blocksb ] ] [-C Directory ] File …

    • Piyush Jan 16, 2013 @ 13:40

      -z option option is not working on AIX

      • 🐧 Vivek Gite Mar 7, 2016 @ 13:28

        Try following on AIX unix:
        gzip -d < file.tar.gz | tar xvf -
        OR use following two commands:
        gunzip file.tar.gz
        tar -xvf file.tar

        Read tar and gunzip man pages for more info on your AIX system.

  • Suresh Jul 17, 2012 @ 7:06

    Sequence is important ! tar -ztfv will fail with error ‘v’ not found where as tar -ztvf works – since you need to specify the filename after the f option. Cheers!

  • Sunny Arora Sep 11, 2014 @ 7:03

    I want to list the files out of file.tar.bz2 and grep a pattern (say all .c files).
    But executing the command ” tar -tjvf fiel.tar.bz2 | grep .c ” doesn’t give the desired output.
    Please suggest

    • mateen Mar 7, 2016 @ 11:09

      tar -tjvf fiel.tar.bz2 | grep *.c would do

  • Ryan Claffey Feb 24, 2016 @ 16:14

    You dont need to put the ‘-‘ symbol it accepts tar tvf /root/myarchive.tar.gz.

    Less is always more!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.