248

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?

  • 1
    Use vim to browse into the files – ako Oct 13 at 8:36

11 Answers 11

267

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

tar -tf filename.tar.gz

screenshot of listing compressed tarball files in the terminal

-t, --list
List the contents of an archive. Arguments are optional. When given, they specify the names of the members to list.

  • 1
    does all these examples to look inside compressed file works on other types of format too. like zip , rar , tar etc. ? – Ciasto piekarz Aug 4 '15 at 11:23
  • 3
    tar -tvf xxx.tgz this would also show detail properties of files. – Eric Wang Mar 21 '17 at 2:10
  • 4
    pipe it to tree to see a tree view tar -tf filename.tar.gz | tree – blockloop Apr 6 '17 at 15:28
  • For zip / rar use unzip -l / unrar -l – pLumo Jun 9 '17 at 13:55
  • If you want to list until a specific depth/level only see here. – holzkohlengrill Aug 17 at 23:04
122

You can also use vim

vim filename.tar.gz
  • 9
    This is awesome. You can also see the contents of the files! – Nico Oct 21 '14 at 13:24
  • 1
    Or use Ex editor: ex +%p foo.tar.gz. – kenorb Jul 23 '15 at 14:33
  • @Nico How do you use this to see the contents of a file within the tgz? – 1252748 Dec 14 '16 at 19:15
  • 1
    When you open de file with vim (vim file.tar.gz) it says "Select a file with cursor and press ENTER". You do just that, move the cursor over a file and press ENTER. – Nico Dec 15 '16 at 19:56
  • 2
    With huge archive you just have to be patient. It can be very long to load folder structure. :-) – Hugo H Jul 27 '17 at 17:03
41

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

  • 5
    Didn't work for me. Displayed as a binary file. – JeromeJ Dec 10 '17 at 16:31
  • 2
    You sure you don't have an alias with special options for less that you're not showing here? I just tried that to see, but it didn't work. I don't have any aliases setup for less. – code_dredd Jul 20 '18 at 22:14
37

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.

9

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.

9

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

6

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.

4

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.

2

Using view filename.tar.gz will also work. much in the same way vim does, but without write permissions.

2

lesspipe is a shell script installed by default as part of the less package that can list the contents of a tar.gz archive, as well as a range of other common archive file formats.

$ lesspipe example.tar.gz
drwxrwxr-x ubuntu/ubuntu     0 2018-11-16 05:32 example/
-rw-rw-r-- ubuntu/ubuntu     7 2018-11-16 05:32 example/ask.txt
-rw-rw-r-- ubuntu/ubuntu     7 2018-11-16 05:32 example/ubuntu.txt

It is called by the less command (see Oli's answer) as an input preprocessor if the $LESSOPEN environment variable is set appropriately.

If feeling adventurous, take a peak at vi /usr/bin/lesspipe to see what commands it uses. For files matching the tar.gz extension, we can see that it uses tar tzvf under the hood along with the --force-local option to disable an obscure feature of tar that would otherwise confuse colons in the filename with a command to use a remote tape drive:

*.tar.gz|*.tgz|*.tar.z|*.tar.dz)
        tar tzvf "$1" --force-local

Note that because it's primarily designed as a preprocessor for less, it won't output anything if it doesn't recognise the file type. I noticed that some .tar.gz files I downloaded wouldn't work because they didn't actually use gzip compression despite the filename.

0

If you want to view the contents of a specific file within an archive without extracting the archive or writing to disk in any way, use the -O (capital o) flag to write to stdout instead of a file.

tar -Oxvf gerald.tar.gz /path/to/sandra.txt | less

Make sure the O comes before the f or it will try to parse the O as a filename.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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