We use archive tools in Linux to compress and save disk space.
Most of us already know this and use them in our daily activities.
There are several applications on Linux that compress and decompress files.
But we only use a few of them and if you want to know more about the best archive tools for Linux go to the following article.
Many of us may know, how to compress and decompress files, but most of them do not aware how to view archive file contents without extracting it.
In some cases, you may want to view archive file contents or a list of files contained within the archive file.
Most server logs were archived to save more disk space.
If so, what is an option and how to read them.
Is it possible to view all types of log files without extracting .zip, .gz, .bz2, .7z, .tar, .tgz, .tbz2, tar.gz, tar.bz, tar.bz2?
1) How to Read the Contents of a .zip File on Linux Without Extracting
There are several commands in Linux that allows you to view the contents of the compressed file without extracting them.
When you have a single file in the zip archive, you can use one of the following commands to read them: zcat, zless and zmore.
These commands will not work if the zip archive contains more than one file.
Use the zcat
command to read the contents of the .zip file.
$ zcat 2gtest.zip This is a test archive file and named 2gtest.txt. Thanks for visiting our site "2DayGeek.com" !!
Use the zmore
command to read the contents of the .zip file.
$ zmore 2gtest.zip
Use the zless
command to read the contents of the .zip file.
$ zless 2gtest.zip
2) How to Read If Your .zip File Contains More Than One File
If you have more than one file in the zip archive, you can read it individually using the vim
editor.
$ vim 2gtest-multiple.zip zip.vim version v28 Browsing zipfile /home/daygeek/shell-script/backup/2gtest-multiple.zip Select a file with cursor and press ENTER 2gtest.txt 2daygeek.txt
Alternatively, you can view the specific file contents from the .zip archive using the unzip command with the -p
option.
$ unzip -p 2gtest-multiple.zip 2daygeek.txt This is a test archive file and named 2daygeek.txt. Thanks for visiting our site "2DayGeek.com" !!
If you want to view all the contents of files from a .zip file at once, use the following command.
$ unzip -c 2gtest-multiple.zip Archive: 2gtest-multiple.zip inflating: 2gtest.txt This is a test archive file and named 2gtest.txt. Thanks for visiting our site "2DayGeek.com" !! inflating: 2daygeek.txt This is a test archive file and named 2daygeek.txt. Thanks for visiting our site "2DayGeek.com" !!
3) How to Read the Contents of a .gz File on Linux Without Decompress
The same commands can be used for the .gzip archive file.
$ zcat tanisha.txt.gz or $ zmore tanisha.txt.gz or $ zless tanisha.txt.gz or $ vim tanisha.txt.gz This is a test archive file and named tanisha.txt. This is my first baby name and she is pursuing Nursery. Thanks for visiting our site "2DayGeek.com" !!
4) How to View the Contents of a .bz2 File on Linux Without Extracting
Use the following commands to view the contents of the .bzip2/.bz2 file without extracting it.
$ bzcat renusha.txt.bz2 or $ bzmore renusha.txt.bz2 or $ vim renusha.txt.bz2 ------> renusha.txt.bz2 <------ This is a test archive file and named renusha.txt. This is my second baby name and she is a 1 year and 3 month old baby. Thanks for visiting our site "2DayGeek.com" !!
5) How to List the First or Last "N" Lines Without Decompressing a file in Linux
Yes, this can be done using the head and tail commands.
$ zcat file.zip | head -5 $ zcat file.zip | tail -5 $ zcat file.gz | head -5 $ zcat file.gz | tail -5 $ bzcat file.bz2 | head -5 $ bzcat file.bz2 | tail -5
6) How to View a List of Files in the .zip Archive Without Being Extracting
This can be done in two ways. Use the zipinfo
command to view a list of files in the .zip archive.
This shows some useful information as well such compressed file size, actual file size, number of files associated, file permissions and compressed ratio percentage.
$ zipinfo 2gtest-multiple.zip Archive: 2gtest-multiple.zip Zip file size: 499 bytes, number of entries: 2 -rw-r--r-- 3.0 unx 94 tx defN 20-Jan-04 21:56 2gtest.txt -rw-r--r-- 3.0 unx 96 tx defN 20-Jan-04 21:57 2daygeek.txt 2 files, 190 bytes uncompressed, 177 bytes compressed: 6.8%
Also, you can use the zip command with the -sf
option to view the contents of the .zip file.
$ zip -sf 2gtest-multiple.zip Archive contains: 2gtest.txt 2daygeek.txt Total 2 entries (190 bytes)
Additionally, you can view the list of files in the .zip archive using the unzip command with the -l
option.
$ unzip -l 2gtest-multiple.zip Archive: 2gtest-multiple.zip Length Date Time Name --------- ---------- ----- ---- 94 2020-01-04 21:56 2gtest.txt 96 2020-01-04 21:57 2daygeek.txt --------- ------- 190 2 files
Alternatively, you can use the vim
editor to achieve this.
$ vim 2gtest-multiple.zip zip.vim version v28 Browsing zipfile /home/daygeek/shell-script/backup/2gtest-multiple.zip Select a file with cursor and press ENTER 2gtest.txt 2daygeek.txt
7) How to View a List of Files in the .7z Archive Without Being Extracting
This can be done using the 7z command with the l
option.
$ 7z l sudha.7z 7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21 p7zip Version 16.02 (locale=en_IN,Utf16=on,HugeFiles=on,64 bits,8 CPUs Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz (506E3),ASM,AES-NI) Scanning the drive for archives: 1 file, 260 bytes (1 KiB) Listing archive: sudha.7z -- Path = sudha.7z Type = 7z Physical Size = 260 Headers Size = 122 Method = LZMA2:12 Solid = - Blocks = 1 Date Time Attr Size Compressed Name ------------------- ----- ------------ ------------ ------------------------ 2020-01-04 22:27:28 ....A 152 138 sudha.txt ------------------- ----- ------------ ------------ ------------------------ 2020-01-04 22:27:28 152 138 1 files
8) How to Check the Information of a .7z Archived File Without Extracting it
This can be done using the xz command with the l
option.
$ xz -l sudha.txt.xz Strms Blocks Compressed Uncompressed Ratio Check Filename 1 1 196 B 152 B 1.289 CRC64 sudha.txt.xz
9) How to View a List of Files in the .tar Archive Without Being Extracting
This can be done using the native tar command with specific options.
Use the following command to view the list of files in the ".tar" archive file.
$ tar -tvf backup.tar drwxr-xr-x daygeek/daygeek 0 2020-01-04 21:04 home/daygeek/shell-script/backup/ -rwxr-xr-x daygeek/daygeek 229 2019-08-16 16:58 home/daygeek/shell-script/backup/service2.sh.gz -rwxr-xr-x daygeek/daygeek 237 2019-08-16 16:58 home/daygeek/shell-script/backup/service3a.sh.gz -rwxr-xr-x daygeek/daygeek 236 2019-08-16 16:59 home/daygeek/shell-script/backup/service2a.sh.gz -rw-r--r-- daygeek/daygeek 639 2020-01-02 10:43 home/daygeek/shell-script/backup/shell-script.zip -rwxr-xr-x daygeek/daygeek 296 2019-08-16 16:58 home/daygeek/shell-script/backup/servicem.sh -rwxr-xr-x daygeek/daygeek 249 2019-08-16 16:58 home/daygeek/shell-script/backup/service3.sh.bz2 -rwxr-xr-x daygeek/daygeek 157 2019-08-16 16:59 home/daygeek/shell-script/backup/service11.sh -rwxr-xr-x daygeek/daygeek 177 2019-08-16 16:59 home/daygeek/shell-script/backup/service1a.sh -rwxr-xr-x daygeek/daygeek 18 2019-08-19 12:33 home/daygeek/shell-script/backup/test.txt -rw-r--r-- daygeek/daygeek 290 2020-01-04 11:17 home/daygeek/shell-script/backup/service1.sh.zip
Use the following command to view the list of files in the ".tgz" archive file.
$ tar -ztvf backup.tgz or $ tar -ztvf backup.tar.gz drwxr-xr-x daygeek/daygeek 0 2020-01-04 21:04 home/daygeek/shell-script/backup/ -rwxr-xr-x daygeek/daygeek 229 2019-08-16 16:58 home/daygeek/shell-script/backup/service2.sh.gz -rwxr-xr-x daygeek/daygeek 237 2019-08-16 16:58 home/daygeek/shell-script/backup/service3a.sh.gz -rwxr-xr-x daygeek/daygeek 236 2019-08-16 16:59 home/daygeek/shell-script/backup/service2a.sh.gz -rw-r--r-- daygeek/daygeek 639 2020-01-02 10:43 home/daygeek/shell-script/backup/shell-script.zip -rwxr-xr-x daygeek/daygeek 296 2019-08-16 16:58 home/daygeek/shell-script/backup/servicem.sh -rwxr-xr-x daygeek/daygeek 249 2019-08-16 16:58 home/daygeek/shell-script/backup/service3.sh.bz2 -rwxr-xr-x daygeek/daygeek 157 2019-08-16 16:59 home/daygeek/shell-script/backup/service11.sh -rwxr-xr-x daygeek/daygeek 177 2019-08-16 16:59 home/daygeek/shell-script/backup/service1a.sh -rwxr-xr-x daygeek/daygeek 18 2019-08-19 12:33 home/daygeek/shell-script/backup/test.txt -rw-r--r-- daygeek/daygeek 290 2020-01-04 11:17 home/daygeek/shell-script/backup/service1.sh.zip
Use the following command to view the list of files in the ".tbz2" archive file.
$ tar -jtvf backup.tbz2 or $ tar -jtvf backup.tar.bz2 drwxr-xr-x daygeek/daygeek 0 2020-01-04 21:04 home/daygeek/shell-script/backup/ -rwxr-xr-x daygeek/daygeek 229 2019-08-16 16:58 home/daygeek/shell-script/backup/service2.sh.gz -rwxr-xr-x daygeek/daygeek 237 2019-08-16 16:58 home/daygeek/shell-script/backup/service3a.sh.gz -rwxr-xr-x daygeek/daygeek 236 2019-08-16 16:59 home/daygeek/shell-script/backup/service2a.sh.gz -rw-r--r-- daygeek/daygeek 639 2020-01-02 10:43 home/daygeek/shell-script/backup/shell-script.zip -rwxr-xr-x daygeek/daygeek 296 2019-08-16 16:58 home/daygeek/shell-script/backup/servicem.sh -rwxr-xr-x daygeek/daygeek 249 2019-08-16 16:58 home/daygeek/shell-script/backup/service3.sh.bz2 -rwxr-xr-x daygeek/daygeek 157 2019-08-16 16:59 home/daygeek/shell-script/backup/service11.sh -rwxr-xr-x daygeek/daygeek 177 2019-08-16 16:59 home/daygeek/shell-script/backup/service1a.sh -rwxr-xr-x daygeek/daygeek 18 2019-08-19 12:33 home/daygeek/shell-script/backup/test.txt -rw-r--r-- daygeek/daygeek 290 2020-01-04 11:17 home/daygeek/shell-script/backup/service1.sh.zip