How to View Contents of Compressed Files in Linux

We use compression or archives the file & folders either to backup or to save space on our system. We can use tar, gzip or any other utility to perform the compression/archiving of the files and folders. We can than keep them for as long as necessary but you might forget about the contents that were archived.

To be able to see what content was archived, we can extract and see it, which seems rather easy & straight forward if we are dealing with small size files but it will be pretty time consuming process, if we are to extract heavy files. But that’s not the only way to see the contents of a compressed file or folder. We can also use commands to see the contents of a compressed file or of a folder without extracting it.

In this tutorial, we are going to discuss all such commands, which can be used to view the contents of compressed files & folders without extracting in Linux. Let’s start with the commands that are used for viewing contents of a compressed folder,

View content of an archived/compressed folder without extracting

If we have an archived folder with tar as extension, we can check the contents of the archive by using the tar command with option ‘t’. For examples,

[linuxtechi@localhost ~]$ tar -tvf backup.tar

Same command can also be used for checking the contents of a tar.gz or files with other extensions like tgz, tar.xz, tar.bz2, tbz2

[linuxtechi@localhost ~]$ tar -tvf backup.tar.gz
[linuxtechi@localhost ~]$ tar -tvf backup.tgz
[linuxtechi@localhost ~]$ tar -tvf backup.tbz2

Read more on tar command: 17 useful tar command with practical examples in Linux

If you have a zipped folder, than we can use zip command with ‘sf‘ options to view all the contents of zipped folder,

[linuxtechi@localhost ~]$ zip -sf

Apart from the above commands, one can also use VIM for checking contents of the archived folder with the above mentioned extensions, i.e. with extensions zip, tar, tgz, tar.xz, tar.bz2, tbz,

[linuxtechi@localhost ~]$ vim backup.xz
[linuxtechi@localhost ~]$ vim

Apart from VIM, we can also use ‘less command‘ to view the contents of above mentioned archive extensions,

[linuxtechi@localhost ~]$ less backup.tgz
[linuxtechi@localhost ~]$ less backup.tbz2

For archives with file extension 7z, we can use the following command to see all the contents of the archives folder,

[linuxtechi@localhost ~]$ 7z l backup.7z

Read Also : How to use 7zip Compression tool from Linux Terminal

While dealing with archived with rar extension, use ‘v’ option in rar command to list out all the contents of the archives folder,

[linuxtechi@localhost ~]$ rar v backup.rar

These were some commands to check the contents of an archived or a compressed folder. Now we will check out the commands that we can use for viewing the contents of a file.

View content of an archived / compressed file without extracting

We can read the contents of the file that has been either compressed on gzip format or zip format. We can use vim, less as well as tar and zip commands to read the contents of compressed file.  For example,

[linuxtechi@localhost ~]$ vim messages-bak.tar
[linuxtechi@localhost ~]$ less messages-bak.xz
[linuxtechi@localhost ~]$ tar -tf messages-bak.bz2
[linuxtechi@localhost ~]$ zip -sf

Apart from these commands, we can also use some ‘z commands‘. These various z commands allows us to read the contents of gzip compressed files. Let’s discuss them one by one,

zcat command

This is similar to cat command but for compressed files. It concatenates the compressed files & prints the output on to the screen,

[linuxtechi@localhost ~]$ zcat messages-20171211.gz
zless & zmore commands

zmore & zless commands are similar to less & more commands used normally in Linux & they perform the same functions for compressed files as well, i.e. display output one screen at a time,

[linuxtechi@localhost ~]$ zless messages-20171211.gz
[linuxtechi@localhost ~]$ zmore messages-20171211.gz
zgrep command

This is another important z command & performs the same functionality as grep command i.e. we can perform a simple search or a complex (regex) search on compressed file with the help of zgrep command,

[linuxtechi@localhost ~]$ zgrep swVersion messages-20171211.gz
[linuxtechi@localhost ~]$ zgrep -w '^foo|bar' messages-20171211.gz

We can also use other variations of grep i.e. egrep or fgrep with z commands as well.

zdiff command

zdiff command provides the same functionality as diff command & is used to compare two compressed files for differences. We can also use zcmp to perform the same funtion,

[linuxtechi@localhost ~]$ zdiff messages.gz messages-20171211.gz
[linuxtechi@localhost ~]$ zcmp messages.gz messages-20171211.gz
znew command

This command is somewhat different from other mentioned z commands, it is used to recompress files from .Z to .gz,

[linuxtechi@localhost ~]$ znew backup.Z

This will convert the backup.Z file to a file with .gz extension.

This was our tutorial on various Linux commands that can be used to view contents of compressed file and Folders in Linux. If you have any questions or queries, please do let us know using the comment box below.

Also Read : Linux Zip and Unzip Command with Examples

5 thoughts on “How to View Contents of Compressed Files in Linux”

  1. Hi
    I am trying to use below command to view the content of the 7z file without extracting but i am getting the error. Is there any other way to see the file. and this 7z file is a bunch of binary files.
    7z l xyz.7z | cat xyzfile1

  2. It’s simply informative article for all. I will share this blog with my team and i am dame sure this will be very helpful to all Linux Users. Great work and keep sharing your ideas!


Leave a Comment