day20: zip压缩工具及打包工具介绍

本文详细介绍了zip和tar这两种常用的文件压缩与打包工具的使用方法,包括压缩、解压缩、查看文件列表等基本操作,以及如何指定压缩目录、过滤特定文件等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、zip:压缩工具:

相比与之前的三种工具,可用于压缩目录,在windows里和linux中比较常用:

解压缩使用格式:(压缩目录时要加“ -r ”

zip        filename.zip          filename         #压缩文件,压缩的文件名称要写在前面,可自定义:

unzip     filename.zip                               #解压文件,当目标文件存在,会提示是否覆盖:

zip      -r     dir.zip       dir      filename     #压缩目录,也可以同时并压缩文件:

[root@localhost d6z]# zip 1.txt.zip 1.txt              #用zip格式压缩文件1.txt:
  adding: 1.txt (deflated 73%)
[root@localhost d6z]# du -sh 1.txt.zip                 #查看其文件大小:
976K    1.txt.zip
[root@localhost d6z]# unzip 1.txt.zip                  #用unzip解压文件:
Archive:  1.txt.zip
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A   #当目标文件存在时,则会提示是否覆盖:
#yes:当压缩文件里存在多个文件时,每个文件都会询问,是否要覆盖?:
#no :当压缩文件里存在多个文件时,每个文件会询问是否不覆盖?:
#all: 当压缩文件里存在多个文件时,只询问一次,直接覆盖:
#None:直接退出,不进行当次操作:
#rename: 等于要重新新建一个文件,也是每个文件都会询问:
  inflating: 1.txt
[root@localhost d6z]# zip -r test.zip test 1.txt 2.txt 3.txt 4.txt    #压缩目录test和压缩文件1txt.2txt..
  adding: test/ (stored 0%)
  adding: test/2.txt (deflated 73%)
  adding: 1.txt (stored 0%)
  adding: 2.txt (deflated 73%)
  adding: 3.txt (deflated 73%)
  adding: 4.txt (deflated 73%)
[root@localhost d6z]# du -sh test.zip                    #查看其压缩后文件大小: 
3.9M    test.zip
[root@localhost d6z]# ls                                 #发现压缩或解压缩后源文件都不会删除:
1.txt  1.txt.zip  2.txt  3.txt  4.txt  5.txt  test  test.zip

如上图所示:压缩目录是要加“ -r ”选项:

压缩或解压缩源文件都不会删除

2、zip压缩也支持压缩到指定目录:则需要加“ -d ”选项:

unzip    filename.zip       -d     /test/              #压缩到指定目录:   -d 后跟目录

[root@localhost d6z]# unzip 1.txt.zip  -d test      #把1.txt.zip压缩到test目录下:
Archive:  1.txt.zip
  inflating: test/1.txt 
[root@localhost d6z]# ls test                       #查看其文件:
1.txt  2.txt

另外zip压缩到指定目录时,不支持自定义名称,也就是说压缩前后的文件名称必须一致

[root@localhost d6z]# unzip 1.txt.zip -d test/3.txt     #解压1.txt.zip到指定目录,并自定义名称,不允许的:
Archive:  1.txt.zip  
  inflating: test/3.txt/1.txt
[root@localhost d6z]# tree test                        #查看时发现自动的变成了目录,并在目录下生成了文件:
test
└── 3.txt
    └── 1.txt

1 directory, 1 file

如上图所示:发现指定的文件名变成了目录,并把文件解压到了目录下:不可以的:

3、zip不支持查看文件内容:但支持查看文件列表:用“  -l  ” 选项:

[root@localhost d6z]# zip -r test.zip test 1.txt 2.txt 3.txt     #压缩目录和文件:
  adding: test/ (stored 0
  adding: test/3.txt/ (stored 0%)
  adding: test/3.txt/1.txt (deflated 73%)
  adding: 1.txt (deflated 73%)
  adding: 2.txt (deflated 73%)
  adding: 3.txt (deflated 73%)
[root@localhost d6z]# unzip -l test.zip                        #查看压缩的文件列表:6个文件:
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  06-28-2018 01:01   test/
        0  06-28-2018 01:01   test/3.txt/
  3699067  06-28-2018 00:57   test/3.txt/1.txt
  3699067  06-28-2018 00:57   1.txt
  3699067  06-28-2018 00:41   2.txt
  3699067  06-28-2018 00:41   3.txt
---------                     -------
 14796268                     6 files

2:tar打包工具介绍:

tar打包:把目录打包成一个文件,把所有文件打包成一个大文件:

方便复制和移动:

节约带宽,方便传输:

tar打包格式:( -f后面要跟打包后的名称 )

tar        -cfv        dir.tar    dir                                 #对目录打包

tar       -cvf        dir.tar     dir     filename             #支持同时对目录及文件一起打包

tar      -xvf        dir.tar                                          #对目标进行解包操作

[root@localhost d6z]# tar -cvf test.tar test                     #tar命令对目录test进行打包:
test/
test/3.txt/
test/3.txt/1.txt
[root@localhost d6z]# ls                                  #查看时发现打包后不删除源文件:
1.txt      2.txt  4.txt  test      test.zip
1.txt.zip  3.txt  5.txt  test.tar
[root@localhost d6z]# tar -xvf test.tar                  #tar解包test.tar:
test/
test/3.txt/
test/3.txt/1.txt
[root@localhost d6z]# tar -cvf test.tar test 1.txt 2.txt 3.txt   #tar同时对目录及多个文件打包:
test/
test/3.txt/
test/3.txt/1.txt
1.txt
2.txt
3.txt
[root@localhost d6z]# tar -cf test.tar test 1.txt 2.txt 3.txt   #不加” -v “选项,则不查看执行过程:并且再次打包时会覆盖,不会任何提示:

如上得知:

不加 " -v " 选项,则表示不可视化:不查看执行过程:

打包或解包时,都不会删除源文件:

打包或解包时会覆盖,不会有任何提示:

2.1:tar命令也只支持查看文件列表(同zip相同),不能查看文件内容

tar     -tf           filename.tar                   #查看文件列表

[root@localhost d6z]# tar -tf test.tar              #用tar查看文件列表:
test/   
test/3.txt/
test/3.txt/1.txt
1.txt
2.txt
3.txt

2.2:tar还支持文件过滤(不打包它):      --exelude

tar        -cvf       dir.tar    --exclude       1.txt     dir     2.txt     3.txt     4.txt      

#表示打包dir目录和文件,并过滤到dir目录下的1.txt文件

[root@localhost d6z]# tar -cvf test.tar  --exclude 1.txt  test 2.txt 4.txt    #过滤掉1.txt文件:
test/
test/11.txt
test/100.txt
2.txt
4.txt
[root@localhost d6z]# tar -tf test.tar      #查看到过滤了test目录下的1.txt文件:
test/
test/11.txt
test/100.txt
2.txt
4.txt

如图所示:对当前目录test和文件2.txt   4.txt进行压缩,并过滤掉test下的1.txt:

当然:exelude也支持通配符,也支持过滤过个选项:

tar -cvf test.tar  --exclude 1.txt --exclde 2.txt  test    3.txt      4.txt

2.3:打包同时并压缩文件:

格式: tar     [ -zjJxcvfpP   ]      filename.tar         filename   dir  

常用选项如下:

-c :  表示建立一个tar包或压缩包:

-x :  表示解包或解压缩 :

-v :  表示可视化,可查看其执行过程:

-f :  后面要跟文件名(解压缩都一样的):

-t :  表示查看tar包里的文件列表:

--exclude :  表示过滤文件时使用:(打包或解压缩时使用)

-z : 表示同时用gzip格式进行压缩:

-j : 表示同时用bzip2格式进行压缩:

-J : 表示同时用xz格式进行压缩:

-p : 表示保留使用源文件的属性,压缩前后的属性一样:(不常用)

-P : 表示可以使用绝对路径:(不常用)

2.4:打包并使用gzip格式进行压缩:

tar     -zcvf      text.tar.gz      test      1.txt   2.txt       #打包并用gzip压缩,同时压目录及文件

tar     -zxvf     text.tar.gz          #进行解包

[root@localhost d6z]# tar -zcvf test.tar.gz  test  1.txt 2.txt   #对test目录及文件打包并zip压缩:
[root@localhost d6z]# tar  -tf test.tar.gz                       #用 “ -tf ”命令查看文件:
test/
test/11.txt
test/1.txt
test/100.txt
1.txt
2.txt
[root@localhost d6z]# tar  -zxvf  test.tar.gz                  #解包及解压缩:
test/
test/11.txt
test/1.txt
test/100.txt
1.txt
2.txt

打包并使用bzip格式进行压缩:

tar      -jcvf    test.tar.bz2    test     1.txt              #tar打包并使用bzip压缩

tar      -jxvf    test.tar.bz2            #解包解压缩

[root@localhost d6z]# tar  -jcvf test.tar.bz2  test  1.txt     #打包并用bzip2格式进行压缩:
[root@localhost d6z]# tar -tf test.tar.bz2                   #用 ” -tf " 查看文件列表:
test/  
test/11.txt
test/1.txt
test/100.txt
1.txt
[root@localhost d6z]# tar   -jxvf test.tar.bz2             #解包及解压缩:
test/
test/11.txt
test/1.txt
test/100.txt
1.txt

打包并使用xz格式压缩:

tar     -Jcvf       test.tar.xz      test    1.txt                  #打包并使用xz格式进行压缩

tar     -Jxvf      test.tar.xz                          #解包解压缩

[root@localhost d6z]# tar -Jcvf test.tar.xz  test 1.txt     #打包并使用xz格式进行压缩:
[root@localhost d6z]# tar -tf test.tar.xz  test 1.txt       #用 ” -tf " 查看文件内容: 
test/
test/11.txt
test/1.txt
test/100.txt
1.txt
[root@localhost d6z]# tar -Jxvf  test.tar.xz               #解包及解压缩:
test/
test/11.txt
test/1.txt
test/100.txt
1.txt

总之,用gzip   bzip2   xz打包并压缩,都可以使用“ -tf ”命令进行查看文件列表,均不支持查看文件内容:

tar    -ft      filename.tar.gz(bzip2   xz)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/yuanhaohao/blog/1836752

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值