zcat命令示例:

john@ubuntu01-pc:~/test$ ls -l
total 4
-rw-rw-r-- 1 john john 51 Jun 22 12:28 test.txt

john@ubuntu01-pc:~/test$ cat test.txt
acpi
adduser.conf
alternatives
anacrontab
apg.conf

john@ubuntu01-pc:~/test$ gzip test.txt

john@ubuntu01-pc:~/test$ ls -l
total 4
-rw-rw-r-- 1 john john 76 Jun 22 12:28 test.txt.gz

john@ubuntu01-pc:~/test$ zcat test.txt.gz 
acpi
adduser.conf
alternatives
anacrontab
apg.conf

在上面的示例中,我们有一个名为test.txt的文本文件。
接下来,我们使用压缩实用程序gzip来压缩文件。这将导致创建一个名为test.txt.gz的文件。要查看此文件的内容,我们使用zcat命令:zcat test.txt.gz, 执行此命令后,我们可以看到此压缩文件的内容。

您还可以使用重定向将zcat命令的输出发送到另一个文件:
zcat test.txt.gz> newfile.txt

john@ubuntu01-pc:~/test$ zcat test.txt.gz > newfile.txt

john@ubuntu01-pc:~/test$ 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

john@ubuntu01-pc:~/test$ cat newfile.txt 
acpi
adduser.conf
alternatives
anacrontab
apg.conf

zcat的另一个非常有用的功能是可以查看没有压缩的文件。例如,如果文件没有被压缩,那么文件内容仍然会显示出来。

john@ubuntu01-pc:~/test$ 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

john@ubuntu01-pc:~/test$ zcat -f newfile.txt
acpi
adduser.conf
alternatives
anacrontab
apg.conf

john@ubuntu01-pc:~/test$ zcat -f test.txt.gz
acpi
adduser.conf
alternatives
anacrontab
apg.conf

zmore命令示例:

john@ubuntu01-pc:~/test$ ls -l /etc/ > etc.txt
john@ubuntu01-pc:~/test$ ls -l
total 16
-rw-rw-r-- 1 john john 14989 Jun 22 13:15 etc.txt

john@ubuntu01-pc:~/test$ gzip etc.txt 
john@ubuntu01-pc:~/test$ ls -l
total 4
-rw-rw-r-- 1 john john 3114 Jun 22 13:15 etc.txt.gz

现在,当我们对压缩文件执行命令zmore时,其输出一次显示在一个屏幕上:

john@ubuntu01-pc:~/test$ zmore  etc.txt.gz
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--
Z命令

使用zcat,zgrep,zdiff,zmore和znew命令处理压缩文件。

znew

znew命令用于重新压缩使用compress命令创建的文件。这些文件的扩展名通常是.Z。然后使用扩展名.gz重新压缩这些文件。

zgrep命令

zgrep是一个命令,可让您以grep允许您搜索正则文件的方式搜索压缩文件中的正则表达式。在以下示例中,我们创建了一个名为test2.txt的新文件。然后使用gzip实用程序压缩了该文件。然后,此操作创建了一个名为test2.txt.gz的压缩文件。接下来,我们将zgrep与选项-i(忽略大小写)一起使用,以查找包含字符串Linux的行。

什么是Z命令?

基本上,Z命令是从字面上以字符Z开头并与压缩文件一起使用的命令。最受欢迎的Z命令是:zcatzgrepzmorezlesszdiffznew

zdiff命令示例:

john@ubuntu01-pc:~/test$ 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

john@ubuntu01-pc:~/test$ cat file1.txt 
This is Line One
This is Line Two
This is Line 3
This is Line Four

john@ubuntu01-pc:~/test$ cat file2.txt 
This is Line One
This is Line Two
This is Line Three
This is Line Four

从上面我们可以看到,除了每个文件中的第三行。 文件很相似。现在,使用gzip命令压缩两个文件。

john@ubuntu01-pc:~/test$ gzip file1.txt file2.txt

john@ubuntu01-pc:~/test$ 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

现在我们有了两个压缩文件,我们可以使用zdiff命令比较两个文件:

john@ubuntu01-pc:~/test$ zdiff file1.txt.gz file2.txt.gz 
3c3
< This is Line 3
---
> This is Line Three

zdiff-zcmp

diff命令用于比较压缩文件的内容。

语法是:zdiff compressed_file compressed_file2

在下面的例子中,我们已经创建了两个文件。

znew命令示例

john@ubuntu01-pc:~/test$ ls /etc/ > newtest.txt

john@ubuntu01-pc:~/test$ ls -l
total 4
-rw-rw-r-- 1 john john 2606 Jun 22 13:49 newtest.txt

john@ubuntu01-pc:~/test$ compress newtest.txt 

john@ubuntu01-pc:~/test$ ls -l
total 4
-rw-rw-r-- 1 john john 1682 Jun 22 13:49 newtest.txt.Z

john@ubuntu01-pc:~/test$ znew newtest.txt.Z

john@ubuntu01-pc:~/test$ ls -l
total 4
-rw-rw-r-- 1 john john 1316 Jun 22 13:49 newtest.txt.gz

在上面的示例中,我们创建了一个名为newtest.txt.Z的压缩文件。然后使用znew命令重新压缩该文件。现在压缩文件的扩展名为.gz

Zless

zless与zmore命令相似,但是,它在屏幕变满后一次只显示一行压缩文件的内容。

zless命令的语法与zmore命令相同:zless compression_file

cat命令

zcat命令用于解压缩命令行上的文件列表或来自标准输入的文件。zcat与命令gunzip -c基本相同。通常,zcat用于查看由gzip压缩实用程序创建的文件。zcat允许您查看这些压缩文件的内容,就像cat命令允许您查看常规文件一样。

zmore

zmore是一个用于一次一页地查看压缩文件内容的命令。zmore处理使用compresspackgzip压缩过的文件。在显示输出时,一旦屏幕被填满,它将暂停。要移动到下一页,只需按空格键

在下面的例子中,我们创建了一个/etc目录列表,并将输出放到一个名为txt的文本文件中。然后用gzip压缩他的文本文件,创建一个名为txt.gz的文件:

zgrep命令示例:

john@ubuntu01-pc:~/test$ ls -l
total 4
-rw-rw-r-- 1 john john 85 Jun 22 13:00 test2.txt

john@ubuntu01-pc:~/test$ cat test2.txt 
Arch
Bodhi Linux
CentOS
Fedora Linux
Mint
Red Hat Enterprise Linux
SLES
Ubuntu Linux

john@ubuntu01-pc:~/test$ gzip test2.txt 
john@ubuntu01-pc:~/test$ ls -l
total 4
-rw-rw-r-- 1 john john 99 Jun 22 13:00 test2.txt.gz

john@ubuntu01-pc:~/test$ zgrep -i linux test2.txt.gz 
Bodhi Linux
Fedora Linux
Red Hat Enterprise Linux
Ubuntu Linux

我们可以看到,通过执行命令zgrep -i linux test.txt.gz,我们现在显示了包含单词Linux的所有行。

日期:2019-04-29 03:17:28 来源:oir作者:oir