您的位置:首页 > 运维架构 > Linux

linux压缩和打包工具gzip_bzip2_xz_zip_tar

2019-09-07 16:52 1501 查看

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
43a6
]# 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: