文件的压缩打包
压缩包格式
windows:
.zip
.tar
.tar.gz
.gz
.rar
.7z
.bz
.bz2
.xz
为什么使用压缩
- 1.文件或目录太大,需要压缩传输
- 2.服务安装包都需要解压
Linux常用压缩格式及命令
| 格式 | Linux命令 |
|---|---|
| .zip | zip |
| .gz | gzip |
| .tar | tar |
| .tar.gz | tar、gzip |
压缩命令-gzip
# gzip命令的安装:(centOS7自带命令)
[root@localhost ~]# yum install -y gzip
# gzip命令用法:
gzip 普通文件名
# 选项
-r:递归压缩
(gzip递归压缩会自动对目录下个每个普通文件分别压缩,不可对上级目录进行压缩)
# 查看压缩包中文件的内容命令
zcat
# 解压命令
gzip -d 压缩包名
## 特性:
1.压缩文件后,源文件不存在
2.只能压缩文件,不能压缩目录
3.压缩后,压缩包的位置在源文件的目录下
4.压缩后可以直接查看文件内容zcat
5.一个压缩包中,只会有一个文件
6.解压后,压缩包没了,只剩源文件
# 示例:
[root@localhost~]# ll /cheshi003/toni/
total 20
-rw-r--r-- 1 root root 29 May 1 00:29 aaa1.txt.gz
-rw-r--r-- 1 root root 29 May 1 00:29 aaa2.txt.gz
-rw-r--r-- 1 root root 29 May 1 00:29 aaa3.txt.gz
-rw-r--r-- 1 root root 29 May 1 00:29 aaa4.txt.gz
-rw-r--r-- 1 root root 29 May 1 00:29 aaa5.txt.gz
[root@localhost~]# gzip -r /cheshi003/toni/
[root@localhost~]# ll /cheshi003/toni/
total 24
-rw-r--r-- 1 root root 33 May 1 00:57 aaa1.txt.gz
-rw-r--r-- 1 root root 33 May 1 00:57 aaa2.txt.gz
-rw-r--r-- 1 root root 33 May 1 00:58 aaa3.txt.gz
-rw-r--r-- 1 root root 33 May 1 00:58 aaa4.txt.gz
-rw-r--r-- 1 root root 33 May 1 00:59 aaa5.txt.gz
[root@localhost~]# file /cheshi003/toni/aaa1.txt.gz
/cheshi003/toni/aaa1.txt.gz: gzip compressed data, was "aaa1.txt", from Unix, last modified: Sun May 1 00:57:44 2022
[root@localhost~]# zcat /cheshi003/toni/aaa1.txt.gz
bbb
# 解压/递归解压:
1)
[root@localhost~]# gzip -d /cheshi003/toni/aaa1.txt.gz
[root@localhost~]# ll /cheshi003/toni/
total 24
-rw-r--r-- 1 root root 4 May 1 00:57 aaa1.txt
-rw-r--r-- 1 root root 33 May 1 00:57 aaa2.txt.gz
-rw-r--r-- 1 root root 33 May 1 00:58 aaa3.txt.gz
-rw-r--r-- 1 root root 33 May 1 00:58 aaa4.txt.gz
-rw-r--r-- 1 root root 33 May 1 00:59 aaa5.txt.gz
-rw-r--r-- 1 root root 33 May 1 00:56 aaaa.txt.gz
2)
[root@localhost~]# gzip -dr /cheshi003/toni/
[root@localhost~]# ll /cheshi003/toni/
total 24
-rw-r--r-- 1 root root 4 May 1 00:57 aaa1.txt
-rw-r--r-- 1 root root 4 May 1 00:57 aaa2.txt
-rw-r--r-- 1 root root 4 May 1 00:58 aaa3.txt
-rw-r--r-- 1 root root 4 May 1 00:58 aaa4.txt
-rw-r--r-- 1 root root 4 May 1 00:59 aaa5.txt
-rw-r--r-- 1 root root 4 May 1 00:56 aaaa.txt
压缩命令-zip
# zip和unzip命令:
[root@localhost ~]# yum install -y zip
[root@localhost ~]# yum install -y unzip
# zip压缩命令:
zip 压缩包名 文件或目录
压缩目录需要加选项 -r 如果不加,压缩后,只有一个空目录,没有里面的文件
命令: 压缩包名 需要放进压缩包的文件
[root@localhost ~]# zip txt.zip 1.txt 2.txt 3.txt
# 选项
-r:递归压缩,包括目录下的所有文件
[root@localhost~]# zip -r /ceshi002/ys.zip /ceshi001/ys
## 压缩并指定压缩包的存放位置:
[root@localhost ~]# zip /opt/zls.zip 1.txt 2.txt 3.txt 4.txt
## 特性:
1.压缩文件后,源文件存在
2.可以指定压缩后保存的路径
3.可以压缩目录,也可以压缩文件,也可以指定多个文件一起压缩
4.压缩目录需要加选项,如果不加,压缩后,只有一个空目录,没有里面的文件
5.解压后,压缩包不会消失,如果同一目录下出现同名文件则会询问是否要覆盖
### 示例:
[root@localhost~]# ll /ceshi001/ys
total 0
-rw-r--r-- 1 root root 0 Apr 30 21:50 file1
-rw-r--r-- 1 root root 0 Apr 30 21:50 file2
-rw-r--r-- 1 root root 0 Apr 30 21:50 file3
-rw-r--r-- 1 root root 0 Apr 30 21:50 file4
-rw-r--r-- 1 root root 0 Apr 30 21:50 file5
[root@localhost~]# zip -r /ceshi002/zzz.zip /ceshi001/ys
adding: ceshi001/ys/ (stored 0%)
adding: ceshi001/ys/file1 (stored 0%)
adding: ceshi001/ys/file2 (stored 0%)
adding: ceshi001/ys/file3 (stored 0%)
adding: ceshi001/ys/file4 (stored 0%)
adding: ceshi001/ys/file5 (stored 0%)
[root@localhost~]# ll /ceshi002
total 4
-rw-r--r-- 1 root root 984 May 1 02:36 zzz.zip
[root@localhost~]# file /ceshi002/zzz.zip
/ceshi002/zzz.zip: Zip archive data, at least v1.0 to extract
# unzip解压缩命令:
unzip 压缩包名
unzip在默认情况下,是解压缩文件到当前工作目录,如果当前目录中存在和压缩文件中同名的文件,将提示用户
[root@localhost~]# unzip /ceshi002/zzz.zip
Archive: /ceshi002/zzz.zip
replace ceshi001/ys/file1? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
extracting: ceshi001/ys/file1
extracting: ceshi001/ys/file2
extracting: ceshi001/ys/file3
extracting: ceshi001/ys/file4
extracting: ceshi001/ys/file5
## unzip -l:查看压缩包里面都有哪些文件
(无法看见压缩包里文件里的内容)
###示例:
[root@localhost~]# unzip -l /ceshi002/ys.zip
Archive: /ceshi002/ys.zip
Length Date Time Name
--------- ---------- ----- ----
0 04-30-2022 21:53 ceshi001/ys/
0 04-30-2022 21:50 ceshi001/ys/file1
0 04-30-2022 21:50 ceshi001/ys/file2
0 04-30-2022 21:50 ceshi001/ys/file3
0 04-30-2022 21:50 ceshi001/ys/file4
0 04-30-2022 21:50 ceshi001/ys/file5
--------- -------
0 6 files
## unzip -d:指定解压路径
[root@localhost~]# unzip /ceshi002/zzz.zip -d /ceshi002
### 示例:
[root@localhost~]# unzip /ceshi002/zzz.zip -d /ceshi002
Archive: /ceshi002/zzz.zip
creating: /ceshi002/ceshi001/ys/
extracting: /ceshi002/ceshi001/ys/file1
extracting: /ceshi002/ceshi001/ys/file2
extracting: /ceshi002/ceshi001/ys/file3
extracting: /ceshi002/ceshi001/ys/file4
extracting: /ceshi002/ceshi001/ys/file5
[root@localhost~]# ll /ceshi002/ceshi001/ys
total 0
-rw-r--r-- 1 root root 0 Apr 30 21:50 file1
-rw-r--r-- 1 root root 0 Apr 30 21:50 file2
-rw-r--r-- 1 root root 0 Apr 30 21:50 file3
-rw-r--r-- 1 root root 0 Apr 30 21:50 file4
-rw-r--r-- 1 root root 0 Apr 30 21:50 file5
### 过程演示:
[root@localhost~]# tree /ceshi001
/ceshi001
├── ys
│ ├── file1
│ ├── file2
│ ├── file3
│ ├── file4
│ └── file5
└── ys02
├── aaa.txt
└── abc.txt
2 directories, 7 files
[root@localhost~]# mkdir /ceshi002
[root@localhost~]# zip -r /ceshi002/zzz.zip /ceshi001/ys
adding: ceshi001/ys/ (stored 0%)
adding: ceshi001/ys/file1 (stored 0%)
adding: ceshi001/ys/file2 (stored 0%)
adding: ceshi001/ys/file3 (stored 0%)
adding: ceshi001/ys/file4 (stored 0%)
adding: ceshi001/ys/file5 (stored 0%)
[root@localhost~]# ll /ceshi002
total 4
-rw-r--r-- 1 root root 984 May 1 02:36 zzz.zip
[root@localhost~]# file /ceshi002/zzz.zip
/ceshi002/zzz.zip: Zip archive data, at least v1.0 to extract
[root@localhost~]# unzip -l /ceshi002/zzz.zip
Archive: /ceshi002/zzz.zip
Length Date Time Name
--------- ---------- ----- ----
0 04-30-2022 21:53 ceshi001/ys/
0 04-30-2022 21:50 ceshi001/ys/file1
0 04-30-2022 21:50 ceshi001/ys/file2
0 04-30-2022 21:50 ceshi001/ys/file3
0 04-30-2022 21:50 ceshi001/ys/file4
0 04-30-2022 21:50 ceshi001/ys/file5
--------- -------
0 6 files
[root@localhost~]# unzip /ceshi002/zzz.zip -d /ceshi002
Archive: /ceshi002/zzz.zip
creating: /ceshi002/ceshi001/ys/
extracting: /ceshi002/ceshi001/ys/file1
extracting: /ceshi002/ceshi001/ys/file2
extracting: /ceshi002/ceshi001/ys/file3
extracting: /ceshi002/ceshi001/ys/file4
extracting: /ceshi002/ceshi001/ys/file5
[root@localhost~]# ll /ceshi002/ceshi001/ys
total 0
-rw-r--r-- 1 root root 0 Apr 30 21:50 file1
-rw-r--r-- 1 root root 0 Apr 30 21:50 file2
-rw-r--r-- 1 root root 0 Apr 30 21:50 file3
-rw-r--r-- 1 root root 0 Apr 30 21:50 file4
-rw-r--r-- 1 root root 0 Apr 30 21:50 file5
压缩命令-tar
# tar命令本身是归档
## 选项:(正常使用不需要-、 特殊情况比如-C就会需要)
c:归档
f:指定包名
z:使用gzip把归档文件压缩(.tar.gz)
v:显示压缩/解压的过程
x:解压归档文件
C:指定解压的位置(路径)
t:查看压缩包里的文件都有哪些(要配合f来指定查看的压缩包)
j:使用bzip2压缩文件
J:压缩成.xz包
X:排除指定的文件
--exclude:排除指定文件
## 特性
1.压缩文件后,源文件存在
2.目录和文件都可以压缩
3.压缩后,压缩包的位置可以指定任意目录
[root@localhost ~]# tar zcf /usr/local/lx.tar.gz /etc /opt /tmp
4.可以查看压缩包里有哪些文件,但是查看不了文件内容
[root@localhost ~]# tar tf /usr/local/lx.tar.gz
5.一个压缩包中,可以有多个文件或目录
6.解压后,压缩包还在,源文件也可以随意指定路径 -C
7.使用zcf压缩,zxf解压
使用jcf压缩,jxf解压
使用Jcf压缩,Jxf解压
万能解压命令:xf
## 注意:
1.tar命令在解压开文件时,如果有文件名冲突,则不会询问,直接覆盖
2.tar命令,在打包时,会自动删除绝对路径的"/"
3.以后打包,尽量使用相对路径,cd到需要打包目录或文件的上级目录
[root@localhost ~]# cd /
[root@localhost /]# tar zcf /usr/local/src/opt.tgz opt/
## zcf举例:
[root@localhost~]# tree /ceshi003
/ceshi003
├── AA1
│ ├── a1.txt
│ ├── a2.txt
│ ├── a3.txt
│ ├── a4.txt
│ └── a5.txt
└── AA2
[root@localhost~]# tar zcf /ceshi004/ttt.tar.gz /ceshi003/AA1
tar: Removing leading `/\' from member names
# 这里是警告在打包的过程中 tar命令自动删除绝对路径的"/"
所以:
[root@localhost~]# cd /
[root@localhost/]# tar zcf /ceshi004/ttt.tar.gz ceshi003/AA1
[root@localhost/]# tar tf /ceshi004/ttt.tar.gz
ceshi003/AA1/
ceshi003/AA1/a1.txt
ceshi003/AA1/a2.txt
ceshi003/AA1/a3.txt
ceshi003/AA1/a4.txt
ceshi003/AA1/a5.txt
相当于是两个步骤:
1)先归档
[root@localhost/]# tar cf /ceshi004/ttt.tar ceshi003/AA1
2)再使用gzip压缩
[root@localhost/]# gzip /ceshi004/ttt.tar
## zxf举例:
## C举例:这个时候C需要加上-即:-C
[root@localhost/]# tar zxf /ceshi004/ttt.tar.gz -C /ceshi004/B1/
[root@localhost/]# ll /ceshi004/B1/ceshi003/AA1
total 0
-rw-r--r-- 1 root root 0 May 1 03:22 a1.txt
-rw-r--r-- 1 root root 0 May 1 03:22 a2.txt
-rw-r--r-- 1 root root 0 May 1 03:22 a3.txt
-rw-r--r-- 1 root root 0 May 1 03:22 a4.txt
-rw-r--r-- 1 root root 0 May 1 03:22 a5.txt
## 万能解压命令:xf
[root@localhost/]# tar xf /ceshi004/ttt333.tar.gz -C /ceshi004/B2
[root@localhost/]# ll /ceshi004/B2/ceshi003/AA1
total 0
-rw-r--r-- 1 root root 0 May 1 03:22 a3.txt
-rw-r--r-- 1 root root 0 May 1 03:22 a4.txt
-rw-r--r-- 1 root root 0 May 1 03:22 a5.txt
## X举例:
[root@localhost~]# tree /ceshi003
/ceshi003
├── AA1
│ ├── a1.txt
│ ├── a2.txt
│ ├── a3.txt
│ ├── a4.txt
│ └── a5.txt
└── AA2
[root@localhost/]# cat /ceshi001/paicu.txt
a1.txt
a2.txt
[root@localhost/]# tar zcf /ceshi004/ttt222.tar.gz -X /ceshi001/paicu.txt ceshi003/AA1
[root@localhost/]# tar tf /ceshi004/ttt222.tar.gz
ceshi003/AA1/
ceshi003/AA1/a3.txt
ceshi003/AA1/a4.txt
ceshi003/AA1/a5.txt
## --exclude举例:
[root@localhost/]# tar zcf /ceshi004/ttt333.tar.gz --exclude=a1.txt --exclude=a2.txt ceshi003/AA1
[root@localhost/]# tar tf /ceshi004/ttt333.tar.gz
ceshi003/AA1/
ceshi003/AA1/a3.txt
ceshi003/AA1/a4.txt
ceshi003/AA1/a5.txt
思维导图
