压缩、解压缩命令,算法不同,压缩比也会不同

压缩格式:gz, bz2, xz, zip, Z

===============================================================

gzip: .gz

    gzip/PATH/TO/SOMEFILE:压缩完成后会删除原文件

       -d:

       -#:1-9,指定压缩比,默认是6;

   

gunzip:

  gunzip/PATH/TO/SOMEFILE.gz: 解压完成后会删除原文件

  zcat /PATH/TO/SOMEFILE.gz: 不解压的情况,查看文本文件的内容


 ===============================================================

bzip2:    .bz2

比gzip有着更大压缩比的压缩工具,使用格式近似

    bzip2 /PATH/TO/SOMEFILE

      -d

      -#: 1-9,默认是6

      -k: 压缩时保留原文件

       

    bunzip2   /PATH/TO/SOMEFILE.bz2

    bzcat


 ===============================================================

xz: .xz

    xz/PATH/TO/SOMEFILE

        -d

        -#: 1-9, 默认是6

        -k: 压缩时保留原文件

       

    unxz

    xzdec

    xzcat

      xz安装包下载链接: xz-5.2.2.tar.bz2 (1165 KiB) 


===============================================================

zip: 既归档又压缩的工具

    zip FILENAME.zip FILE1 FILE2 ...: 压缩后不删除原文件

    unzipFILENAME.zip


  eg1: zip常用命令

[root@localhost ~]# du -sh test2

7.2M    test2

[root@localhost ~]# zip test3.zip test2/*
  adding: test2/11/ (stored 0%)
  adding: test2/a (stored 0%)
  adding: test2/b (stored 0%)
  adding: test2/c (stored 0%)
  adding: test2/d (stored 0%)
  adding: test2/initrd-2.6.18-164.el5.img (deflated 62%)
  adding: test2/test (deflated 90%)
  adding: test2/test1.txt (deflated 97%)
  adding: test2/test2.txt (deflated 97%)
  adding: test2/test3.txt (deflated 97%)
  adding: test2/test.tar (deflated 98%)
  adding: test2/test.tar.bz2 (deflated 14%)
[root@localhost ~]# ls -lh test3.zip
-rw-r--r-- 1 root root 2.7M Nov  1 09:39 test3.zip


 ===============================================================

tar: 归档工具, .tar         英文原义 Tape ARchive   磁带归档

    -c: 创建归档文件

    -f FILE.tar:操作的归档文件

    -x: 展开归档

    --xattrs: 归档时,保留文件的扩展属性信息

    -t: 不展开归档,直接查看归档了哪些文件

 

    -jcf:    归档并调用bzip2压缩

    -jxf: 调用bzip2解压缩并展开归档,-j选项可省略


    -zcf: 归档并调用gzip压缩

    -zxf:  调用gzip解压缩并展开归档,-z选项可省略

  

    -Jcf:    归档并调用xz压缩

    -Jxf: 调用xz解压缩并展开归档,-J选项可省略


  eg1: tar常用命令

[root@localhost test2]# tar -cf test.tar  test*.txt     ** 只归档不压缩 
[root@localhost test2]# ll
total 268
-rw-r--r-- 1 root  root  10240 Nov  1 07:56 test
-rw-r--r-- 1 root  root  39016 Nov  1 07:57 test1.txt
-rw-r--r-- 1 root  root  39277 Nov  1 08:38 test2.txt
-rw-r--r-- 1 root  root  44611 Nov  1 08:41 test3.txt
-rw-r--r-- 1 root  root 122880 Nov  1 07:58 test.tar

[root@localhost test2]# tar -jcf test.tar.bz2 test*.txt     **归档并调用gzip压缩 
[root@localhost test2]# ll
total 268
-rw-r--r-- 1 root  root  10240 Nov  1 07:56 test
-rw-r--r-- 1 root  root  39016 Nov  1 07:57 test1.txt
-rw-r--r-- 1 root  root  39277 Nov  1 08:38 test2.txt
-rw-r--r-- 1 root  root  44611 Nov  1 08:41 test3.txt
-rw-r--r-- 1 root  root 122880 Nov  1 07:58 test.tar
-rw-r--r-- 1 root  root   2437 Nov  1 08:47 test.tar.bz2

[root@localhost test2]# tar -tf test.tar.bz2     **不展开归档直接查看

test1.txt
test2.txt
test3.txt

[root@localhost test2]# rm -f test*.txt

[root@localhost test2]# ll
total 144
-rw-r--r-- 1 root  root  10240 Nov  1 07:56 test
-rw-r--r-- 1 root  root 122880 Nov  1 07:58 test.tar
-rw-r--r-- 1 root  root   2437 Nov  1 08:47 test.tar.bz2

[root@localhost test2]# tar -jxf test.tar.bz2    **调用gzip解压缩并展开归档,-j选项可省略

[root@localhost test2]# ll
total 268
-rw-r--r-- 1 root  root  10240 Nov  1 07:56 test
-rw-r--r-- 1 root  root  39016 Nov  1 07:57 test1.txt
-rw-r--r-- 1 root  root  39277 Nov  1 08:38 test2.txt
-rw-r--r-- 1 root  root  44611 Nov  1 08:41 test3.txt
-rw-r--r-- 1 root  root 122880 Nov  1 07:58 test.tar
-rw-r--r-- 1 root  root   2437 Nov  1 08:47 test.tar.bz2


===============================================================

archive: 归档,归档本身并不意味着压缩


cpio: 归档工具


  eg1: 查看OS中已有的cpio文档:

 [root@localhost test2]# du -sh /boot/initrd-2.6.18-164.el5.img
2.6M    /boot/initrd-2.6.18-164.el5.img

[root@localhost test2]# file /boot/initrd-2.6.18-164.el5.img

/boot/initrd-2.6.18-164.el5.img: gzip compressed data, from Unix, last modified: Thu Apr 17 11:43:57 2014, max compression

[root@localhost test2]# gzip -d initrd-2.6.18-164.el5.img.gz

[root@localhost test2]# ls -lh
total 7.1M

-rw------- 1 root  root 6.8M Nov  1 09:00 initrd-2.6.18-164.el5.img

[root@localhost test2]# file initrd-2.6.18-164.el5.img

initrd-2.6.18-164.el5.img: ASCII cpio archive (SVR4 with no CRC)


  eg1:写一个脚本:从键盘让用户输入几个文件,脚本能够将此几个文件归档压缩成一个文件;

read:  -p“PROMPT":  给出提示

#!/bin/bash
#
read -p "Three files: " FILE1 FILE2 FILE3
read -p "Destionation:" DEST
read -p "Compress[gzip|bzip2|xz]:" COMP

case $COMP in
bzip2)
  tar -jcf ${DEST}.tar.bz2 $FILE1 $FILE2 $FILE3
  ;;  
gzip)
  tar -zcf ${DEST}.tar.bz2 $FILE1 $FILE2 $FILE3
  ;;  
xz)
  tar -cf ${DEST}.tar.bz2 $FILE1 $FILE2 $FILE3
  xz ${DEST}.tar
  ;;  
*)
 echo "Unknow."
 exit 9
 ;;
esac


[root@localhost ~]# ./myarc.sh
Three files: ./test2/test1.txt ./test2/test2.txt ./test2/test3.txt
Destionation:test3d
Compress[gzip|bzip2|xz]:gzip
[root@localhost ~]# ll
total 1344508
-rw-r--r--  1 root  root       2130 Nov  1 13:22 test3d.tar.bz2



---end---