2023-05-31    2024-01-16    999 字  2 分钟

Linux常用的压缩格式如下表:

格式 压缩工具
.zip zip压缩工具。
.gz gzip压缩工具,只能压缩文件,会删除原文件,通常配合tar使用。
.bz2 bzip2压缩工具,只能压缩文件,会删除原文件,通常配合tar使用。
.tar.gz 先使用tar命令归档打包,然后使用gzip压缩。
.tar.bz2 先使用tar命令归档打包,然后使用bzip压缩。

gzip

gzip是一种用于压缩文件的工具,通常用于压缩单个文件,压缩后的文件扩展名为.gz

1
[root@localhost ~]# yum install gzip

压缩

打包与压缩,仅对文件有效。如新建一个文件snoopy.txt,文件内容为Hello

1
[root@localhost ~]# echo "Hello" > snoopy.txt

把文件打包,源文件会消失。

1
2
3
4
[root@localhost ~]# gzip snoopy.txt 
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 33 Aug 13 20:38 snoopy.txt.gz

解压

可以通过zcat查看压缩文件内容。

1
2
[root@localhost ~]# zcat snoopy.txt.gz 
Hello

-d选项可为文件解压,解压后压缩包消失。

1
2
3
4
[root@localhost ~]# gzip -d snoopy.txt.gz
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 6 Aug 13 20:38 snoopy.txt

gzip常用于备份或恢复不在使用的文件的场景。

[root@localhost yum.repos.d]# gzip CentOS-Vault.repo 
[root@localhost yum.repos.d]# gzip -d CentOS-Vault.repo.gz

zip

zip工具可以同时压缩多个文件和目录,并将它们打包到一个zip文件中。

需要安装zip相关工具。

1
[root@localhost ~]# yum install zip unzip

压缩

压缩文件。

1
2
3
4
5
6
[root@localhost ~]# zip test.zip test.txt 
  adding: test.txt (deflated 56%)
[root@localhost ~]# ll
total 8
-rw-r--r--. 1 root root 979 Aug 13 10:20 test.txt
-rw-r--r--. 1 root root 599 Aug 13 10:22 test.zip

递归压缩,把dir目录和snoopy.txt文件都压缩到dir.zip

1
2
3
4
5
6
[root@localhost ~]# zip -r dir.zip dir/ snoopy.txt 
  adding: dir/ (stored 0%)
  adding: dir/snoopy.txt (stored 0%)
  adding: dir/test/ (stored 0%)
  adding: dir/hello.txt (deflated 56%)
  adding: snoopy.txt (stored 0%)

解压

查看包内容

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[root@localhost ~]# unzip -l dir.zip 
Archive:  dir.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  08-13-2019 20:58   dir/
        7  08-13-2019 20:57   dir/snoopy.txt
        0  08-13-2019 20:57   dir/test/
      979  08-13-2019 20:57   dir/hello.txt
        6  08-13-2019 20:38   snoopy.txt
---------                     -------
      992                     5 files

解压到当前目录

1
2
3
4
5
6
7
[root@localhost ~]# unzip dir.zip 
Archive:  dir.zip
   creating: dir/
 extracting: dir/snoopy.txt              
   creating: dir/test/
  inflating: dir/hello.txt           
 extracting: snoopy.txt     

解压到指定目录下

1
2
3
4
5
6
7
[root@localhost ~]# unzip dir.zip -d /opt/
Archive:  dir.zip
   creating: /opt/dir/
 extracting: /opt/dir/snoopy.txt         
   creating: /opt/dir/test/
  inflating: /opt/dir/hello.txt      
 extracting: /opt/snoopy.txt  

tar

压缩

1
2
3
4
# 以gzip的方式压缩文件
[root@localhost ~]# tar czf test.tar.gz dir dir.txt 
# 以bz2的方式压缩文件
[root@localhost ~]# tar cjf test.tar.bz2 test test2 dir.txt 

打包/tmp下所有文件。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[root@localhost ~]# find /var/log -type f | xargs tar czf log.tar.gz
tar: Removing leading `/' from member names
[root@localhost ~]# tar tf log.tar.gz 
var/log/tuned/tuned.log
var/log/audit/audit.log
var/log/cron
var/log/messages
var/log/yum.log
var/log/boot.log
var/log/wtmp

压缩时,排除文件。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 压缩/etc排除单个文件
[root@localhost ~]# tar czf etc.tar.gz /etc/ --exclude=etc/services
# 压缩/etc排除多个文件
[root@localhost ~]# tar czf etc.tar.gz /etc/ --exclude=etc/services --exclude=etc/shadow
# 将要排除的文件写入文本,通过X选项排除
[root@localhost ~]# cat hello.txt 
/etc/passwd
/etc/passwd-
/etc/locale.conf
/etc/hostname
/etc/.updated
/etc/aliases.db
/etc/vimrc
/etc/wgetrc
[root@localhost ~]# tar czXf hello.txt etc.tar.gz /etc/
tar: Removing leading `/' from member names

解压

查看压缩文件。

1
2
3
4
5
6
7
8
# 不论查看何种类型的压缩文件都是用tf选项
[root@localhost ~]# tar tf test.tar.gz 
dir/
dir.txt
[root@localhost ~]# tar tf test.tar.bz2 
test/
test2/
dir.txt

解压文件。

1
2
3
4
5
# 不论解压何种类型的文件,都使用xf选项
[root@localhost ~]# tar xf test.tar.gz 
[root@localhost ~]# tar xf test.tar.bz2
# -C可指定解压的目录 
[root@localhost ~]# tar xf test.tar.gz -C /opt

image-20231028232834657