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,
[[email protected] ~]$ 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
[[email protected] ~]$ tar -tvf backup.tar.gz [[email protected] ~]$ tar -tvf backup.tgz [[email protected] ~]$ 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,
[[email protected] ~]$ zip -sf remediation-backup.zip
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,
[[email protected] ~]$ vim backup.xz [[email protected] ~]$ vim remediation-backup.zip
Apart from VIM, we can also use ‘less command‘ to view the contents of above mentioned archive extensions,
[[email protected] ~]$ less backup.tgz [[email protected] ~]$ less backup.tbz2
For archives with file extension 7z, we can use the following command to see all the contents of the archives folder,
[[email protected] ~]$ 7z l backup.7z
While dealing with archived with rar extension, use ‘v’ option in rar command to list out all the contents of the archives folder,
[[email protected] ~]$ 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,
[[email protected] ~]$ vim messages-bak.tar [[email protected] ~]$ less messages-bak.xz [[email protected] ~]$ tar -tf messages-bak.bz2 [[email protected] ~]$ zip -sf messages-bak.zip
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,
This is similar to cat command but for compressed files. It concatenates the compressed files & prints the output on to the screen,
[[email protected] ~]$ 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,
[[email protected] ~]$ zless messages-20171211.gz [[email protected] ~]$ zmore messages-20171211.gz
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,
[[email protected] ~]$ zgrep swVersion messages-20171211.gz [[email protected] ~]$ 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 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,
[[email protected] ~]$ zdiff messages.gz messages-20171211.gz [[email protected] ~]$ zcmp messages.gz messages-20171211.gz
This command is somewhat different from other mentioned z commands, it is used to recompress files from .Z to .gz,
[[email protected] ~]$ 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