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?
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:
You can also use vim
less
can also opengz
-compressed and uncompressedtar
archives. It gives you a lovelyls -l
style output too:And because it's
less
, you can scroll through it, search it, etc. However it fails miserably with other compression algorithms (in my experience).You could use the z command:
zcat
,zless
,zgrep
.To view a files content use:
To grep something use:
To check difference between files use:
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
fileszip
files7zip
filesrar
filesThat'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:
Save that script in your
PATH
and make it executable. You can then list the contents of any archive:And since someone mentioned that lesser editor, naturally, emacs can also do this:
tar
's-t
flag will list contents for you. Add that to your other flags (so-tvfz
for atar.gz
,-tvfj
for atar.bz2
, etc) and you can browse without extracting. From there you can extract single files quite easilyThe big problem with
tar
is remembering all the other flags. So I usually rely on7z
(of thep7zip-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.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.Why not use
vim
to browse your archive and open files (at least text-like files):Press the arrow keys to scroll and Enter to open a file.
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.lesspipe
is a shell script installed by default as part of theless
package that can list the contents of atar.gz
archive, as well as a range of other common archive file formats.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 thetar.gz
extension, we can see that it usestar tzvf
under the hood along with the--force-local
option to disable an obscure feature oftar
that would otherwise confuse colons in the filename with a command to use a remote tape drive: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.Using
view filename.tar.gz
will also work. much in the same way vim does, but without write permissions.