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?
Run the below command in the terminal to see the contents of a tar.gz file without extracting it:
tar -tf filename.tar.gz
-t, --list
List the contents of an archive. Arguments are optional. When given, they specify the names of the members to list.
-f, --file=ARCHIVE Use archive file or device ARCHIVE...
tar -tvf xxx.tgz
this would also show detail properties of files.
tree
to see a tree view tar -tf filename.tar.gz | tree
Commented
Apr 6, 2017 at 15:28
You can also use vim
vim filename.tar.gz
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.
:q
exits the current file and brings you back to the file list
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).
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
.
Commented
Jul 20, 2018 at 22:14
.bashrc
contains [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
which makes this work as described.
Commented
Feb 18, 2022 at 6:54
xz
algorithm
Commented
May 26, 2022 at 13:20
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.
Well, that depends on the file. Most (de)compression programs have a flag that lists an archive's contents.
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/
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)
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
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:
Why not use vim
to browse your archive and open files (at least text-like files):
vim archive.tar.gz
Press the arrow keys to scroll and Enter to open a file.
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.
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.
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.
Using
view filename.tar.gz
will also work. much in the same way vim does, but without write permissions.
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.
vim
to browse into the files