压缩与打包(上)

简介:

    在windows系统中,我们会使用一些压缩软件去压缩文件,以达到节省磁盘空间的目的。在linux系统中,也有这种压缩和打包的工具。


1、gzip

命令 作用
gzip 1.txt 压缩

gzip -d 1.txt.gz

gunzip 1.txt.gz

解压缩
gzip -n 1.txt 压缩等级,n范围为1-9,默认6
zcat 1.txt.gz 不解压的情况下查看文件内容
gzip -c 1.txt>/root/1.txt.gz 压缩时保留原文件
gunzip -c /root/1.txt.gz>/tmp/1.txt 解压时保留原压缩文件

测试示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@server02 ~] # ls -lh 1.txt
-rw-r--r--. 1 root root 21M 6月  20 17:23 1.txt
[root@server02 ~] # gzip 1.txt
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root 5.8M 6月  20 17:23 1.txt.gz
[root@server02 ~] # gzip -d 1.txt.gz
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root 21M 6月  20 17:23 1.txt
[root@server02 ~] # gzip -c 1.txt > /root/1.txt.gz
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root  21M 6月  20 17:23 1.txt
-rw-r--r--. 1 root root 5.8M 6月  20 17:35 1.txt.gz
[root@server02 ~] # rm -rf 1.txt
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root 5.8M 6月  20 17:35 1.txt.gz
[root@server02 ~] # gunzip -c /root/1.txt.gz >/root/1.txt
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root  21M 6月  20 17:37 1.txt
-rw-r--r--. 1 root root 5.8M 6月  20 17:35 1.txt.gz


2、bzip2

命令 作用

bzip2 1.txt

bzip2 -z 1.txt

压缩

bzip2 -d 1.txt.bz2

bunzip2 1.txt.bz2

解压
bzip2 -n 1.txt 压缩等级,n范围为1-9,默认9
bzcat 1.txt.bz2 不解压的情况下查看文件内容
bzip2 -c 1.txt>/root/1.txt.bz2 压缩时保留原文件
bzip2 -c -d /root/1.txt.bz2>/tmp/1.txt.new2 解压时保留原压缩文件

测试示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@server02 ~] # bzip2 1.txt
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root 4.2M 6月  20 17:37 1.txt.bz2
-rw-r--r--. 1 root root 5.8M 6月  20 17:35 1.txt.gz
[root@server02 ~] # bunzip2 1.txt.bz2
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root  21M 6月  20 17:37 1.txt
-rw-r--r--. 1 root root 5.8M 6月  20 17:35 1.txt.gz
[root@server02 ~] # bzip2 -c 1.txt>/root/1.txt.bz2
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root  21M 6月  20 17:37 1.txt
-rw-r--r--. 1 root root 4.2M 6月  20 17:55 1.txt.bz2
-rw-r--r--. 1 root root 5.8M 6月  20 17:35 1.txt.gz
[root@server02 ~] # rm -rf 1.txt
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root 4.2M 6月  20 17:55 1.txt.bz2
-rw-r--r--. 1 root root 5.8M 6月  20 17:35 1.txt.gz
[root@server02 ~] # bzip2 -c -d /root/1.txt.bz2>/root/1.txt
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root  21M 6月  20 17:56 1.txt
-rw-r--r--. 1 root root 4.2M 6月  20 17:55 1.txt.bz2
-rw-r--r--. 1 root root 5.8M 6月  20 17:35 1.txt.gz


3、xz

命令 作用

xz 1.txt

xz -z 1.txt

压缩

xz -d 1.txt.xz

unxz 1.txt.xz

解压缩
xz -n 1.txt 压缩等级,n范围为1-9,默认9
xzcat 1.txt.xz 不解压的情况下查看文件内容
xz -c 1.txt>/root/1.txt.xz 压缩时保留原文件
xz -d -c /root/1.txt.xz>1.txt.new 解压时保留原压缩文件

测试示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@server02 ~] # xz -z 1.txt
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root 4.2M 6月  20 17:55 1.txt.bz2
-rw-r--r--. 1 root root 5.8M 6月  20 17:35 1.txt.gz
-rw-r--r--. 1 root root 681K 6月  20 17:56 1.txt.xz
[root@server02 ~] # unxz 1.txt.xz
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root  21M 6月  20 17:56 1.txt
-rw-r--r--. 1 root root 4.2M 6月  20 17:55 1.txt.bz2
-rw-r--r--. 1 root root 5.8M 6月  20 17:35 1.txt.gz
[root@server02 ~] # xz -c 1.txt>/root/1.txt.xz
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root  21M 6月  20 17:56 1.txt
-rw-r--r--. 1 root root 4.2M 6月  20 17:55 1.txt.bz2
-rw-r--r--. 1 root root 5.8M 6月  20 17:35 1.txt.gz
-rw-r--r--. 1 root root 681K 6月  20 17:59 1.txt.xz
[root@server02 ~] # xz -d -c /root/1.txt.xz>1.txt.new
[root@server02 ~] # ls -lh 1.txt*
-rw-r--r--. 1 root root  21M 6月  20 17:56 1.txt
-rw-r--r--. 1 root root 4.2M 6月  20 17:55 1.txt.bz2
-rw-r--r--. 1 root root 5.8M 6月  20 17:35 1.txt.gz
-rw-r--r--. 1 root root  21M 6月  20 17:59 1.txt.new
-rw-r--r--. 1 root root 681K 6月  20 17:59 1.txt.xz

 

总结:gzip、bzip2和xz都可以压缩文件,但都不能压缩目录。压缩能力是xz > bzip2 > gzip。但压缩能力越强,相应的耗费的系统处理资源也就越多。











本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1940851,如需转载请自行联系原作者


相关文章
|
29天前
|
Linux
linux命令行打包、压缩及解压缩
linux命令行打包、压缩及解压缩
8 0
|
计算机视觉