更新时间:2022-08-13 15:55:52
6.5:ZIP压缩工具:用来缩小文件大小:
1、压缩工具zip:(可以同时压缩文件和目录)-->压缩后会保留源文件:
可以使用yum install -y zip 来安装
windows和linux下的压缩文件可以通用:
语法如下
压缩文件:zip 压缩后名称 需压缩的file/dir
解压文件:unzip 压缩包名称
1.1:解压缩文件:
[root@localhost ~]# zip 1.txt.zip 1.txt #用zip来压缩文件:
adding: 1.txt (deflated 59%)
[root@localhost ~]# ls -l 1.txt 1.txt.zip
-rw-r--r-- 1 root root 964 11月 9 21:16 1.txt
-rw-r--r-- 1 root root 555 11月 9 22:42 1.txt.zip
3.2:解压缩目录:zip -r 压缩后目录名称 需压缩的目录
[root@localhost ~]# zip -r yuanhh.zip yuanhh
adding: yuanhh/ (stored 0%)
adding: yuanhh/2/ (stored 0%)
adding: yuanhh/4/ (stored 0%)
adding: yuanhh/3 (stored 0%)
adding: yuanhh/1.bak (stored 0%)
adding: yuanhh/1.txt.bz2 (stored 0%)
[root@localhost ~]# ls -ld yuan* #源文件来存在
drwxr-sr-x 4 root yuanhh 63 11月 9 19:24 yuanhh
-rw-r--r-- 1 root root 1360 11月 9 23:02 yuanhh.zip
1.2:所以解压缩文件时,会提示是否覆盖呢:会提示是否覆盖:
[root@localhost ~]# unzip yuanhh.zip
Archive: yuanhh.zip
replace yuanhh/3? [y]es, [n]o, [A]ll, [N]one, [r]ename:y
extracting: yuanhh/3
replace yuanhh/1.bak? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
extracting: yuanhh/1.bak
replace yuanhh/1.txt.bz2? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
extracting: yuanhh/1.txt.bz2
如果不想提示:可以使用unzip -o 压缩文件名
[root@localhost ~]# unzip -o yuanhh.zip #-o则自动覆盖,不会提示:
Archive: yuanhh.zip
extracting: yuanhh/3
extracting: yuanhh/1.bak
extracting: yuanhh/1.txt.bz2
1.4:unzip支持解压到某个目录:" -d "
[root@localhost ~]# unzip yuanhh.zip -d test #-d后跟要解压到的目录:
Archive: yuanhh.zip
extracting: test/yuanhh/3
extracting: test/yuanhh/1.bak
extracting: test/yuanhh/1.txt.bz2
[root@localhost ~]# ls test/ #确是解压到了此目录:
yuanhh
1.5:unzip只支持查看文件列表(不像gzip/bzip2那样可以查看文件内容)
1
2
3
4
5
6
7
|
[root@localhost ~] # unzip -l yuanhh.zip #查看文件列表内容
Archive: yuanhh.zip Length Date Time Name
--------- ---------- ----- ---- 0 11-09-2017 19:24 yuanhh/
0 10-27-2017 00:15 yuanhh /2/
0 10-27-2017 00:18 yuanhh /4/
|
6.6:tar打包工具:(打包或解包源文件都不会丢失)
1、tar命令可以把一堆文件或目录打包成一个文件,这对于备份文件和网络传输的方便快捷:
语法: tar options filename.tar filename
options 选项如下:
-c: 建立一个tar包或者打包一个压缩文件:(打包)
-v: 可视化,显示命令执行的过程:
-f: 后跟目标文件名,
-z: 打包的同时指定用gzip进行压缩:
-j: 打包的同时指定用bzip2进行压缩:
-J: 打包的同时指定用xz进行压缩:
-x: 从压缩包中还原文件(解包)
-t: 列出包内的文件内容(查看)
--exclude: 用于排除不打包的内容:
1.1:打包文件:tar -cvf 打包后名称 打包的文件
1
2
3
4
|
[root@localhost ~] # tar -cvf 5.txt.tar 5.txt #打包5.txt为5.txt.tar
5.txt [root@localhost ~] # ls -ld 5.txt.tar
-rw-r--r-- 1 root root 10240 11月 10 23:05 5.txt. tar
|
1.2:打包目录:tar -cvf 打包后名称 打包的目录
1
2
3
4
5
6
7
|
[root@localhost ~] # tar -cvf yuan.tar yuanhh #打包yuanhh目录为yuan.tar
yuanhh/ yuanhh /2/
yuanhh /1 .bak
yuanhh /1 .txt.bz2
[root@localhost ~] # ls -ld yuan.tar
-rw-r--r-- 1 root root 10240 11月 10 23:06 yuan. tar
|
1.3:打包文件和目录: tar -cvf 打包后名称 需要打包的目录或文件
1
2
3
4
5
6
7
|
[root@localhost ~] # tar -cvf 11.tar 1.txt test #打包
1.txt test /
test /yuanhh/1 .bak
test /yuanhh/1 .txt.bz2
[root@localhost ~] # ls -ld 11.tar
-rw-r--r-- 1 root root 10240 11月 10 23:09 11. tar
|
注意:当本次打包后,再次打包时会将旧文件覆盖,并且不会有任何提示:
1.4:支持查看tar包里的文件:“ -tf ”
1
2
3
4
5
|
[root@localhost ~] # tar -tf 11.tar #查看包里内容列表
1.txt test /
test /yuanhh/2/
test /yuanhh/4/
|
1.5:当然也可以解包:" -xvf " #解包时且文件存在自动覆盖,不会有任何提示:
1
2
3
4
5
6
|
[root@localhost ~] # tar -xvf 11.tar
1.txt test /
test /yuanhh/
test /yuanhh/1 .bak
test /yuanhh/1 .txt.bz2
|
1.6: tar命令还支持设置过滤文件或目录,表示不打包此文件或目录: “ --exclude ”
1
2
3
4
5
6
7
|
[root@localhost ~] # ls test #查看原有文件内容
123 1.txxt 2.txt yuanhh [root@localhost ~] # tar -cvf test.tar --exclude 2.txt test/ #发现下列2.txt未打包
test /
test /yuanhh/
test /123/
test /1 .txxt/
|
同时支持多个过滤条件,也支持通配符" * " 如下:
[root@localhost ~]# tar -cvf test.tar --exclude "*.txt" --exclude 123 test/
test/
test/yuanhh/
test/1.txxt/
test/test.tar
如上:使用通配符时需要加上双引号才可以:
6.7:tar打包并压缩:(解压缩源文件存在,默认覆盖)
tar支持打包的同时并压缩文件:可支持gzip、bzip2、xz等格式:
1、打包并压缩为gzip格式:如下:打包1.txt文件和目录test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@localhost ~] # tar -czvf 1.txt.gz 1.txt test #打包为gzip格式:
1.txt test /
test /yuanhh/
test /123/
test /1 .txxt/
test /2 .txt/
test /test . tar
[root@localhost ~] # tar tf 1.txt.gz #查看压缩的文件:
1.txt test /
test /yuanhh/
test /123/
test /1 .txxt/
test /2 .txt/
test /test . tar
[root@localhost ~] # tar -xzvf 1.txt.gz 1.txt test #对gzip格式的解包解压缩
|
2、打包并压缩为bzip2格式:如下:打包5.txt文件和目录test:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@localhost ~] # ls -ld 5.txt test #查看文件内容
-rw-r--r-- 1 root root 964 11月 9 23:23 5.txt drwxr-xr-x 6 root root 74 11月 10 23:26 test
[root@localhost ~] # tar -cjvf 5.txt.bz2 5.txt test #对文件5.txt和目录test进行打包压缩
[root@localhost ~] # tar -tf 5.txt.bz2 #查看打包压缩的文件
5.txt test /
test /yuanhh/
test /123/
test /1 .txxt/
test /2 .txt/
test /test . tar
[root@localhost ~] # tar -xjvf 5.txt.bz2 #对bzip2格式的包进行解压缩
|
3、打包并压缩为xz格式:如下:打包11.tsxt文件和目录test:
1
2
3
4
5
6
7
8
9
10
|
[root@localhost ~] # tar -cJvf 11.tar.xz 11.tsxt test #打包并压缩
[root@localhost ~] # tar -tf 11.tar.xz #查看
11.tsxt test /
test /yuanhh/
test /123/
test /1 .txxt/
test /2 .txt/
test /test . tar
[root@localhost ~] # tar -xJvf 11.tar.xz #对xz格式的包进行解包姐压缩
|
注意,压缩打包的同时,也支持对之前的压缩包基础上再次打包压缩:
1
2
3
4
5
6
|
[root@localhost ~] # tar -cJvf 11.tar.xz 11.tsxt 11.txt.bz2
11.tsxt 11.txt.bz2 [root@localhost ~] # tar -tf 11.tar.xz
11.tsxt 11.txt.bz2 |