Unix Z Commands – Zcat, Zless, Zgrep, Zegrep and Zdiff Examples

September 7, 2016 | By in LINUX COMMANDS
| Reply More

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 uncompressed the file temporarily in the /tmp directory to perform the specified operation. Some of the z commands uncompressed it to perform 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 Uncompressed with zcat

Compressing a file using gzip creates a compressed file with *.gz extension and all .zip extension file 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 uncompressed the file and shows it in the stdout.

[shaha@oc8535558703 Desktop]$ zcat sample_file.zip
CDKNMS4 524 694 651 735 812 671 553
CDKNMS5 484 389 1899 516 1307 241 1416
CDKNMS7 340 86 54 1597 24 1545 241
CDMBMS1 361 936 517 274 300 916 531
[shaha@oc8535558703 Desktop]$

[shaha@oc8535558703 Desktop]$ zcat Compliance.gz
CDKNMS5 484 389 1899 516 1307 241 1416
CDKNMS7 340 86 54 1597 24 1545 241
CDMBMS1 361 936 517 274 300 916 531

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.

[shaha@oc8535558703 Desktop]$ gunzip sample_file
gzip: sample_file: unknown suffix -- ignored
[shaha@oc8535558703 Desktop]$

But this zcat will uncompress the file and shows the content as shown below.

[shaha@oc8535558703 Desktop]$ cat > sample_file
This is a test file used for gunzip and zcat testing for this article
this is power of z command

[shaha@oc8535558703 Desktop]$ gzip sample_file

[shaha@oc8535558703 Desktop]$ mv sample_file.gz sample_file

[shaha@oc8535558703 Desktop]$ gzip -d sample_file
gzip: sample_file: unknown suffix -- ignored
[shaha@oc8535558703 Desktop]$

[shaha@oc8535558703 Desktop]$ zcat sample_file
This is a test file used for gunzip and zcat testing for this article
this is power of z command
[shaha@oc8535558703 Desktop]$

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.

[shaha@oc8535558703 Desktop]$ zcat -f sample_file
CDKNMS4 524 694 651 735 812 671 553
CDKNMS5 484 389 1899 516 1307 241 1416
CDKNMS7 340 86 54 1597 24 1545 241

Example 4: Paging the compressed file with zless / zmore

You can paginate a compressed file with zless command or zmore command as shown below.

[shaha@oc8535558703 Desktop]$ zmore passwd_file.gz
------> passwd_file.gz <------
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[shaha@oc8535558703 Desktop]$ zless passwd_file.gz
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
systemfj:x:501:489::/opt/ibm/ibm-config-firejail/systemfj:/bin/bash
passwd_file.gz (END)

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.

1) Search the pattern in the file along with associated line number(s) using the -n option in compressed file .

[shaha@oc8535558703 Desktop]$ zgrep -n mysql passwd_file.gz
39:mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
[shaha@oc8535558703 Desktop]$

2) Display all the lines that starts with specified pattern using ^ symbo in compressed file.

[shaha@oc8535558703 Desktop]$ zgrep ^root passwd_file.gz
root:x:0:0:root:/root:/bin/bash
[shaha@oc8535558703 Desktop]$

3) Display all the lines that ends with specified pattern using $ symbol in compressed file.

[shaha@oc8535558703 Desktop]$ zgrep bash$ passwd_file.gz
root:x:0:0:root:/root:/bin/bash
shaha:x:500:501:Shaha Alam:/home/shaha:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
systemfj:x:501:489::/opt/ibm/ibm-config-firejail/systemfj:/bin/bash
[shaha@oc8535558703 Desktop]$

4) search all the empty or blank lines of a file using zgrep in compressed file.

[shaha@oc8535558703 Desktop]$ zgrep ^$ passwd_file.gz
[shaha@oc8535558703 Desktop]$

5) Display N number of lines before & after pattern matching in compressed file.

a) Display Four lines before patten matching using -B option in compressed file.

root@Linux-shah:~# zgrep -B 4 "games" /etc/passwd

b) Display Four lines after pattern matching using -A option in compressed file.

root@Linux-shah:~# zgrep -A 4 "games" /etc/passwd

c) Display Four lines around the pattern matching using -C option in compressed file.

root@Linux-shah:~# zgrep -C 4 "games" /etc/passwd

Example 6: Multiple searching inside the compressed file with zegrep

zegrep command with multiple regex patterns to reduce the output much more,  egrep command to perform a case-insensitive search on multiple regular expressions .

a) Search the multiple pattern in the compressed file with zegrep

[shaha@oc8535558703 Desktop]$ zegrep 'shah|root|apache' passwd_file.gz
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
shaha:x:500:501:Shaha Alam:/home/shaha:/bin/bash
apache:x:48:48:Apache:/var/www:/sbin/nologin
[shaha@oc8535558703 Desktop]$

b) Search the multiple pattern in the file along with associated line number(s) using the -n option in compressed file

[shaha@oc8535558703 Desktop]$ zegrep -n 'shah|root|apache' passwd_file.gz
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
36:shaha:x:500:501:Shaha Alam:/home/shaha:/bin/bash
38:apache:x:48:48:Apache:/var/www:/sbin/nologin
[shaha@oc8535558703 Desktop]$

c) List the file name of those compressed files which contain multiple pattern using -l option.

[shaha@oc8535558703 Desktop]$ zegrep -l 'shah|root|apache' *.gz
passwd_file1.gz
passwd_file2.gz
passwd_file3.gz
passwd_file.gz
[shaha@oc8535558703 Desktop]$

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’.

[shaha@oc8535558703 Desktop]$ cat > file1.txt
This is line one
This is line two

[shaha@oc8535558703 Desktop]$ cat > file2.txt
This is line 1
This is line two

[shaha@oc8535558703 Desktop]$ diff file1.txt file2.txt
1c1
This is line 1

[shaha@oc8535558703 Desktop]$ gzip file1.txt file2.txt

[shaha@oc8535558703 Desktop]$ zdiff file1.txt.gz file2.txt.gz
1c1
This is line 1

Conclusion

In this article we learn of unix Z command power , z commands uncompressed the file temporarily in the /tmp directory to perform the specified operation. Some of the z commands uncompressed it to perform 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 .

Filed Under : LINUX COMMANDS, LINUX HOWTO

Free Linux Ebook to Download

Leave a Reply

All comments are subject to moderation.