文件的压缩、解压缩和打包命令

压缩命令:     compress       gzip      bzip2      xz        等

解压缩命令:  uncompress   gunzip  bunzip2   unxz     等

打包(归档)命令: tar   cpio

     其中compress、gzip、bzip2和xz只能用于单个压缩文件,不能用于压缩目录

compress: .Z   最古老的压缩命令

  compress  /PATH/TO/FILENAME          -----> FILENAME.Z

  uncompress FILENAME.Z

gzip: .gz           
    gzip /PATH/TO/SOMEFILE:    压缩完成后会删除原文件
        -d: decompress
        -#:1-9,指定压缩比,默认是6;

    gunzip  相当于 gzip -d

    zcat  将信息解压到stdout压缩文件没有发生改变。

            即不解压的情况,查看文本文件的内容。

[root@localhost tar]# ls -lh
total 1.2M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
[root@localhost tar]# gzip messages
[root@localhost tar]# ls -lh
total 152K
-rw-------. 1 root root 152K Aug 12 10:44 messages.gz
[root@localhost tar]# gunzip messages.gz
[root@localhost tar]# ls -lh
total 1.2M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
[root@localhost tar]# gzip messages
[root@localhost tar]# gzip -d messages.gz
[root@localhost tar]# ls -lh
total 1.2M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
[root@localhost tar]# gzip messages
[root@localhost tar]# zcat messages.gz | wc -l
14026

bzip2: .bz2
比gzip有着更大压缩比的压缩工具,使用格式近似
    bzip2 /PATH/TO/SOMEFILE
        -d    
        -#: 1-9,默认是6
        -k: 压缩时保留原文件
        
    bunzip2 /PATH/TO/SOMEFILE.bz2
    bzcat         decompresses files to stdout

[root@localhost tar]# ls -lh
total 56K                                 -------比gzip压缩能力更强大。
-rw-------. 1 root root 55K Aug 12 10:44 messages.bz2
[root@localhost tar]# bzip2 -k messages
[root@localhost tar]# ls -lh
total 1.2M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
-rw-------. 1 root root  55K Aug 12 10:44 messages.bz2

xz: .xz
    xz /PATH/TO/SOMEFILE
        -d
        -#: 1-9, 默认是6
        -k: 压缩时保留原文件

    unxz               is equivalent to xz --decompress.
    xzcat/xzdec     is equivalent to xz --decompress --stdout.

[root@localhost tar]# xz messages
[root@localhost tar]# ls -lh
total 36K                 ------------------压缩能力更强
-rw-------. 1 root root 34K Aug 12 10:44 messages.xz
[root@localhost tar]# xz -dk messages.xz
[root@localhost tar]# ls -lh
total 1.2M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
-rw-------. 1 root root  34K Aug 12 10:44 messages.xz
[root@localhost tar]# unxz messages.xz -k
[root@localhost tar]# ls -lh
total 1.2M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
-rw-------. 1 root root  34K Aug 12 10:44 messages.xz

zip - package and compress (archive) files

zip: 既归档又压缩的工具              众多OS默认支持此功能

    zip is a compression and file  packaging  utility  for  Unix,  VMS,
    MSDOS,  OS/2, Windows 9x/NT/XP, Minix, Atari, Macintosh, Amiga, and
    Acorn RISC OS.  It is analogous to a combination of the  Unix  com-
    mands  tar(1)  and  compress(1)  and is compatible with PKZIP (Phil
    Katz’s ZIP for MSDOS systems).

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

[root@localhost tar]# ls -lh
total 1.2M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
-rw-------. 1 root root  34K Aug 12 10:44 messages.xz
[root@localhost tar]# zip messages.zip messages*
  adding: messages (deflated 87%)
  adding: messages.xz (deflated 0%)
[root@localhost tar]# ls -lh
total 1.4M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
-rw-------. 1 root root  34K Aug 12 10:44 messages.xz
-rw-r--r--. 1 root root 186K Aug 12 11:33 messages.zip
[root@localhost tar]# unzip messages.zip
Archive:  messages.zip
replace messages? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: messages                
replace messages.xz? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: messages.xz             
[root@localhost tar]# ls -lh
total 1.4M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
-rw-------. 1 root root  34K Aug 12 10:44 messages.xz
-rw-r--r--. 1 root root 186K Aug 12 11:33 messages.zip    

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

tar: .tar   归档工具

    GNU  ‘tar’  saves  many  files  together into a single tape or disk archive,

    and can restore individual files from the archive.

    -c: 创建归档文件
    -f FILE.tar: 归档文件(目的文件)               use archive file or device ARCHIVE
    -x: 展开归档                                           extract files from an archive
    --xattrs: 归档时,保留文件的扩展属性信息
    -t: 不展开归档,直接查看归档了哪些文件       --list    list the contents of an archive

    -v, --verbose
              verbosely list files processed


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


    -j, --bzip2
              filter the archive through bzip2
    -jcf: bzip2
    -jxf: 调用bzip2解压缩并展开归档,-z选项可省略
    

    -J, --xz
              filter the archive through xz
    -Jcf: xz   rhel6
    -Jxf:

       tar -cf archive.tar foo bar
              # Create archive.tar from files foo and bar.

       tar -tvf archive.tar
              # List all files in archive.tar verbosely.

       tar -xf archive.tar
              # Extract all files from archive.tar.

[root@localhost tar]# ls -lh
total 1.4M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
-rw-------. 1 root root  34K Aug 12 10:44 messages.xz
-rw-r--r--. 1 root root 186K Aug 12 11:33 messages.zip

[root@localhost tar]# tar -cf messeages.tar messages*
[root@localhost tar]# ls -lh
total 2.7M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
-rw-------. 1 root root  34K Aug 12 10:44 messages.xz
-rw-r--r--. 1 root root 186K Aug 12 11:33 messages.zip
-rw-r--r--. 1 root root 1.4M Aug 12 11:57 messeages.tar
[root@localhost tar]# tar -xvf messeages.tar
messages
messages.xz
messages.zip
[root@localhost tar]# ls -lh
total 2.7M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
-rw-------. 1 root root  34K Aug 12 10:44 messages.xz
-rw-r--r--. 1 root root 186K Aug 12 11:33 messages.zip
-rw-r--r--. 1 root root 1.4M Aug 12 11:57 messeages.tar

[root@localhost tar]# rm -f messages.tar

[root@localhost tar]# tar -zcvf messages.tar.gz mess*
messages
messages.xz
messages.zip
[root@localhost tar]# ls -lh
total 1.7M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
-rw-r--r--. 1 root root 362K Aug 12 12:26 messages.tar.gz
-rw-------. 1 root root  34K Aug 12 10:44 messages.xz
-rw-r--r--. 1 root root 186K Aug 12 12:17 messages.zip
messages
messages.xz
messages.zip
[root@localhost tar]# ls -lh
total 1.7M
-rw-------. 1 root root 1.2M Aug 12 10:44 messages
-rw-r--r--. 1 root root 362K Aug 12 12:26 messages.tar.gz
-rw-------. 1 root root  34K Aug 12 10:44 messages.xz
-rw-r--r--. 1 root root 186K Aug 12 12:17 messages.zip

...

cpio - copy files to and from archives

[root@localhost tar]# cp /boot/initramfs-2.6.32-573.el6.x86_64.img .

[root@localhost tar]# file /boot/initramfs-2.6.32-573.el6.x86_64.img
/boot/initramfs-2.6.32-573.el6.x86_64.img: gzip compressed data, from Unix, last modified: Wed Jan  6 08:43:04 2016, max compression

[root@localhost tar]# mv initramfs-2.6.32-573.el6.x86_64.img initramfs-2.6.32-573.el6.x86_64.img.gz
[root@localhost tar]# gzip -d initramfs-2.6.32-573.el6.x86_64.img.gz

[root@localhost tar]# file initramfs-2.6.32-573.el6.x86_64.img
initramfs-2.6.32-573.el6.x86_64.img: ASCII cpio archive (SVR4 with no CRC)

原文地址:https://www.cnblogs.com/ant-colonies/p/6292590.html