Linux-压缩与归档

时间:2025-05-03 12:33:44

压缩:gzip/gunzip、bzip2/bunzip2、xz/unxz

归档:tar

归档+压缩:zip


1. gzip, gunzip, zcat - compress or expand files

gzip:压缩(压缩后会删除原文件)

gunzip:解压缩(解压缩后会删除原压缩文件)

zcat:直接查看压缩后的文本文件内容(建议不要查看大文件)

说明:

gzip、gunzip均可以同时操作多个文件

gzip、gunzip压缩/解压缩文件支持通配符

gzip、gunzip不能操作目录。

SYNOPSIS

gzip [OPTIONS] <file ...>

OPTIONS


-d --decompress --uncompress
Decompress(解压缩).
gzip -d <file> 相当于gunzip <file> -# --fast --best
指定压缩比(默认压缩比为6,建议无特殊需求不要改变默认压缩比) -c --stdout --to-stdout
将压缩结果输出至标准输出
gzip <file> 相当于 gzip -c <file> > <file.gz>(输出重定向)

EXAMPLES

[root@VM_0_171_centos tmp]# ls -lh
总用量 16K
-rw-r--r-- 1 root root 13K 4月 4 23:43 sentinel.log
[root@VM_0_171_centos tmp]# gzip sentinel.log
[root@VM_0_171_centos tmp]# ls -lh
总用量 4.0K
-rw-r--r-- 1 root root 1.4K 4月 4 23:43 sentinel.log.gz
[root@VM_0_171_centos tmp]# gzip -d sentinel.log.gz
[root@VM_0_171_centos tmp]# ls -lh
总用量 16K
-rw-r--r-- 1 root root 13K 4月 4 23:43 sentinel.log
[root@VM_0_171_centos tmp]#

2. bzip2/bunzip2/bzcat

bzip2:压缩(压缩后会删除原文件)

bunzip2:解压缩(解压缩后会删除原压缩文件)

bzcat:直接查看压缩后的文本文件内容(建议不要查看大文件)

SYNOPSIS

bzip2 [OPTIONS] <file ...>

** OPTIONS**

-d:解压缩
-#:指定压缩比;默认是6;数字越大压缩比越大(1-9);
-k:keep,保留原文件;

** EXAMPLES**

[root@VM_0_171_centos tmp]# ls -lh
总用量 16K
-rw-r--r-- 1 root root 13K 4月 4 23:43 sentinel.log
[root@VM_0_171_centos tmp]# bzip2 sentinel.log
[root@VM_0_171_centos tmp]# ls -lh
总用量 4.0K
-rw-r--r-- 1 root root 1.5K 4月 4 23:43 sentinel.log.bz2
[root@VM_0_171_centos tmp]# bzip2 -d sentinel.log.bz2
[root@VM_0_171_centos tmp]# ls -lh
总用量 16K
-rw-r--r-- 1 root root 13K 4月 4 23:43 sentinel.log
[root@VM_0_171_centos tmp]# bzip2 -k sentinel.log
[root@VM_0_171_centos tmp]# ls -lh
总用量 20K
-rw-r--r-- 1 root root 13K 4月 4 23:43 sentinel.log
-rw-r--r-- 1 root root 1.5K 4月 4 23:43 sentinel.log.bz2
[root@VM_0_171_centos tmp]#

3. xz/unxz/xzcat

用法与bzip2基本相同,但这种格式不是很常见

SYNOPSIS

xz [option]...  [file]...

** OPTIONS**

-d:解压缩
-#:指定压缩比;默认是6;数字越大压缩比越大(1-9);
-k:keep,保留原文件;

** EXAMPLES**

[root@VM_0_171_centos tmp]# ls -lh
总用量 16K
-rw-r--r-- 1 root root 13K 4月 4 23:43 sentinel.log
[root@VM_0_171_centos tmp]# xz sentinel.log
[root@VM_0_171_centos tmp]# ls -lh
总用量 4.0K
-rw-r--r-- 1 root root 1.3K 4月 4 23:43 sentinel.log.xz
[root@VM_0_171_centos tmp]# xz -d sentinel.log.xz
[root@VM_0_171_centos tmp]# ls -lh
总用量 16K
-rw-r--r-- 1 root root 13K 4月 4 23:43 sentinel.log
[root@VM_0_171_centos tmp]# xz -k sentinel.log
[root@VM_0_171_centos tmp]# ls -lh
总用量 20K
-rw-r--r-- 1 root root 13K 4月 4 23:43 sentinel.log
-rw-r--r-- 1 root root 1.3K 4月 4 23:43 sentinel.log.xz
[root@VM_0_171_centos tmp]#

4. tar - manual page for tar 1.26

归档文件或目录

SYNOPSIS

tar [OPTION...] [FILE]...

OPTIONS

#创建归档(v选项用于显示执行过程)
tar -cvf /PATH/TO/file.tar <file ...>
#创建归档并压缩成.gz格式
tar -zcvf /PATH/TO/file.tar <file ...>
#创建归档并压缩成.bz2格式
tar -jcvf /PATH/TO/file.tar <file ...> #展开归档
tar -xvf <file ...> [-C <展开至目标目录>]
#解压并展开归档(.gz)
tar -zxvf <file ...> [-C <展开至目标目录>]
#解压并展开归档(.bz2)
tar -jxvf <file ...> [-C <展开至目标目录>] #查看归档文件的文件列表
tar -tf <file>

EXAMPLES

下载nginx-1.10.3.tar.gz,解压,再压缩成.bz2格式

[root@VM_0_171_centos tmp]# ls
sentinel.log sentinel.log.xz
[root@VM_0_171_centos tmp]# wget http://nginx.org/download/nginx-1.10.3.tar.gz
--2017-04-11 22:44:56-- http://nginx.org/download/nginx-1.10.3.tar.gz
正在解析主机 nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ...
正在连接 nginx.org (nginx.org)|206.251.255.63|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:911509 (890K) [application/octet-stream]
正在保存至: “nginx-1.10.3.tar.gz” 100%[=========================================================>] 911,509 445KB/s 用时 2.0s 2017-04-11 22:44:59 (445 KB/s) - 已保存 “nginx-1.10.3.tar.gz” [911509/911509]) [root@VM_0_171_centos tmp]# ls
nginx-1.10.3.tar.gz sentinel.log sentinel.log.xz
[root@VM_0_171_centos tmp]# tar -jcf nginx-1.10.3.tar.bz2 nginx-1.10.3
[root@VM_0_171_centos tmp]# ls
nginx-1.10.3 nginx-1.10.3.tar.bz2 nginx-1.10.3.tar.gz sentinel.log sentinel.log.xz
[root@VM_0_171_centos tmp]#

5.zip - package and compress (archive) files

最通用的压缩、打包工具

用法

zip file.zip <file ...>
unzip -d <解压目录> <file>

EXAMPLES

[root@VM_0_171_centos tmp]# ls
nginx-1.10.3 sentinel.log sentinel.log.xz
[root@VM_0_171_centos tmp]# zip nginx-1.10.3.zip nginx-1.10.3/
adding: nginx-1.10.3/ (stored 0%)
[root@VM_0_171_centos tmp]# ls
nginx-1.10.3 nginx-1.10.3.zip sentinel.log sentinel.log.xz
[root@VM_0_171_centos tmp]# zip test.zip nginx-1.10.3 sentinel.log
adding: nginx-1.10.3/ (stored 0%)
adding: sentinel.log (deflated 89%)
[root@VM_0_171_centos tmp]# ls
nginx-1.10.3 nginx-1.10.3.zip sentinel.log sentinel.log.xz test.zip
[root@VM_0_171_centos tmp]# unzip -d ./test/ test.zip
Archive: test.zip
creating: ./test/nginx-1.10.3/
inflating: ./test/sentinel.log
[root@VM_0_171_centos tmp]# ls
nginx-1.10.3 nginx-1.10.3.zip sentinel.log sentinel.log.xz test test.zip
[root@VM_0_171_centos tmp]#