文件/目录操作(7)——zip、gzip、bzip2、tar命令

来源:互联网 发布:淘宝收到假货怎么投诉 编辑:程序博客网 时间:2024/06/08 20:12
*.zip
说明:zip对文件和目录都可以进行压缩操作
           zip  file.zip     file1  file2    //将file1file2打包成file.zip
           zip  file.zip     *.jpg           //将所有jpg结尾的文件打包成file.zip
           unzip      file.zip               //解压缩
           unzip  -l  file.zip               //查看压缩文件内容
[root@vm_hosts test]# unzip file.zip Archive: file.zipreplace testfile? [y]es, [n]o, [A]ll, [N]one, [r]ename: //解压时若还和原文件冲突,则询问是否替换或重命名 [root@vm_hosts test]# unzip -l file.zip Archive: file.zipLength Date Time Name--------- ---------- ----- ----104857600 07-15-2015 08:13 testfile--------- -------104857600 1 file

*.gz
说明:gzip
只能对文件进行压缩操作
注意:这种方式的压缩和解压都将不再保留原文件或原压缩文件
           gzip  -num//按照#程度打包;num=1—9(1最快压缩比最差,9反之,默认是6)
                   -r       //对目录下所有的子目录所有“文件”递归打包
                   -v      //显示过程
                   -d      //解压缩
           gunzip      //解压缩
           zcat         //查看压缩包文本文件的内容
           gzip <pw>pw.gz      //用输入输出重定向的方法,使得压缩或解压后原文件得以保留
           gunzip <pw.gz >pw
[root@localhost test]# lsfile1 file2[root@localhost test]# gzip file2 file1[root@localhost test]# lsfile1.gz file2.gz[root@localhost test]# gzip -l file1.gz compressed uncompressed ratio uncompressed_name26 0 0.0% file1[root@localhost test]# ls mk1 mk2[root@localhost test]# gzip mk1gzip: mk1 is a directory -- ignored[root@localhost test]# ls mk2file1 file2[root@localhost test]# gzip -r mk2[root@localhost test]# ls mk2file1.gz file2.gz

*.bz2
说明:基本原理同
*.gz一样
           bzip2   -num    //压缩
           bzip2   -d         //解压缩
           bzcat


tar命令
      -c:创建 .tar 格式的包文件 -c, --create
      -x:解开.tar格式的包文件 -x, --extract,
      -v:输出详细信息 -v, --verbose
      -f:表示使用归档文件 -f, --file
      -t:列表查看包内的文件     ( tar tf xx.gz   //查看包内内容)
      -p:保持原文件的原来属性
      -P:保持原文件的绝对路径(在创建tar包文件的时候使用,会保留文件的绝对路径,在解压的时候使用,会按照绝对路径去释放压缩包的内容,会替换原来的文件。)(注意,应尽量不使用-P选项)
      -C:解压时指定路径
      --exclude=:指定打包时不包含某个文件
      
--->.tar.gz    tar
zcvf  file.tar.gz    file1 file2  
--->.tar         tar
cvf   file.tar         file1 file2
--->.tar.bz2  tar
jcvf   file.tar.bz2  file1 file2
--->.tar.xz    tar cvf     file.tar        file1 file2  &&   xz -z file.tar.xz
--->.tar.Z     tar Zcvf   file.tar.Z     file1 file2


--->.tar.gz/.tgz  tar  zxvf  xx.tgz/xx.tar.gz
--->.tar/.gz        tar   
xvf   xx.gz/xx.tar
--->.tar.xz         tar   xvf    file.tar.xz 或 xz -d file.tar.xz&&tar xvf file.tar
--->.tar.Z          tar   Zxvf   xx.tar.Z

应用实例:
tar czvf /lianxi/bak123.tar.gz /bak/* --exclude=/bak/{boot,var,etc,jj_diandian.tar.gz}
0 0