6.5 zip压缩工具

  • zip 1.txt.zip 1.txt    //压缩文件
  • zip -r 123.zip 123/   //压缩目录
  • unzip 1.txt.zip //解压
  • unzip 123.zip -d /root/456/ //解压文件,并指定解压到那个目录下
  • 不能查看压缩文件的内容,只能查看内容列表
  • unzip -l 123.zip //查看压缩文件的内容列表
  • zip压缩文件后,源文件不消失
  1. 需要安装zip包
[root@linux-151 d6z]# zip 1.txt.zip 1.txt
-bash: zip: 未找到命令
[root@linux-151 d6z]# yum install -y zip
  1. 使用zip工具压缩文件1.txt文件。
[root@linux-151 d6z]# zip 1.txt.zip 1.txt
adding: 1.txt (deflated 74%)
[root@linux-151 d6z]# ls
1.txt 1.txt.zip 2.txt 3.txt 4.txt test
[root@linux-151 d6z]# du -sh 1.txt.zip
664K 1.txt.zip
  1. 使用zip工具压缩目录test
[root@linux-151 d6z]# zip -r test.zip test
adding: test/ (stored 0%)
adding: test/3.txt (deflated 74%)
adding: test/4.txt (deflated 74%)
[root@linux-151 d6z]# ls
1.txt 1.txt.zip 2.txt 3.txt 4.txt test test.zip
[root@linux-151 d6z]# du -sh test.zip
1.3M test.zip
[root@linux-151 d6z]# du -sh test
5.0M test
  1. 解压压缩文件
[root@linux-151 d6z]# unzip 1.txt.zip
-bash: unzip: 未找到命令
[root@linux-151 d6z]# yum install -y unzip
[root@linux-151 d6z]# unzip 1.txt.zip
Archive: 1.txt.zip
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A //A表示全部覆盖,N表示全部不覆盖
inflating: 1.txt
[root@linux-151 d6z]# ls
1.txt 1.txt.zip 2.txt 3.txt 4.txt test test.zip
[root@linux-151 d6z]# unzip test.zip
Archive: test.zip
replace test/3.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
inflating: test/3.txt
inflating: test/4.txt
  1. 指定解压文件路径
[root@linux-151 d6z]# unzip  test.zip  -d  /tmp/test
Archive: test.zip
creating: /tmp/test/test/
inflating: /tmp/test/test/3.txt
inflating: /tmp/test/test/4.txt
[root@linux-151 d6z]# ls /tmp/test
333 test test1 test2
  1. 查看压缩文件内容列表
[root@linux-151 d6z]# unzip -l test.zip
Archive: test.zip
Length Date Time Name
--------- ---------- ----- ----
0 04-17-2018 21:34 test/
2572100 04-17-2018 21:33 test/3.txt
2572100 04-17-2018 21:34 test/4.txt



6.6 tar打包

  1. tar本身是一个打包工具,可以把目录打包成一个文件,它把所有的文件整合成一个大文件,方便复制或者移动。
  2. 命令格式:tar [-zjxcvfpP] filename.tar
  3. tar打包或解包均会直接覆盖原文件和目录,不会提示覆盖信息


tar参数

  • -z表示同时使用gzip压缩
  • -j表示同时用bzip压缩
  • -J表示同时用xz压缩
  • -c表示建立一个tar包或者压缩文件包
  • -x表示解包或者解压
  • -v表示可视化
  • -f后面跟文件名(-f filename,表示压缩后的文件名为filename)注意:如果多个参数组合的情况下,-f要写在最后面。
  • -t表示查看tar包里的文件
  • --exclude filename 表示在打包或压缩时,不要将某个文件不包含在里面。
  • 打包或者解包,源文件都存在。
  • 打包后产生的文件与打包前的文件在同一目录下。


tar使用方法:

  1. tar -cvf 123.tar 123 // 打包目录123
  2. tar -cvf aming.tar 1.txt 123 //打包目录123和文件1.txt
  3. tar -xvf aming.tar //解包
  4. tar -tf aming.tar //查看打包文件
  5. tar -cvf aming.tar --exclude 1.txt --exclude 2 123 //打包目录123,单不包括文件1.txt和2
  6. 打包目录test和文件1.txt,2.txt
[root@linux-151 d6z]# tar -cvf test.tar test 1.txt 2.txt
test/
test/3.txt
test/4.txt
1.txt
2.txt
[root@linux-151 d6z]# ls
1.txt 1.txt.zip 2.txt 3.txt 4.txt test test.tar test.zip
  1. 查看打包文件test.tar的内容
[root@linux-151 d6z]# tar -tf test.tar
test/
test/3.txt
test/4.txt
1.txt
2.txt
  1. 解包文件test.tar
[root@linux-151 d6z]# tar -xvf test.tar
test/
test/3.txt
test/4.txt
1.txt
2.txt
  1. 打包目录test和文件1.txt 2.txt ,但是不包含文件3.txt
[root@linux-151 d6z]# tar -cvf test.tar --exclude 3.txt  test 1.txt 2.txt
test/
test/4.txt
1.txt
2.txt
  1. 打包目录test和文件1.txt 2.txt ,但是不包含文件3.txt和4.txt
[root@linux-151 d6z]# tar -cvf test.tar --exclude 3.txt --exclude 4.txt  test 1.txt 2.txt
test/
1.txt
2.txt



6.7 打包并压缩

  • tar 命令还可以在打包的同时支持gzip压缩,bzip压缩和xz压缩


打包并压缩的使用方法:

  • tar -zcvf 123.tar.gz 123
  • tar -zxvf 123.tar.gz
  • tar -jcvf 123.bz2 123
  • tar -jxvf 123.bz2
  • tar -Jcvf 123.xz 123
  • tar -Jxvf 123.xz
  • tar -tf 123.bz2 / tar -tf 123.gz / tar -tf 123.xz
  1. 打包目录test以及文件1.txt和2.txt并使用gzip压缩
[root@linux-151 d6z]# tar -zcvf test.tar.gz test 1.txt 2.txt
test/
test/3.txt
test/4.txt
1.txt
2.txt
[root@linux-151 d6z]# ls
1.txt 1.txt.zip 2.txt 3.txt 4.txt test test.tar test.tar.gz test.zip
  1. 查看打包文件
[root@linux-151 d6z]# tar -tf test.tar.gz
test/
test/3.txt
test/4.txt
1.txt
2.txt
解包
[root@linux-151 d6z]# tar -zxvf test.tar.gz
test/
test/3.txt
test/4.txt
1.txt
2.txt
  1. 打包文件并使用bzip2压缩
[root@linux-151 d6z]# tar -jcvf test.tar.bz2 test 1.txt 2.txt
test/
test/3.txt
test/4.txt
1.txt
2.txt
解包
[root@linux-151 d6z]# tar -jxvf test.tar.bz2
test/
test/3.txt
test/4.txt
1.txt
2.txt
  1. 打包文件并使用xz压缩
[root@linux-151 d6z]# tar -Jcvf test.tar.xz test 1.txt 2.txt
test/
test/3.txt
test/4.txt
1.txt
2.txt
解包
[root@linux-151 d6z]# tar -Jxvf test.tar.xz
test/
test/3.txt
test/4.txt
1.txt
2.txt
  1. 查看打包文件
[root@linux-151 d6z]# tar tf test.tar.xz
test/
test/3.txt
test/4.txt
1.txt
2.txt
[root@linux-151 d6z]# du -sh test.tar.gz test.tar.bz2 test.tar.xz
2.6M test.tar.gz
988K test.tar.bz2
64K test.tar.xz

注意:打包后文件越小,耗时越长,占用cpu资源越多。