In this article let us review how to perform normal file operation on a compressed files using the powerful Linux Z commands.
Some of these z commands uncompresses the file temporarily in the /tmp directory to perform the specified operation. Some of the z commands uncompresses it on the fly to perfom the specified operation. But, under any case, z commands gives the peace of mind, as you don’t want to worry about the overhead of uncompressing the compressed file to perform an operation.
You can do the following normal file operations on the compressed file
- Viewing the compressed file with zcat.
- Paging the compressed file with zless / zmore.
- Searching inside the compressed file with zgrep / zegrep.
- Comparison of file using zdiff / zcmp
Example 1: View Compressed File and Uncompress with zcat
Compressing a file using gzip creates a compressed file with *.gz extension. You can view a compressed file with zcat with the following way. Which would be as same as the uncompressed file operation ‘cat filename’. zcat uncompresses the file and shows it in the stdout.
$ zcat filename.gz | more
$ ls -l big-file.* -rw-r--r-- 1 ramesh ramesh 24853275 May 9 15:14 big-file.txt $ gzip big-file.txt [Note: Compress the file] $ ls -l big-file.* -rw-r--r-- 1 ramesh ramesh 9275204 May 9 15:14 big-file.txt.gz $ zcat big-file.txt.gz [Note: View the file without uncompressing it] zcat big-file.txt.gz > big-file.txt [Note: Uncompress the file]
Example 2: View a gzipped file which don’t have the gz suffix.
You can uncompress a gzipped file which don’t have the gz suffix. If you try to uncompress a gzipped file which don’t have the gz suffix with “gunzip” or “gzip -d” command you will face the following error.
gunzip: auth.log: unknown suffix -- ignored
But this zcat will uncompress the file and shows the content as shown below.
$ cat > test-file.txt This is a test file used for gunzip and zcat testing zcat is awesome command. $ gzip test-file.txt $ mv test-file.txt.gz test-file-no-ext $ gzip -d test-file-no-ext gzip: test-file-no-ext: unknown suffix -- ignored $ zcat test-file-no-ext This is a test file used for gunzip and zcat testing zcat is awesome command.
Example 3: Display the file content without worrying about whether it is compressed or not
When you are not sure whether a file is compressed or not, you can still view the file without worrying about it’s compression status as shown below.
In this example, If the input-file is compressed zcat will display the content by uncompressing it. If the input-file is not compressed zcat will display the content as it is.
$ zcat -f input-file
Example 4: Paging the compressed file with zless / zmore.
You can paginate a compressed file with zless command or zmore command as shown below.
$ zcat filename.gz | more $ zcat filename.gz | less (or) $ zless filename.gz $ zmore filename.gz
Note: To open any kind of file type, refer to our previous article Open & View 10 Different File Types with Linux Less Command – The Ultimate Power of Less.
Example 5: Searching inside the compressed file with zgrep / zegrep.
You can search inside a compressed file with zgrep / zegrep as shown below. This would be as same as the uncompressed file operation ‘grep -i filename’. All the options to the zgrep command will be passed to grep, and the file will be fed to grep command. It may uncompress and feed the file to grep command if needed.
$ cat > test-file.txt gzip, gunzip, zcat - compress or expand files zless - file perusal filter for crt viewing of compressed text zcmp, zdiff - compare compressed files $ grep -i less test-file.txt zless - file perusal filter for crt viewing of compressed text $ gzip test-file.txt $ zgrep -i less test-file.txt.gz zless - file perusal filter for crt viewing of compressed text
Note: Become familiar with the grep command by reading our earlier article Get a Grip on the Grep! – 15 Practical Grep Command Examples.
Example 6: Comparison of file using zdiff / zcmp
You can compare two compressed files with zdiff / zcmp as shown below. This would be same as the uncompressed file operation ‘diff file1 file2’.
$ cat > file1.txt This is line one This is line two $ cat > file2.txt This is line 1 This is line two $ diff file1.txt file2.txt 1c1 < This is line one --- > This is line 1 $ gzip file1.txt file2.txt $ zdiff file1.txt.gz file2.txt.gz 1c1 < This is line one --- > This is line 1
This article was written by SathiyaMoorthy, Author of of Vi / Vim Tips and Tricks articles. The Geek Stuff welcomes your tips and guest articles.
Comments on this entry are closed.
Thanks a ton for the article…I will definitely use is as I have to grep through compressed history logs quite often. Can you tell me though which is faster zcat |grep *expr* or zgrep *expr* ?
Jason
Thanks a lot for sharing….Z commands are really powerful and helpful
Regards
Abdulrehman
You have saved my day.
Zcat is like Lion…. the king of all commands… 🙂 🙂
Is there a Z equivalent for “ls” like zls?
I’m imagining that “zls file.gz” is like “ls somedirectory”. “zcat -l file.gz” is already similar to “ls -l somedirectory”
“zipinfo -l” can’t be used for *.gz
zcat is not working for me because my gzip file is made up of multiple files. What is the unix command to display the filenames in the gzip file? Thank you ahead of time.
Hi,
Getting following error while performing following command
-bash: /usr/bin/zgrep: Argument list too long
zgrep abc *.gz
I m trying to check pattern abc in multiple zipped files
Where are zhead and ztail?
The command to see filenames inside gzipped files is: gunzip -l file.gz
If it is a tar.gz file, then: tar -tvf file.tar.gz
“-bash: /usr/bin/zgrep: Argument list too long”
You should try: find ./ -name “*.gz” | xargs zgrep
I have a “file.tar.gz, that is made by other gzipped files.
tar -tvf file.tar.gz
-rw-r–r– user/group 0 2015-12-09 11:09:54 1.gz
-rw-r–r– user/group 0 2015-12-09 11:09:57 2.gz
How to zgrep a string in the inside 1.gz and/or 2.gz , without decompressing all files?
This is a example. In fact I have 18G gz files, with thousands of gz files from a period.