How can I view the contents of tar.gz file without extracting from the command-line?

Solution 1:

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.

-f, --file=ARCHIVE Use archive file or device ARCHIVE...

Solution 2:

You can also use vim

vim filename.tar.gz

Solution 3:

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

Solution 4:

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.