Unix Z Commands
In this tutorial we can discuss about some of the powerful Z commands in Linux to perform normal operations on a compressed file. Some Z commands uncompress the file temporarily in /tmp directory to perform the operation defined. An advantage of using Z commands to uncompress a file is that it does not really uncompress the files to perform the operation. It will be a great relief for those who really are not intended to uncompress the compressed files for any verification or checking purposes.
Following operations can be performed on a compressed file:
- zcat command is used to view a compressed file.
- Zless/ zmore command is used to page a compressed file.
- Zgrep command is used to search inside a compressed file.
- Zdiff/ zcmp is used to compare file.
View Compressed File and Uncompress with zcat
A compressed file can be viewed by using the command zcat. Compression is done using the command gzip which gives the output as a compressed file with *.gz extension and .zip. The output of the zcat command is nothing, but same as the cat command operation for uncompressed file and which shows the output in the stdout.
$ zcat example_file.zip
$ cat test.gz
CDKNMS4 456 124 786 468 726 782 189
CDKNMS5 157 785 1773 752 786 289 1785
CDKNMS7 417 65 14 4517 96 3578 278
CDMBMS1 647 254 256 789 784 389 165
View a gzipped file which don’t have the .gz suffix
There might be some files which are gzipped files and do not have the .gz suffix with it. Following error occurs while giving a try to uncompress a gzipped file missing with a .gz suffix with the help of “gunzip” or “gzip -d”.
$ gunzip example_file
gzip: test_file: unknown suffix – ignored
In other cases, we can use zcat to uncompress the file and display the content as shown below:
$ cat > example_file
———————————–
THIS FILE IS A ZIPPED FILE.
———————————-
$ gzip example_file
$ mv example_file.gz example_file
$ gzip -d example_file
gzip: example_file: unknown suffix – ignored
$ zcat sample_file
———————————–
THIS FILE IS A ZIPPED FILE.
———————————-
Paging the compressed file with zless / zmore
It is possible to paginate a compressed file with the help of zless and zmore commands.
$ zmore security_file.gz
——> security_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:4:3:adm:/var/adm:/sbin/nologin
lp:x:3:6:lp:/var/spool/lpd:/sbin/nologin
$ zless secured_file.gz
apache:x:48:48:Apache:/var/www:/sbin/nologin
mysql:x:32:33:MySQL Server:/var/lib/mysql:/bin/bash
systemfj:x:384:489::/opt/ibm/ibm-config-firejail/systemfj:/bin/bash
secured_file.gz (END)
Searching inside the compressed file with zgrep / zegrep
This command allows to search inside a compressed file. Everyone uses the grep command where it allows you to search the strings in a file. Likewise zgrep command will search for string with the help of grep command. In case of any uncompression it will input the file to the grep command if it is needed. The usage of zgrep is more similar to grep command itself.
In the given example a pattern in a file is searched also the numbers related to that pattern
$ zgrep -n root security_file.gz
root:x:0:0:root:/root:/bin/bash
Multiple searching inside the compressed file with zegrep
zegrep command allows you to produce more specified outputs with lesser output expressions.
In this example we are searching for multiple patterns within a compressed file with the help of zegrep
$ zegrep ‘user1|root|apache’ security_file.gz
root:x:0:0:root:/root:/bin/bash
user1:x:496:497:User1:/home/user1:/bin/bash
apache:x:48:48:Apache:/var/www:/sbin/nologin
If you need any further assistance please contact our support department.