What are Z Commands?
"Z Commands" are commands that literally begin with the character "Z" and are used with compressed files. The most popular Z commands are: "zcat", "zgrep", "zmore", "zless", "zdiff" and "znew". Each of these commands will be explained below along with examples of their use.
zcat command
The "zcat" command is used to uncompress either a list of files on the command line or from standard input. zcat is basically the same as the command gunzip -c. Generally zcat is used to view files that have been created by the gzip compression utility. zcat allows you to view the contents of these compressed files the same way as the "cat" command allows you to view a regular file.
zcat command examples:
$ ls -l
total 4
-rw-rw-r-- 1 john john 51 Jun 22 12:28 test.txt
$ cat test.txt
acpi
adduser.conf
alternatives
anacrontab
apg.conf
$ gzip test.txt
$ ls -l
total 4
-rw-rw-r-- 1 john john 76 Jun 22 12:28 test.txt.gz
$ zcat test.txt.gz
acpi
adduser.conf
alternatives
anacrontab
apg.conf
In the above example we have a text file called "test.txt". This text file contains the following text:
acpi
adduser.conf
alternatives
anacrontab
apg.conf
Next we used the compression utility "gzip" to compress the file. This results in the creation of a file called "test.txt.gz". To view the content of this file we use the "zcat" command: zcat test.txt.gz
After issuing this command, we can see the contents of this compressed file. You can also use re-direction to send the output from the zcat command to another file: zcat test.txt.gz > newfile.txt
$ zcat test.txt.gz > newfile.txt
$ ls -l
total 8
-rw-rw-r-- 1 john john 51 Jun 22 12:42 newfile.txt
-rw-rw-r-- 1 john john 76 Jun 22 12:28 test.txt.gz
$ cat newfile.txt
acpi
adduser.conf
alternatives
anacrontab
apg.conf
Another very useful function of "zcat" is the ability to view none compressed files. For example, if the file is not compressed, then the files contents are still displayed.
$ ls -l
total 8
-rw-rw-r-- 1 john john 51 Jun 22 12:42 newfile.txt
-rw-rw-r-- 1 john john 76 Jun 22 12:28 test.txt.gz
$ zcat -f newfile.txt
acpi
adduser.conf
alternatives
anacrontab
apg.conf
$ zcat -f test.txt.gz
acpi
adduser.conf
alternatives
anacrontab
apg.conf
By using the "-f" parameter, we can view the content of the file without worrying whether the file has been compressed or not
zgrep command
zgrep is a command that allows you to search compressed files for a regular expression in the same way that grep allows you to search a regular file. In the following example we have created a new file called "test2.txt". This file has then been compressed using the gzip utility. This action has then created a compressed file called "test2.txt.gz". Next we use "zgrep" with the option "-i" (ignore case) to look for lines containing the string "Linux".
zgrep command example:
$ ls -l
total 4
-rw-rw-r-- 1 john john 85 Jun 22 13:00 test2.txt
$ cat test2.txt
Arch
Bodhi Linux
CentOS
Fedora Linux
Mint
Red Hat Enterprise Linux
SLES
Ubuntu Linux
$ gzip test2.txt
$ ls -l
total 4
-rw-rw-r-- 1 john john 99 Jun 22 13:00 test2.txt.gz
$ zgrep -i linux test2.txt.gz
Bodhi Linux
Fedora Linux
Red Hat Enterprise Linux
Ubuntu Linux
We can see that by issuing the command "zgrep -i linux test.txt.gz" we have now displayed any lines that contained the word "Linux".
zmore
zmore is a command that is used to view the contents of a compressed file one page at a time. zmore works on files that have been compressed using "compress", "pack" or "gzip". As output is displayed, it pauses once the screen has become filled. To move to the next page, simply press the "Space Bar".
In the example below we have created a listing of the "/etc" directory and placed the output into a text file called "etc.txt". This text file is then compressed using gzip to create a file called "etc.txt.gz":
zmore command example:
$ ls -l /etc/ > etc.txt
$ ls -l
total 16
-rw-rw-r-- 1 john john 14989 Jun 22 13:15 etc.txt
$ gzip etc.txt
$ ls -l
total 4
-rw-rw-r-- 1 john john 3114 Jun 22 13:15 etc.txt.gz
Now when we issue the command "zmore" against our compressed file, its output is displayed one screen at a time:
total 1344
drwxr-xr-x 3 root root 4096 Apr 17 19:22 acpi
-rw-r--r-- 1 root root 2981 Oct 16 2013 adduser.conf
drwxr-xr-x 2 root root 12288 Jun 16 21:42 alternatives
-rw-r--r-- 1 root root 401 Dec 20 2012 anacrontab
-rw-r--r-- 1 root root 112 Oct 1 2012 apg.conf
drwxr-xr-x 6 root root 4096 Oct 16 2013 apm
drwxr-xr-x 3 root root 4096 Apr 17 19:29 apparmor
drwxr-xr-x 8 root root 4096 Jun 13 09:28 apparmor.d
drwxr-xr-x 5 root root 4096 May 22 09:39 apport
drwxr-xr-x 6 root root 4096 Apr 17 18:50 apt
drwxr-xr-x 3 root root 4096 Oct 16 2013 aptdaemon
drwxr-xr-x 2 root root 4096 Apr 17 19:15 at-spi2
drwxr-xr-x 3 root root 4096 Apr 17 19:28 avahi
-rw-r--r-- 1 root root 9013 Mar 24 06:15 avserver.conf
-rw-r--r-- 1 root root 2177 Mar 30 2013 bash.bashrc
-rw-r--r-- 1 root root 45 Jun 17 2012 bash_completion
drwxr-xr-x 2 root root 4096 Jun 16 21:08 bash_completion.d
-rw-r--r-- 1 root root 356 Jan 1 2012 bindresvport.blacklist
-rw-r--r-- 1 root root 321 Jun 12 2013 blkid.conf
lrwxrwxrwx 1 root root 15 Jun 3 21:54 blkid.tab -> /dev/.blkid.tab
drwxr-xr-x 2 root root 4096 Apr 17 19:28 bluetooth
drwxr-xr-x 2 root root 4096 Apr 17 19:14 bonobo-activation
--More--
zless
zless is similar to the "zmore" command, however, it displays the content of a compressed file one line at a time after the screen has become filled.
The syntax of the "zless" command is the same as the zmore command:zless compressed_file
zdiff - zcmp
The zdiff command is used to compare the contents of compressed files.
The syntax is: zdiff compressed_file compressed_file2
In the example below, we have created two files. One file is called "file1.txt" and the other "file2.txt". The contents of the files can seen below:
zdiff command example:
$ ls -l
total 8
-rw-rw-r-- 1 john john 67 Jun 22 13:31 file1.txt
-rw-rw-r-- 1 john john 71 Jun 22 13:32 file2.txt
$ cat file1.txt
This is Line One
This is Line Two
This is Line 3
This is Line Four
$ cat file2.txt
This is Line One
This is Line Two
This is Line Three
This is Line Four
From the above we can see that the files are similar except for the third line within each file. Now lets compress both files using the gzip command.
$ gzip file1.txt file2.txt
$ ls -l
total 8
-rw-rw-r-- 1 john john 61 Jun 22 13:34 file1.txt.gz
-rw-rw-r-- 1 john john 63 Jun 22 13:34 file2.txt.gz
Now that we have two compressed files we can use the "zdiff" command to compare the two files:
$ zdiff file1.txt.gz file2.txt.gz
3c3
< This is Line 3
---
> This is Line Three
From the above we can see that the "zdiff" command has made a line by line comparison and displayed the differences found.
znew
The znew command is used to re-compress a file that has been created with the "compress" command. These files normally have an extension of ".Z". These files are then re-compressed with an extension of ".gz".
Options:
-f Force re-compression from .Z to .gz format even if a .gz file
already exists.
-t Tests the new files before deleting originals.
-v Verbose. Display the name and percentage reduction for each file
compressed.
-9 Use the slowest compression method (optimal compression).
-P Use pipes for the conversion to reduce disk space usage.
-K Keep a .Z file when it is smaller than the .gz file; implies -t.
znew command example:
$ ls /etc/ > newtest.txt
$ ls -l
total 4
-rw-rw-r-- 1 john john 2606 Jun 22 13:49 newtest.txt
$ compress newtest.txt
$ ls -l
total 4
-rw-rw-r-- 1 john john 1682 Jun 22 13:49 newtest.txt.Z
$ znew newtest.txt.Z
$ ls -l
total 4
-rw-rw-r-- 1 john john 1316 Jun 22 13:49 newtest.txt.gz
In the above example we created a compressed file called "newtest.txt.Z". This file was then re-compressed using the "znew" command. This resulted in a compressed file with an extension of ".gz"