压缩工具zip,tar打包,打包并压缩

zip压缩工具

zip命令可以压缩目录和文件,-r 压缩目录。

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压缩文件后,源文件不消失
  • unzip解压文件后,会直接覆盖原文件和目录,不会提示覆盖信息
  1. 需要安装zip包
[root@yong-02 yyl]# zip 2.txt.zip 2.txt 
-bash: zip: 未找到命令
[root@yong-02 yyl]# yum install -y zip
  1. 使用zip工具压缩文件2.txt文件。
[root@yong-02 gzip]# zip 2.txt.zip 2.txt 
  adding: 2.txt (deflated 74%)
[root@yong-02 gzip]# ls
1.txt  2.txt  2.txt.zip  3.txt  4.txt  5.txt  yyl
[root@yong-02 gzip]# du -sh 2.txt.zip 
560K	2.txt.zip
  1. 使用zip工具压缩目录yyl
[root@yong-02 gzip]# zip -r yyl.zip yyl
  adding: yyl/ (stored 0%)
  adding: yyl/1.txt (deflated 74%)
  adding: yyl/2.txt (deflated 74%)
  adding: yyl/3.txt (deflated 74%)
  adding: yyl/4.txt (deflated 74%)
  adding: yyl/5.txt (deflated 74%)
[root@yong-02 gzip]# ls
1.txt  2.txt  2.txt.zip  3.txt  4.txt  5.txt  yyl  yyl.zip
[root@yong-02 gzip]# du -sh yyl.zip 
2.8M	yyl.zip
[root@yong-02 gzip]# du -sh yyl
11M	yyl
  1. 解压压缩文件
[root@yong-02 gzip]# unzip yyl.zip
-bash: unzip: 未找到命令
[root@yong-02 gzip]# yum install unzip
[root@yong-02 gzip]# unzip yyl.zip 
Archive:  yyl.zip
replace yyl/1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: yyl/1.txt               
replace yyl/2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
  inflating: yyl/2.txt               
  inflating: yyl/3.txt               
  inflating: yyl/4.txt               
  inflating: yyl/5.txt               
[root@yong-02 gzip]# ls
1.txt  2.txt  2.txt.zip  3.txt  4.txt  5.txt  yyl  yyl.zip

1.指定解压文件路径

[root@yong-02 gzip]# unzip yyl.zip -d test/
Archive:  yyl.zip
   creating: test/yyl/
  inflating: test/yyl/1.txt          
  inflating: test/yyl/2.txt          
  inflating: test/yyl/3.txt          
  inflating: test/yyl/4.txt          
  inflating: test/yyl/5.txt          
[root@yong-02 gzip]# ls test/yyl/
1.txt  2.txt  3.txt  4.txt  5.txt

2.查看压缩文件内容列表

[root@yong-02 gzip]# unzip -l yyl.zip 
Archive:  yyl.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  04-17-2018 11:30   yyl/
  2190726  04-17-2018 11:30   yyl/1.txt
  2190726  04-17-2018 11:30   yyl/2.txt
  2190726  04-17-2018 11:30   yyl/3.txt
  2190726  04-17-2018 11:30   yyl/4.txt
  2190726  04-17-2018 11:30   yyl/5.txt
---------                     -------
 10953630                     6 files

tar打包

  1. tar本身是一个打包工具,可以把目录打包成一个文件,它把所有的文件整合成一个大文件,方便复制或者移动。
  2. 命令格式:tar [-zjxcvfpP] filename.tar

tar参数

  • -z表示同时使用gzip压缩
  • -j表示同时用bzip2压缩
  • -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

打包目录yyl

[root@yong-02 gzip]# tar -cvf yyl.tar yyl/
yyl/
yyl/1.txt
yyl/2.txt
yyl/3.txt
yyl/4.txt
yyl/5.txt

查看打包文件yyl.tar的内容

[root@yong-02 gzip]# tar -tf yyl.tar
yyl/
yyl/1.txt
yyl/2.txt
yyl/3.txt
yyl/4.txt
yyl/5.txt

解包文件yyl.tar

[root@yong-02 gzip]# tar -xvf yyl.tar
yyl/
yyl/1.txt
yyl/2.txt
yyl/3.txt
yyl/4.txt
yyl/5.txt

打包目录yyl ,但是不包含文件2.txt

[root@yong-02 gzip]# tar -cvf yyl.tar --exclude 2.txt yyl/
yyl/
yyl/1.txt
yyl/3.txt
yyl/4.txt
yyl/5.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

打包文件并使用gzip压缩

[root@yong-02 gzip]# tar -czvf yyl.tar.gz  yyl/
yyl/
yyl/1.txt
yyl/2.txt
yyl/3.txt
yyl/4.txt
yyl/5.txt

   查看打包文件

[root@yong-02 gzip]# tar -tf yyl.tar.gz
yyl/
yyl/1.txt
yyl/2.txt
yyl/3.txt
yyl/4.txt
yyl/5.txt

解压缩yyl.tar.gz

[root@yong-02 gzip]# tar -zxvf yyl.tar.gz 
yyl/
yyl/1.txt
yyl/2.txt
yyl/3.txt
yyl/4.txt
yyl/5.txt

打包文件并使用bzip2压缩

[root@yong-02 gzip]# tar -cjvf yyl.tar.bz2  yyl/
yyl/
yyl/1.txt
yyl/2.txt
yyl/3.txt
yyl/4.txt
yyl/5.txt

解压缩yyl.tar.bz2

[root@yong-02 gzip]# tar -xjvf yyl.tar.bz2 
yyl/
yyl/1.txt
yyl/2.txt
yyl/3.txt
yyl/4.txt
yyl/5.txt

打包文件并使用xz压缩

[root@yong-02 gzip]# tar -cJvf yyl.tar.xz  yyl/
yyl/
yyl/1.txt
yyl/2.txt
yyl/3.txt
yyl/4.txt
yyl/5.txt

 解压缩yyl.tar.xz

[root@yong-02 gzip]# tar -xJvf yyl.tar.xz
yyl/
yyl/1.txt
yyl/2.txt
yyl/3.txt
yyl/4.txt
yyl/5.txt

查看打包文件

[root@yong-02 gzip]# tar -tf yyl.tar.xz
yyl/
yyl/1.txt
yyl/2.txt
yyl/3.txt
yyl/4.txt
yyl/5.txt

 压缩包大小比较:

[root@yong-02 gzip]# du -sh yyl.tar.gz yyl.tar yyl.tar.bz2 yyl.tar.xz 
2.8M	yyl.tar.gz
11M	yyl.tar
980K	yyl.tar.bz2
60K	yyl.tar.xz

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

猜你喜欢

转载自my.oschina.net/u/3791387/blog/1796417