2018.6.23 四周第五次课 6.5 zip压缩工具 6.6 tar打包 6.7 打包并压缩

四周第五次课
6.5 zip压缩工具
6.6 tar打包
6.7 打包并压缩

一、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

二、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
  1. 打包目录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

三、打包并压缩

  • 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资源越多。

精品帖子: http://ask.apelearn.com/question/5435

转载于:https://my.oschina.net/u/3866935/blog/1834409

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值