压缩与解压缩命令
2.8 压缩与解压缩命令
Linux下常见的压缩文件类型主要有.gz、.bz2、.xz、.zip,以及通过tar打包结合压缩生成的.tar.gz、.tar.bz2、.tar.xz等。其中.tar本身只是归档打包,并不压缩。
gzip 命令
命令格式:gzip [选项] 文件
gzip 命令用于压缩或解压文件(压缩文件后缀为.gz )
常用选项如下:
- -d:将压缩文件解压(decompress)
# 创建 test.txt 文件
[root@localhost ~]# echo "hello" >> test.txt
# 压缩文件
[root@localhost ~]# gzip test.txt
# 显示文件(原文件将被删除)
[root@localhost ~]# ll
-rw-r--r--. 1 root root 35 Oct 28 22:21 test.txt.gz
# 解压文件
[root@localhost ~]# gzip -d test.txt.gz
# 或
[root@localhost ~]# gunzip test.txt.gz
# 显示文件(压缩文件将被删除)
[root@localhost ~]# ll
-rw-r--r--. 1 root root 6 Oct 28 22:21 test.txt
bzip2 命令
命令格式:bzip2 [选项] 文件
bzip2 命令用于压缩或解压文件(压缩文件后缀为.bz2)
常用选项如下:
-
-k:产生压缩文件后保留原文件
-
-d:将压缩文件解压(decompress)
# 压缩文件
[root@localhost ~]# bzip2 -k test.txt
# 显示文件(原文件被保留)
[root@localhost ~]# ll
-rw-r--r--. 1 root root 6 Oct 28 22:21 test.txt
-rw-r--r--. 1 root root 42 Oct 28 22:21 test.txt.bz2
# 删除原文件
[root@localhost ~]# rm -rf test.txt
# 解压文件
[root@localhost ~]# bzip2 -d test.txt.bz2
# 或
[root@localhost ~]# bunzip2 test.txt.bz2
# 显示文件
[root@localhost ~]# ll
-rw-r--r--. 1 root root 6 Oct 28 22:21 test.txt
gzip和 bzip2 都只能压缩文件,不能压缩目录;bzip2 通常比 gzip 具有更高的压缩比,但高压缩比有代价,就是 bzip2 压缩速度明显慢于 gzip,CPU占用也更高
zip 命令
命令格式:zip [选项] 压缩后文件名 文件或目录
zip 命令用于压缩(解压)文件(压缩文件后缀为.zip)
****常用选项如下:
-
-r:压缩目录
-
-d:解压到指定目录
# 压缩文件
[root@localhost ~]# zip test.zip test.txt
adding: test.txt (stored 0%)
# 显示文件(原文件被保留)
[root@localhost ~]# ll
-rw-r--r--. 1 root root 6 Oct 28 22:21 test.txt
-rw-r--r--. 1 root root 172 Oct 28 22:35 test.zip
# 删除原文件
[root@localhost ~]# rm -rf test.txt
# 解压文件
[root@localhost ~]# unzip test.zip
# 显示文件
[root@localhost ~]# ll
-rw-r--r--. 1 root root 6 Oct 28 22:21 test.txt
-rw-r--r--. 1 root root 172 Oct 28 22:35 test.zip
# 创建目录
[root@localhost ~]# mkdir mydir
# 压缩目录
[root@localhost ~]# zip -r mydir.zip mydir/
adding: mydir/ (stored 0%)
# 显示目录
[root@localhost ~]# ll
drwxr-xr-x. 2 root root 6 Oct 28 22:36 mydir
-rw-r--r--. 1 root root 162 Oct 28 22:36 mydir.zip
# 解压目录到指定目录
[root@localhost ~]# unzip mydir.zip -d ./newdir
Archive: mydir.zip
creating: ./newdir/mydir/
# 显示目录
[root@localhost ~]# ll
drwxr-xr-x. 2 root root 6 Oct 28 22:36 mydir
-rw-r--r--. 1 root root 162 Oct 28 22:36 mydir.zip
drwxr-xr-x. 3 root root 19 Oct 28 22:41 newdir
zip -r 可以压缩目录
tar 命令(核心)
命令格式:tar [选项] 压缩后文件名 文件或目录
tar 命令用于文件、目录的打包或解包(不压缩)
常用选项如下:
-
-c : 建立一个压缩文件的参数指令(create),后缀是.tar
-
-x : 解开一个压缩文件的参数指令(extract)
-
-z: 以 gzip 命令压缩/解压缩
-
-j :以 bzip2 命令压缩/解压缩
-
-v :压缩的过程中显示文件(verbose)
-
-f : file 指定文件名,必选项
# 只打包文件
[root@localhost ~]# tar -cvf test.tar test.txt
test.txt
# 显示文件
[root@localhost ~]# ll
-rw-r--r--. 1 root root 10240 Oct 28 22:43 test.tar
-rw-r--r--. 1 root root 6 Oct 28 22:21 test.txt
# 打包并使用 gzip 压缩
[root@localhost ~]# tar -zcvf test.tar.gz test.txt
test.txt
# 显示文件
[root@localhost ~]# ll
-rw-r--r--. 1 root root 125 Oct 28 22:45 test.tar.gz
-rw-r--r--. 1 root root 6 Oct 28 22:21 test.txt
# 解压 .tar.gz 文件
[root@localhost ~]# tar -zxvf test.tar.gz
test.txt
# 解压 .tar.gz 文件 到指定目录
[root@localhost ~]# tar -zxvf test.tar.gz -C /tmp
test.txt
# 显示文件
[root@localhost ~]# ll /tmp/
-rw-r--r--. 1 root root 6 Oct 28 22:21 test.txt
# 打包并使用 bzip2 压缩
[root@localhost ~]# tar -jcvf test.tar.bz2 test.txt
test.txt
# 显示文件
[root@localhost ~]# ll
-rw-r--r--. 1 root root 127 Oct 28 22:46 test.tar.bz2
# 解压 .tar.bz2 文件
[root@localhost ~]# tar -jxvf test.tar.bz2
test.txt
tar 最常用的命令组合:tar –zcvf 压缩文件、tar –zxvf 解压文件
注意:建议打包前先cd到目标目录,是为了让归档包内只保留相对路径,解压时就不会还原出一长串绝对路径目录结构,-C参数后跟绝对路径,也可以实现保留相对路径打包,比如tar -zcvf /root/webapp_v1.tar.gz -C /root app.txt
打包压缩多个文件时直接在后面跟空格加文件即可
实例;tar -czvf /data/deliverable_$(date +%F).tar.gz docs src
这表示把docs和src都打包到deliverable_$(date +%F).tar.gz里面

浙公网安备 33010602011771号