常见压缩包类型

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

压缩解压命令—gzip

注意:

1
2
3
4
使用打包压缩命令,最好到需要被打包目录的上级目录,
解压时,-C最好不要指定/目录 可能会覆盖/下某些原文件
[root@localhost ~]# tar xf mysql.tgz -C /
/etc/profile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## 压缩文件
语法:gzip 文件名
[root@localhost ~]# gzip file1
[root@driver-zeng ~]# echo 22222|gzip > zls.txt.gz

## 解压 -d:解压文件

[root@localhost ~]# gzip -d file1.gz
## 查看文件类型
[root@localhost ~]# file file2.gz
file2.gz: gzip compressed data, was "file2", from Unix, last modified: Thu Apr
18 17:33:26 2024

## 可以直接查看压缩包中文件的内容
[root@localhost ~]# zcat file4.gz
xxx
## 特点:
1.压缩包后缀.gz
2.压缩后源文件不存在
3.压缩率很高
4.缺陷就是,不能压缩目录,不能压缩多个文件
5.解压后,压缩包不在了,源文件出来了
6.可以直接使用命令查看压缩包中文件的内容

压缩解压命令—zip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
## 安装
[root@localhost ~]# yum install -y zip

## 语法:
zip 包名 文件名...

## 压缩文件
[root@localhost ~]# zip file1.zip file1

## 压缩多个文件
[root@localhost ~]# zip file.zip file1 file10

## 查看文件类型
[root@localhost ~]# file file.zip
file.zip: Zip archive data, at least v1.0 to extract

## 压缩目录 -r 包含目录内容一起压缩
[root@localhost ~]# zip -r dir1.zip dir1/

## 指定路径压缩 压缩test下所有文件到tmp下,文件名是test.zip
[root@localhost ~]# zip -r /tmp/test.zip test/

## 解压
[root@localhost ~]# yum install -y unzip
[root@localhost opt]# unzip dir1.zip
[root@localhost tmp]# unzip /opt/dir1.zip 解压指定目录文件
## 查看包里有哪些文件或目录
[root@localhost local]# unzip -l /opt/dir1.zip
Archive: /opt/dir1.zip
Length Date Time Name
--------- ---------- ----- ----
0 04-18-2024 18:40 dir1/
26 04-18-2024 18:40 dir1/file1.gz
27 04-18-2024 18:40 dir1/file10.gz
26 04-18-2024 18:40 dir1/file2.gz
26 04-18-2024 18:40 dir1/file3.gz
26 04-18-2024 18:40 dir1/file4.gz
261 11 files
## 特点:
1.压缩包后缀.zip
2.压缩后源文件存在
3.压缩率很高
4.默认情况下不能压缩目录,能压缩多个文件,压缩目录需要选项
5.压缩时可以指定压缩包的路径
6.解压后,压缩包还在,源文件出来了
7.不可以直接使用命令查看压缩包中文件的内容

压缩解压命令—tar

选项 作用
c 创建归档文件
f 指定归档文件的包名
z 使用gzip来压缩归档后的文件,xf解压.tar.gz包
x 对归档文件进行解压
v 显示过程
t 查看压缩包中的所有文件和目录
X 可以排除多个不想打包的文件,指定文件名 (把不想打包的文件名写入一个文件,X指定这个文件就行,若排除的文件在嵌套目录,则把相对路径写上即可)
h 打包软链接的源文件
J 使用xz压缩归档后的文件,xf 解压.tar.xz包
-P 使用绝对路径打包
j 使用bzip2压缩归档后的文件,xf解压 .tar.bz2包
-C 解压到指定目录
—exclude 排除不想要的文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
## tar命令本身只是一个归档命令
## 语法:
tar [选项] 包名 文件...
[root@localhost ~]# file abc.tar
abc.tar: POSIX tar archive (GNU)
[root@localhost ~]# file nginx-1.22.1.tar.gz
nginx-1.22.1.tar.gz: gzip compressed data, from Unix, last modified: Wed Oct 19
16:02:28 2022
## 文件归档
[root@localhost ~]# tar cf dir1.tar dir1
## 文件归档并指定路径
[root@localhost ~]# tar cf /tmp/dir1_xxx.tar dir1
## 压缩目录
[root@localhost ~]# tar zcf dir1.tgz dir1/
## 压缩并显示过程
[root@localhost ~]# tar zcvf dir1.tgz dir1/
## 解压
[root@localhost ~]# tar zxf dir1.tgz
## 查看
[root@localhost ~]# tar tf nginx-1.22.1.tar.gz
## 解压到指定目录
[root@localhost ~]# tar zxf nginx-1.22.1.tar.gz -C /tmp/
## 打包时排除不想要的文件
[root@localhost tmp]# tar zcf root2.tgz --exclude=file6 --exclude=file7 /root
1.归档包后缀.tar
2.归档后源文件存在
3.没有压缩率
4.默认情况下能归档目录,能归档多个文件
5.归档时可以指定归档的路径
6.解压后,压缩包还在,源文件出来了
7.不可以直接使用命令查看压缩包中文件的内容
1
2
#移动海量小文件使用tar
[root@localhost abc]# tar czf - ./ | tar xzf - -C /tmp