linux压缩和打包工具gzip_bzip2_xz_zip_tar
admin
2023-03-10 13:22:33
0

gizp:
*gzip工具不能压缩目录,只能压缩文件
压缩:gzip filename

[root@localhost test01]# ll -h *   #查看压缩前all.txt文件大小
-rw-r--r-- 1 root root 4.2M 9月   7 13:44 all.txt
[root@localhost test01]# gzip all.txt   #压缩all.txt文件
[root@localhost test01]# ll -h *    #查看压缩后all.txt文件大小
-rw-r--r-- 1 root root 1.2M 9月   7 13:44 all.txt.gz

解压:gzip -d filename

[root@localhost test01]# ls
all.txt.gz
[root@localhost test01]# gzip -d all.txt.gz 
[root@localhost test01]# ls
all.txt

解压:gunzip -d filename

[root@localhost test01]# ls
all.txt.gz
[root@localhost test01]# gunzip -d all.txt.gz 
[root@localhost test01]# ls
all.txt

指定压缩率:gzip -n filename (n的范围:1-9,压缩等级9压缩率最高,压缩速度也就最慢,对cup资源的消耗也就相对过高,压缩等级1压缩率最低,压缩速度也就最快,对cpu资源的消耗相对过低,默认等级为6)

[root@localhost test01]# gzip -9 all.txt
[root@localhost test01]# file all.txt.gz  #file查看文件最后一列压缩等级为最大压缩率
all.txt.gz: gzip compressed data, was "all.txt", from Unix, last modified: Sat Sep  7 13:44:13 2019, max compression

查看压缩文件内容:(在不解压的情况下查看压缩文件内容使用zcat命令)

[root@localhost test01]# zcat all.txt.gz 

-c 参数:在压缩或解压时保留源文件

[root@localhost test01]# gzip -c all.txt > all.txt.gz
[root@localhost test01]# ls
all.txt  all.txt.gz
[root@localhost test01]# gzip -d -c all.txt.gz > all2.txt
[root@localhost test01]# ls
all2.txt  all.txt  all.txt.gz

bzip2:
*与gzip类似,不能压缩目录,只能压缩文件,压缩率比gzip高
安装bzip2工具:

[root@localhost test01]# yum -y install bzip2

压缩:bzip2 filename
解压:bizp2 -d filename 或 bunzip2 -d filename
查看压缩文件内容:bzcat filename
*与gzip一样可以指定压缩率,但bzip2默认压缩等级为9,同样可以使用-c参数

xz:
与gzip、bzip2类似,不能压缩目录,只能压缩文件,压缩率比gzip、bzip2高*
压缩:xz filename
解压:xz -d filename 或 unxz -d filename
查看压缩文件内容:xzcat filename
与gzip、bzip一样可以指定压缩率,默认压缩等级最高,同样可以使用-c参数
gzip、bzip2、xz在解压时使用-c参数不仅可以保留源文件,还可以重命名解压文件*

zip:
*zip可以压缩目录和文件,在解压时可以指定解压路径,但不能重命名解压内容
安装:

[root@localhost ~]# yum -y install zip

压缩文件:zip 压缩文件名 源文件名 (源文件可以是多个文件)

[root@localhost test01]# ls
filetest.txt  test02  test.sh
[root@localhost test01]# zip abc.zip filetest.txt test.sh 
  adding: filetest.txt (deflated 85%)
  adding: test.sh (deflated 79%)
[root@localhost test01]# ls  #将filetest.txt test.sh两个文件添加到压缩文件abc.zip
abc.zip  filetest.txt  test02  test.sh

压缩目录:zip -r 压缩文件名 源文件名 (源文件可以是多个目录和文件)

[root@localhost test01]# ls
abc.zip  filetest.txt  test02  test.sh
[root@localhost test01]# zip -r linuxtest.zip test02/ filetest.txt 
  adding: test02/ (stored 0%)
  adding: test02/all.txt (deflated 71%)
  adding: filetest.txt (deflated 85%)
[root@localhost test01]# ls
abc.zip  filetest.txt  linuxtest.zip  test02  test.sh

*zip压缩或解压文件或目录后,会自动保留源文件

解压:unzip filename

[root@localhost test01]# ls
abc.zip  filetest.txt  linuxtest.zip  test02  test.sh
[root@localhost test01]# rm -rf filetest.txt test.sh 
[root@localhost test01]# ls
abc.zip  linuxtest.zip  test02
[root@localhost test01]# unzip abc.zip 
Archive:  abc.zip
  inflating: filetest.txt            
  inflating: test.sh                 
[root@localhost test01]# ls
abc.zip  filetest.txt  linuxtest.zip  test02  test.sh

将压缩文件中的内容解压到指定目录: unzip filename -d 目标目录路径

[root@localhost test01]# ls
abc.zip  filetest.txt  linuxtest.zip  test02  test.sh
[root@localhost test01]# unzip linuxtest.zip -d /root/mytest/
Archive:  linuxtest.zip
   creating: /root/mytest/test02/
  inflating: /root/mytest/test02/all.txt  
  inflating: /root/mytest/filetest.txt  
[root@localhost test01]# ls /root/mytest/
filetest.txt  test02

查看压缩文件中的文件列表: unzip -l filename
*与gzip、bzip2、xz不同,unzip只能查看文件列表,不能查看文件中的内容

[root@localhost test01]# unzip -l linuxtest.zip 
Archive:  linuxtest.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  09-07-2019 15:18   test02/
  4340076  09-07-2019 13:44   test02/all.txt
     2943  09-07-2019 15:26   filetest.txt
---------                     -------
  4343019                     3 files

tar:
*tar工具将多个文件或目录打包到一个文件中(比如要压缩一个目录,里面有很多小文件,可以使用tar将该目录先打包成一个文件再压缩),增加传输速度,对文件大小改变不会太大,tar打包时可以同时打包多个目录加文件

打包:tar -cvf 打包文件名 源文件

[root@localhost test01]# ls
test02  test.sh
[root@localhost test01]# tar -cvf testfile.tar test02/ test.sh 
test02/
test02/all.txt
test02/filetest.txt
test.sh
[root@localhost test01]# ls  #将目录/test02和文件test.sh都打包为testfile.tar文件
test02  testfile.tar  test.sh

解包:tar -xvf 目标文件

[root@localhost test01]# ls
test02  testfile.tar  test.sh
[root@localhost test01]# rm -rf test02 test.sh 
[root@localhost test01]# ls
testfile.tar
[root@localhost test01]# tar -xvf testfile.tar 
test02/
test02/all.txt
test02/filetest.txt
test.sh
[root@localhost test01]# ls
test02  testfile.tar  test.sh

查看tar文件的文件列表:tar -tf 目标文件

[root@localhost test01]# tar -tf testfile.tar 
test02/
test02/all.txt
test02/filetest.txt
test.sh

打包时过滤指定文件:- -exclude
过滤指定文件:

[root@localhost test01]# ls test02/
all.txt  filetest.txt  test.sh
[root@localhost test01]# tar -cvf testfile.tar --exclude filetest.txt test02/ 
test02/
test02/all.txt
test02/test.sh
[root@localhost test01]# ls
test02  testfile.tar
[root@localhost test01]# tar -tf testfile.tar 
test02/
test02/all.txt
test02/test.sh

过滤指定类型的文件:

[root@localhost test01]# tar -cvf testfile.tar --exclude "*.txt" test02/ 
test02/
test02/test.sh
[root@localhost test01]# ls
test02  testfile.tar
[root@localhost test01]# tar -tf testfile.tar 
test02/
test02/test.sh

可以使用多个- -exclude:

[root@localhost test01]# tar -cvf testfile.tar --exclude filetest.txt --exclude test.sh test02/ 
test02/
test02/all.txt
[root@localhost test01]# ls
test02  testfile.tar
[root@localhost test01]# tar -tf testfile.tar 
test02/
test02/all.txt

tar在打包的同时支持压缩:

1.打包的同时压缩成gzip包:-zcvf

[root@localhost test01]# du -sh test02/
4.2M    test02/
[root@localhost test01]# tar -zcvf testfile.tar.gz test02/
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh
[root@localhost test01]# du -sh testfile.tar.gz 
1.2M    testfile.tar.gz

解压tar.gz包:-zxvf

[root@localhost test01]# tar -zxvf testfile.tar.gz 
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh

2.打包的同时压缩成bzip2包: -jcvf

[root@localhost test01]# tar -jcvf testfile.tar.bz2 test02/
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh
[root@localhost test01]# du -sh testfile.tar.bz2 
1.2M    testfile.tar.bz2

解压tar.bz2包: -jxvf

[root@localhost test01]# tar -jxvf testfile.tar.bz2 
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh

3.打包的同时压缩成xz包: -Jcvf

[root@localhost test01]# tar -Jcvf testfile.tar.xz test02/
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh
[root@localhost test01]# du -sh testfile.tar.xz 
252K    testfile.tar.xz

解压tar.xz包: -Jxvf

[root@localhost test01]# tar -Jxvf testfile.tar.xz 
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh

相关内容

热门资讯

浙江人形与杰克科技签约2000... 来源:上海证券报·中国证券网 上证报中国证券网讯(记者 王子霖)据浙江人形机器人创新中心有限公司(以...
原创 三... 不出意外的话,今年苹果,高通、三星,联发科,这4家确定会推出2nm的手机芯片。 并且高通、联发科、苹...
Kimi、阶跃再获百亿融资,D... 图片由AI生成 出品 | 搜狐科技 作者 | 梁昌均 编辑 | 杨锦 中国大模型创业公司再次迎来融资...
“我妈有俩老公”?OPPO活该... 5月8日,OPPO官方微博发布了一条关于母亲节的宣发文案,内容因表述问题,引起热议。 相关话题“...
他,扛起又一个任正非时刻! 奔赴一场新的长征。 文 | 华商韬略 张静波 黄仁勋,急了。 2026年4月15日,在一场长达90分...
全球开源创新大会在巴黎举行 聚... 全球开源创新大会活动现场(5月6日摄)。新华社发(主办方供图) 新华社巴黎5月8日电 全球开源创新...
罕见!韩媒:韩国总统、国会议长... 据韩国《朝鲜日报》等韩媒报道,5月8日,韩国政坛出现罕见一幕:韩国总统李在明、韩国国会议长禹元植以及...
4000辆奇瑞商用车散件启运非... 河南日报讯(全媒体记者 龚砚庆 马青竹)5月8日上午,开封综合保税区内的汽车及零部件外贸基地一片繁忙...
“河南材料”中丨南阳新材料“追... 柔性功能薄膜隔热效率比传统产品高30%、紫外线与红外线阻隔率高达99%,产品在汽车玻璃、建筑玻璃和航...
河南超有品 第十个“中国品牌日... 从西安到灵宝寺河山,往返数百公里,仅半年,杨女士便跑了4趟。5月7日,她再次叩开果农司红娥家的大门,...