≡ Menu

The Power of Z Commands – Zcat, Zless, Zgrep, Zdiff Examples

Power of Linux Z Commands 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

  1. Viewing the compressed file with zcat.
  2. Paging the compressed file with zless / zmore.
  3. Searching inside the compressed file with zgrep / zegrep.
  4. 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.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Jason May 14, 2009, 12:43 pm

    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

  • Abdulrehman May 15, 2009, 12:04 am

    Thanks a lot for sharing….Z commands are really powerful and helpful

    Regards
    Abdulrehman

  • Mahesh June 8, 2009, 1:12 pm

    You have saved my day.

  • Muneer Dar February 23, 2011, 4:43 am

    Zcat is like Lion…. the king of all commands… 🙂 🙂

  • SomeGuy November 30, 2011, 9:28 pm

    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

  • Felicie February 14, 2013, 8:12 am

    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.

  • sreejith February 25, 2014, 3:17 am

    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

  • Anonymous March 16, 2015, 4:39 am

    Where are zhead and ztail?

  • Moises Cardoso December 9, 2015, 7:01 am

    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

  • Moises Cardoso December 9, 2015, 7:04 am

    “-bash: /usr/bin/zgrep: Argument list too long”

    You should try: find ./ -name “*.gz” | xargs zgrep

  • Moises Cardoso December 9, 2015, 7:13 am

    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.