Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

I want to see the contents (list of files and folders) of an archive, for example a tar.gz file without extracting it.

Are there any methods for doing that?

share|improve this question

8 Answers 8

up vote 38 down vote accepted

Run the below command on terminal to see the contents of a tar.gz file without extracting it,

tar -tf filename.tar.gz

enter image description here

share|improve this answer

You can also use vim

vim filename.tar.gz
share|improve this answer
    
This is awesome. You can also see the contents of the files! –  Nico Oct 21 '14 at 13:24
    
Or use Ex editor: ex +%p foo.tar.gz. –  kenorb Jul 23 at 14:33

less can also open gz-compressed and uncompressed tar archives. It gives you a lovely ls -l style output too:

$ less ~/src/compiz_0.9.7.8-0ubuntu1.6.debian.tar.gz
drwxrwxr-x 0/0               0 2012-09-21 11:41 debian/
drwxrwxr-x 0/0               0 2012-08-09 13:32 debian/source/
-rw-rw-r-- 0/0              12 2012-08-09 13:32 debian/source/format
-rw-rw-r-- 0/0              25 2012-08-09 13:32 debian/libdecoration0-dev.docs
-rw-rw-r-- 0/0              25 2012-08-09 13:32 debian/compiz-dev.docs
-rw-rw-r-- 0/0             347 2012-08-09 13:32 debian/compiz-core.install
-rw-rw-r-- 0/0             125 2012-08-09 13:32 debian/libdecoration0-dev.install
...

And because it's less, you can scroll through it, search it, etc. However it fails miserably with other compression algorithms (in my experience).

share|improve this answer

Why not use vim to browse your archive and open files (at least text-like files):

vim archive.tar.gz

enter image description here

Press the arrow keys to scroll and Enter to open a file.

share|improve this answer

tar's -t flag will list contents for you. Add that to your other flags (so -tvfz for a tar.gz, -tvfj for a tar.bz2, etc) and you can browse without extracting. From there you can extract single files quite easily

tar -xvfz mybackup.tar.gz path/to/file

The big problem with tar is remembering all the other flags. So I usually rely on 7z (of the p7zip-full package) to do all my archiving. I won't claim it is entirely better but it supports almost everything (without having to specify compression type) and the arguments are logical.

7z l archive.ext
7z e archive.ext path/to/file

It's certainly less capable, but you don't need the man page to use it.

There's also Midnight Commander (mc). This is an all-around badass for quasi-graphical terminal-based file management and with some light testing it just let you browse into both .tar.gz and .7z archives. I'm not sure how many others it supports.

share|improve this answer

You could use the z command: zcat, zless, zgrep.

To view a files content use:

zcat file.gz   

To grep something use:

zgrep test file.gz   

To check difference between files use:

zdiff file1.gz file2.gz

These are just a few example, there are many more.

share|improve this answer

Midnight Commander (mc) also has a good compressed file viewer, although I consider this a bit of cheating since mc is a file manager, albeit a text-based one.

Also, if all you want is to see what's inside compressed archives, you could learn the "view" command for each compressor. tar tzvf will show you the contents of a tar file, unzip -l will do it for a zip file, and so on.

share|improve this answer

Well, that depends on the file. Most (de)compression programs have a flag that lists an archive's contents.

  1. tar/tar.gz/tgz/tar.xz/tar.bz2/tbz files

    $ tar tf foo.tgz 
    dir1/
    dir1/subdir1/
    dir1/subdir1/file
    dir1/subdir2/
    dir1/subdir2/file
    dir2/
    
  2. zip files

    $ zip -sf foo.zip 
    Archive contains:
      dir1/
      dir2/
      dir1/subdir1/
      dir1/subdir1/file
      dir1/subdir2/
      dir1/subdir2/file
    Total 6 entries (0 bytes)
    
  3. 7zip files

    $ 7z l foo.7z 
    
    7-Zip [64] 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18
    p7zip Version 9.20 (locale=en_US.utf8,Utf16=on,HugeFiles=on,4 CPUs)
    
    Listing archive: foo.7z
    
    --
    Path = foo.7z
    Type = 7z
    Solid = -
    Blocks = 0
    Physical Size = 168
    Headers Size = 168
    
       Date      Time    Attr         Size   Compressed  Name
    ------------------- ----- ------------ ------------  ------------------------
    2015-03-30 19:00:07 ....A            0            0  dir1/subdir1/file
    2015-03-30 19:00:07 ....A            0            0  dir1/subdir2/file
    2015-03-30 19:07:32 D....            0            0  dir2
    2015-03-30 19:00:07 D....            0            0  dir1/subdir2
    2015-03-30 19:00:07 D....            0            0  dir1/subdir1
    2015-03-30 19:00:06 D....            0            0  dir1
    ------------------- ----- ------------ ------------  ------------------------
                                         0            0  2 files, 4 folders
    
  4. rar files

     $ rar v foo.rar 
    
    RAR 4.20   Copyright (c) 1993-2012 Alexander Roshal   9 Jun 2012
    Trial version             Type RAR -? for help
    
    Archive foo.rar
    
    Pathname/Comment
                      Size   Packed Ratio  Date   Time     Attr      CRC   Meth Ver
    -------------------------------------------------------------------------------
     dir1/subdir1/file
                         0        8   0% 30-03-15 19:00 -rw-r--r-- 00000000 m3b 2.9
     dir1/subdir2/file
                         0        8   0% 30-03-15 19:00 -rw-r--r-- 00000000 m3b 2.9
     dir1/subdir1
                         0        0   0% 30-03-15 19:00 drwxr-xr-x 00000000 m0  2.0
     dir1/subdir2
                         0        0   0% 30-03-15 19:00 drwxr-xr-x 00000000 m0  2.0
     dir1
                         0        0   0% 30-03-15 19:00 drwxr-xr-x 00000000 m0  2.0
     dir2
                         0        0   0% 30-03-15 19:07 drwxr-xr-x 00000000 m0  2.0
    -------------------------------------------------------------------------------
        6                0       16   0%
    

That's most of the more popular archive formats. With all this in mind, you could write a little script that uses the appropriate command depending on the extension of the file you give to it:

#!/usr/bin/env bash

for file in "$@"
do
    printf "\n-----\nArchive '%s'\n-----\n" "$file"
    ## Get the file's extension
    ext=${file##*.}
    ## Special case for compressed tar files. They sometimes
    ## have extensions like tar.bz2 or tar.gz etc.
    [[ "$(basename "$file" ."$ext")" =~ \.tar$ ]] && ext="tgz"

    case $ext in
        7z)
            type 7z >/dev/null 2>&1 && 7z l "$file" || 
            echo "ERROR: no 7z program installed"
            ;;
        tar|tbz|tgz)
            type tar >/dev/null 2>&1 && tar tf "$file"|| 
            echo "ERROR: no tar program installed"
            ;;
        rar)
            type rar >/dev/null 2>&1 && rar v "$file"|| 
            echo "ERROR: no rar program installed"
            ;;
        zip)
            type zip >/dev/null 2>&1 && zip -sf "$file"|| 
            echo "ERROR: no zip program installed"
            ;;
        *)
            echo "Unknown extension: '$ext', skipping..."
            ;;
    esac
done

Save that script in your PATH and make it executable. You can then list the contents of any archive:

$ list_archive.sh foo.rar foo.tar.bz foo.tar.gz foo.tbz foo.zip

-----
Archive 'foo.rar'
-----

RAR 4.20   Copyright (c) 1993-2012 Alexander Roshal   9 Jun 2012
Trial version             Type RAR -? for help

Archive foo.rar

Pathname/Comment
                  Size   Packed Ratio  Date   Time     Attr      CRC   Meth Ver
-------------------------------------------------------------------------------
 dir1/subdir1/file
                     0        8   0% 30-03-15 19:00 -rw-r--r-- 00000000 m3b 2.9
 dir1/file
                     0        8   0% 30-03-15 19:29 -rw-r--r-- 00000000 m3b 2.9
 dir1/subdir1
                     0        0   0% 30-03-15 19:00 drwxr-xr-x 00000000 m0  2.0
 dir1
                     0        0   0% 30-03-15 19:29 drwxr-xr-x 00000000 m0  2.0
 dir2
                     0        0   0% 30-03-15 19:07 drwxr-xr-x 00000000 m0  2.0
-------------------------------------------------------------------------------
    5                0       16   0%


-----
Archive 'foo.tar.bz'
-----
dir1/
dir1/subdir1/
dir1/subdir1/file
dir1/file
dir2/

-----
Archive 'foo.tar.gz'
-----
dir1/
dir1/subdir1/
dir1/subdir1/file
dir1/file
dir2/

-----
Archive 'foo.tbz'
-----
dir1/
dir1/subdir1/
dir1/subdir1/file
dir1/file
dir2/

-----
Archive 'foo.zip'
-----
Archive contains:
  dir1/
  dir1/subdir1/
  dir1/subdir1/file
  dir1/file
  dir2/
Total 5 entries (0 bytes)

And since someone mentioned that lesser editor, naturally, emacs can also do this:

emacs showing an archive's contents

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.