Linux圧縮および解凍ツールおよびアーカイブツールの紹介


一、一般的な圧縮と解凍ツール
あっしゅく
解凍
接尾辞
compress
uncompress
.z
gzip
gunzip
.gz
bzip2
bunzip2
.bz2
xz
unxz
.xz
lzma
unlzma
.lzma
zip
unzip
.zip
1、gzip/gunzip/zcat
gzip圧縮後、元のファイルは削除され、圧縮後のファイルのみが保持されます.
(1)gzip:圧縮ファイル
  • SYNOPSIS:gzip [OPTIONS]… FILE…
  • OPTIONS:
  • -d:gunzipに相当する解凍;
  • -#:圧縮比を指定し、デフォルトは6で、数字範囲は1~9で、数字が大きいほど圧縮比が大きくなります.
  • -c:圧縮結果を標準出力に出力し、出力リダイレクトを通じて元のファイルを圧縮して保持する目的を達成することが多い.

  •   /var/log/messags   /tmp   
    [root@localhost tmp]# cp /var/log/messages ./
    [root@localhost tmp]# ll -h
        236K
    -rw-------. 1 root root 234K 3   12 04:52 messages
    
     messages      ,  messages.gz  , messages     。
    [root@localhost tmp]# gzip messages 
    [root@localhost tmp]# ll -h
        32K
    -rw-------. 1 root root 32K 3   12 04:52 messages.gz
    
       messsages.gz  ,
    [root@localhost tmp]# gzip -d messages.gz 
    [root@localhost tmp]# ll -h
        236K
    -rw-------. 1 root root 234K 3   12 04:52 messages
    
           1 messages      ,  messages.gz      36K,      6    32K  4K
    [root@localhost tmp]# gzip -1 messages 
    [root@localhost tmp]# ll -h
        36K
    -rw-------. 1 root root 36K 3   12 04:52 messages.gz
    
        ,  -c        ,              ;
           messages.gz         ,             
    [root@localhost tmp]# gzip -d messages.gz 
    [root@localhost tmp]# ls
    messages
    [root@localhost tmp]# gzip -c messages >messages.gz
    [root@localhost tmp]# ll -h
        268K
    -rw-------. 1 root root 234K 3   12 04:52 messages
    -rw-r--r--. 1 root root  32K 3   12 04:56 messages.gz
    

    (2)gunzip:解凍
  • SYNOPSIS:gunzip FILE…
  •    gunzip     
    [root@localhost tmp]# ls
    messages.gz
    [root@localhost tmp]# gunzip messages.gz 
    [root@localhost tmp]# ls
    messages
    

    (3)zcat:圧縮ファイルの内容を解凍せずに表示する
  • SYNOPSIS:zcat FILE…
  • [root@localhost tmp]# ls
    messages.gz
    [root@localhost tmp]# zcat messages.gz  |head 
    Mar 11 23:45:01 localhost kernel: imklog 5.8.10, log source = /proc/kmsg started.
    Mar 11 23:45:01 localhost rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" 
    ...(       )...
    

    2、bzip 2/bunzip 2/bzcat:ほとんどgzipと同じ使用
    (1)bzip 2:圧縮ファイル
  • SYNOPSIS:bzip2 [OPTIONS]… FILE…
  • OPTIONS:
  • -d:解凍
  • -#:圧縮比を指定し、デフォルトは6で、数字範囲は1~9で、数字が大きいほど圧縮比が大きくなります.
  • -k:keep、元のファイル
  • を保持
      -k  ,        
    [root@localhost tmp]# ll -h
        236K
    -rw-------. 1 root root 236K 3   12 05:32 messages
    [root@localhost tmp]# bzip2 -k messages 
    [root@localhost tmp]# ll -h
        256K
    -rw-------. 1 root root 236K 3   12 05:32 messages
    -rw-------. 1 root root  20K 3   12 05:32 messages.bz2
    

    (2)bunzip 2:解凍
    (3)bzcat:圧縮ファイルの内容を解凍せずに表示する
    3、xz/unxz/xzcat:ほとんどbzip 2と同じ使用
    (1)xz:圧縮ファイル
  • SYNOPSIS:xz [OPTIONS]… FILE…
  • OPTIONS:
  • -d:解凍
  • -#:圧縮比を指定し、デフォルトは6で、数字範囲は1~9で、数字が大きいほど圧縮比が大きくなります.
  • -k:keep、元のファイル
  • を保持

    (2)unxz:解凍
    (3)xzcat:圧縮ファイルの内容を解凍せずに表示する
    二、アーカイブツールtar:
    多くの圧縮ツールでは、1つのファイルしか圧縮できません.複数のファイルを1つのファイルに圧縮することはできません.圧縮ディレクトリもサポートされていません.これにより,アーカイブツールの活用の場が得られる.アーカイブツールの役割は、複数のファイルまたはディレクトリを1つのファイルに統合することで、圧縮ツールを使用して圧縮できます.一般的なアーカイブツールはtar、cpioです.
    tarの使用:
    SYNOPSIS:tar [OPTION]… FILE…
    1、アーカイブの作成
    -cf/PATH/TO/SOMEFILE.tar FILE…
    [root@localhost tmp]# cp /var/log/messages  .
    [root@localhost tmp]# ls
    log  messages
    [root@localhost tmp]# tar -cf first.tar ./log ./messages 
    [root@localhost tmp]# ls
    first.tar  log  messages

    2、アーカイブの展開
    -xf/PATH/FROM/SHOMEFILE.tarアーカイブファイルを現在の作業ディレクトリの下に展開します.-xf/PATH/FROM/SHOMEFILE.tar-C/PATH/TO/SomeDIR指定ディレクトリの下にアーカイブファイルを展開する.
     [root@localhost tmp]# ls
    first.tar
                  :
    [root@localhost tmp]# tar -xf first.tar 
    [root@localhost tmp]# ls
    first.tar  log  messages
    [root@localhost tmp]# mkdir test
    [root@localhost tmp]# ls
    first.tar  log  messages  test
                 :
    [root@localhost tmp]# tar -xf first.tar -C ./test/
    [root@localhost tmp]# ls test
    log  messages

    3、アーカイブファイルのファイルリストを表示する
    -tf/PATH/TO/SOMEFILE.tar
    [root@localhost tmp]# ls
    first.tar  log  messages  test
               :
    [root@localhost tmp]# tar -tf first.tar 
    ./log/
    ./log/spice-vdagent.log
    ./log/wtmp
    ...      ...

    4、アーカイブして圧縮/解凍して展開する
  • -z:gzipの使用
  • -zcf/PATH/TO/SOMEFILE.tar.gz FILE...gzip圧縮
  • をアーカイブして使用
  • -zxf/PATH/TO/SOMEFILE.tar.gz解gzip圧縮および展開アーカイブ
  • [root@localhost tmp]# ls
    log
    [root@localhost tmp]# tar -zcf log1.tar.gz log
    [root@localhost tmp]# ls
    log  log1.tar.gz
    [root@localhost tmp]# tar -zxf log1.tar.gz 
    [root@localhost tmp]# ls
    log  log1.tar.gz
  • -j:アーカイブ後にbzip 2圧縮を使用し、-zオプションと同じ
  • を使用
  • -J:アーカイブ後にxz圧縮を使用し、-zオプションと同じ
  • を使用