≡ Menu

List the contents of a tar or tar.gz file

Q. How do I list the contents of a tar or tar.gz file?

A. GNU/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 sometime you need to list the contents of a tar or tar.gz file on screen before extracting the all files.

Task: List the contents of a tar file

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

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

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
Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 19 comments… add one }

  • Shashi September 2, 2008, 6:19 am

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

  • Bolser September 15, 2008, 3:25 pm

    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 December 24, 2008, 10:37 pm

    great and thx..

  • asd January 25, 2009, 4:28 am

    thanks!

  • Amit Verma June 3, 2009, 10:41 am

    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 July 16, 2009, 11:16 pm

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

  • Ankur Sinha August 27, 2009, 9:57 am

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

  • Nomaun Khan September 22, 2009, 2:20 pm

    Thanks for the helpful contests

  • kiran October 23, 2009, 1:59 pm

    Thanks a lot for this post guys …:)

    Keep it up …

  • Chris Henley February 16, 2010, 9:22 pm

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

  • ode September 8, 2010, 10:11 am

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

    $ unzip -l works for zip files

  • Stefan July 17, 2011, 3:54 pm

    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 July 21, 2011, 9:25 pm

    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 November 25, 2011, 12:16 am

    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 am

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

  • Moron March 12, 2012, 9:31 pm

    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 January 16, 2013, 1:40 pm

      -z option option is not working on AIX

  • Suresh July 17, 2012, 7:06 am

    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 September 11, 2014, 7:03 am

    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

Leave a Comment