List the contents of a tar or tar.gz file

last updated in Categories , , , , , ,

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
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: Vivek Gite

The author is the creator of nixCraft 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.

Notable Replies

  1. Assuming that file name is demo.tar.gz, run:

    tar ztvf demo.tar
    

    Look for 4th column:

    drwxr-xr-x vivek/vivek       0 2018-12-31 19:13 t/
    -rw-r--r-- vivek/vivek      73 2018-12-31 19:13 t/resolv.conf
    -rw-r--r-- vivek/vivek    1123 2018-12-31 19:13 t/hosts
    

Continue the discussion www.nixcraft.com

1 more reply

Participants

Historical Comment Archive

25 comment

  1. 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.

  2. 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

  3. 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.

  4. 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.

  5. 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 …

      1. 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.

  6. 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!

  7. 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

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

    Less is always more!

    Still, have a question? Get help on our forum!