xz、unxz和xzcat
压缩工具的新秀,xz为压缩工具,unxz为解压缩工具,xzcat也是在不打开压缩文件的情况下查看文件内容。
语法:xz [OPTION]... FILE... 常用选项: -d:解压缩 -#:指定压缩比,默认为6 -k:压缩并保留原文件
[root@localhost tmp]# xz functions [root@localhost tmp]# ll total 8 -rw-r--r--. 1 root root 4576 Sep 3 06:20 functions.xz
解压缩:
[root@localhost tmp]# unxz functions.xz #使用unxz解压缩 [root@localhost tmp]# ll total 16 -rw-r--r--. 1 root root 15131 Sep 3 06:20 functions [root@localhost tmp]# xz functions [root@localhost tmp]# ll total 8 -rw-r--r--. 1 root root 4576 Sep 3 06:20 functions.xz [root@localhost tmp]# xz -d functions.xz #使用-d选项解压缩 [root@localhost tmp]# ll total 16 -rw-r--r--. 1 root root 15131 Sep 3 06:20 functions
使用-k选项压缩并保留原文件:
[root@localhost tmp]# xz -k functions [root@localhost tmp]# ll total 24 -rw-r--r--. 1 root root 15131 Sep 3 06:20 functions -rw-r--r--. 1 root root 4576 Sep 3 06:20 functions.xz
试试xzcat:
[root@localhost tmp]# xzcat functions.xz # -*-Shell-script-*- # # functions This file contains functions to be used by most or all # shell scripts in the /etc/init.d directory. # TEXTDOMAIN=initscripts # Make sure umask is sane umask 022 # Set up a default search path. PATH="/sbin:/usr/sbin:/bin:/usr/bin" export PATH ......(略)
扩展,是用man手册的时候,我们会发现另外一个工具lzma、unlzma和lzcat,其后缀名为.lzma,记住xz就好啦,它和lzma是有一定的关系的,详细可见man手册。
lzma is equivalent to xz --format=lzma unlzma is equivalent to xz --format=lzma --decompress lzcat is equivalent to xz --format=lzma --decompress --stdout
我们linux内核官网查找内核文件的时候,文件被压缩使用的工具是gzip和xz,也可以看到xz的压缩率更大。
https://www.kernel.org/pub/linux/kernel/v4.x/

现在存在的一个问题是,仅仅只是对单个文件进行压缩,那么这些工具能够对目录进行压缩吗?
我们在tmp文件下创建test目录,拷贝几个文件到里面:
[root@localhost tmp]# ll /tmp/test/ total 332 -rw-r--r--. 1 root root 15131 Sep 3 06:59 functions -rw-------. 1 root root 318014 Sep 3 06:59 messages -rw-r--r--. 1 root root 1054 Sep 3 06:58 passwd
现在来试试压缩目录:
[root@localhost tmp]# gzip /tmp/test/ gzip: /tmp/test/ is a directory -- ignored [root@localhost tmp]# bzip2 /tmp/test/ bzip2: Input file /tmp/test/ is a directory. [root@localhost tmp]# xz /tmp/test/ xz: /tmp/test/: Is a directory, skipping
都不行,那该怎么办呢?接下来我们讲讲tar吧!
打包工具tar
tar命令是用来归档文件的,可以将多个文件或者一个目录归一成一个后缀名为.tar的文件,归档文件并不会压缩文件,反而可能使文件的大小稍微大一点,所以一般在归档之后再执行压缩!。下面我们就来看一下tar的使用方法吧!
语法:tar [OPTION]... FILE... 方法: (1)创建归档 -c -f /path/to/somefile.tar file... -cf /path/to/somefile.tar file... (2)展开归档 -xf /path/from/somefile.tar -xf /path/from/somefile.tar -C /path/to/somedir (3)查看归档文件的文件列表 -tf /path/to/somefile.tar 归档之后然后进行压缩,结合之前的压缩工具,就能实现压缩多个文件。 (4)归档压缩 -z:gzip2 -zcf /path/to/somefile.tar.gz file... -zxf /path/to/somefile.tar.gz -C /path/to/somedir #z可以去掉 -j:bzip2 -jcf /path/to/somefile.tar.bz2 file... -jxf /path/to/somefile.tar.bz2 -C /path/to/somedir #j可以去掉 -J:xz -Jcf /path/to/somefile.tar.xz file... -Jxf /path/to/somefile.tar.xz -C /path/to/somedir #J可以去掉
下面我们就将/tmp/test先使用tar打包成tar文件,再将tar压缩成.xz的压缩文件:
[root@localhost tmp]# tar -cf test.tar test/ [root@localhost tmp]# ll total 340 drwxr-xr-x. 2 root root 53 Sep 3 06:59 test -rw-r--r--. 1 root root 348160 Sep 3 07:35 test.tar [root@localhost tmp]# xz test.tar [root@localhost tmp]# ll total 28 drwxr-xr-x. 2 root root 53 Sep 3 06:59 test -rw-r--r--. 1 root root 26748 Sep 3 07:35 test.tar.xz
使用unxz解压缩,再展开归档至/root下:
[root@localhost tmp]# unxz test.tar.xz #解压缩 [root@localhost tmp]# ll total 340 drwxr-xr-x. 2 root root 53 Sep 3 06:59 test -rw-r--r--. 1 root root 348160 Sep 3 07:35 test.tar [root@localhost tmp]# tar -xf test.tar -C /root/ #展开归档至指定的目录 [root@localhost tmp]# ll /root/ total 4 -rw-------. 1 root root 1707 Aug 10 07:14 anaconda-ks.cfg drwxr-xr-x. 2 root root 53 Sep 3 06:59 test
这样显得有点麻烦,所有在生产环境中,我们一般直接使用选项-z,-j,-J来实现压缩归档。
[root@localhost tmp]# tar -zcf test.tar.gz test/ [root@localhost tmp]# ll total 48 drwxr-xr-x. 2 root root 53 Sep 3 06:59 test -rw-r--r--. 1 root root 46416 Sep 3 07:48 test.tar.gz [root@localhost tmp]# tar -zxf test.tar.gz -C /root/ [root@localhost tmp]# ll /root/test/ total 332 -rw-r--r--. 1 root root 15131 Sep 3 06:59 functions -rw-------. 1 root root 318014 Sep 3 06:59 messages -rw-r--r--. 1 root root 1054 Sep 3 06:58 passwd
所有两组命令 tar -zcf,tar -zxf 或者 tar -Jcf,tar -Jxf 的是非常好用的,也是最常用的组合。
zip和unzip
一个可以在windows和Linux共用的压缩工具,方便在这两种操作系统之间压缩和解压缩文件,这里就简单的看一下:
[root@localhost tmp]# ll /tmp/test
total 360
-rw-r--r--. 1 root root 15131 Sep 5 21:41 functions
-rw-------. 1 root root 345807 Sep 5 21:41 messages
-rw-r--r--. 1 root root 1117 Sep 5 21:41 passwd
[root@localhost tmp]# zip -r test.zip test #选项-r实现递归压缩
adding: test/ (stored 0%)
adding: test/functions (deflated 69%)
adding: test/passwd (deflated 57%)
adding: test/messages (deflated 90%)
[root@localhost tmp]# unzip test.zip -d /root/ #选项-d可以指定解压缩的路径
Archive: test.zip
creating: /root/test/
inflating: /root/test/functions
inflating: /root/test/passwd
inflating: /root/test/messages
[root@localhost tmp]# ll /root/test
total 360
-rw-r--r--. 1 root root 15131 Sep 5 21:41 functions
-rw-------. 1 root root 345807 Sep 5 21:41 messages
-rw-r--r--. 1 root root 1117 Sep 5 21:41 passwd
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-09/146901.htm
