Linux, コマンド · 1 min read · Sep 14, 2025

初心者のためのLinux bzip2コマンドチュートリアル(6つの例)

ファイル圧縮は特定のアルゴリズムに従って行われます。多くの圧縮技術があり、そのうちの1つはbzip2を通じて実現されます。このチュートリアルでは、いくつかのわかりやすい例を使ってbzip2の基本を学びます。この記事で使用されるすべての例は、Ubuntu 22.04LTSマシンでテストされています。

Linux bzip2コマンド

bzip2は、Burrows-Wheelerブロックソートテキスト圧縮アルゴリズムとハフマン符号化を使用して圧縮プロセスを実行するLinuxのコマンドラインベースのファイル圧縮ツールです。以下はその構文です:

bzip2 [OPTIONS] filenames ...

そして、以下はこのツールについてのmanページの説明です:

       bzip2  compresses  files  using  the Burrows-Wheeler block sorting text  
       compression algorithm, and Huffman coding.   Compression  is  generally  
       considerably   better   than   that   achieved   by  more  conventional  
       LZ77/LZ78-based compressors, and approaches the performance of the  PPM  
       family of statistical compressors.  
  
       The  command-line options are deliberately very similar to those of GNU  
       gzip, but they are not identical.  
  
       bzip2 expects a list of file names to accompany the command-line flags.  
       Each  file is replaced by a compressed version of itself, with the name  
       "original_name.bz2".  Each compressed file has  the  same  modification  
       date,  permissions,  and, when possible, ownership as the corresponding  
       original, so that these properties can be correctly restored at  decom?  
       pression  time.  File name handling is naive in the sense that there is  
       no mechanism for preserving original file  names,  permissions,  owner?  
       ships  or dates in filesystems which lack these concepts, or have seri?  
       ous file name length restrictions, such as MS-DOS.

以下は、bzip2コマンドの動作を理解するのに役立つQ&A形式の例です。

Q1. bzip2を使用してファイルを圧縮するには?

基本的な使用法は非常に簡単です - 圧縮するファイルをbzip2コマンドに入力として渡すだけです。以下はその例です:

bzip2 list.txt

以下のスクリーンショットは、コマンドの実行を示しています:

Q2. bzip2を使用して複数のファイルを圧縮するには?

簡単です - ファイル名を入力として渡すだけです。以下の例を参照してください:

bzip2 list.txt list1.txt list2.txt

Q3. bzip2を使用して解凍するには?

解凍するには、-dコマンドラインオプションを使用します。以下はその例です:

bzip2 -d list.txt.bz2

Q4. bzip2が入力ファイルを削除しないようにするには?

デフォルトでは、bzip2がファイルを圧縮すると、元の(または入力)ファイルが削除されます。しかし、それを避けたい場合は、-kコマンドラインオプションを使用します。

以下はその例です:

Q5. bzip2が各圧縮操作の詳細を表示するようにするには?

これは、-vコマンドラインオプションを使用することで実現できます。以下はmanページの説明です:

-v --verbose  
              Verbose  mode -- show the compression ratio for each file processed.  Further -v's   
              increase the verbosity level, spewing out lots of information which is primarily of   
              interest for diagnostic purposes.

以下は、-vを使用したときのbzip2コマンドの出力を示す例です:

Q6. 圧縮ファイルの整合性を確認するには?

bzip2コマンドは、.bz2ファイルの整合性を確認するためにも使用できます(ファイルが破損していないか、作成以来変更されていないかを確認するテスト)。これは、-tコマンドラインオプションを使用して行うことができます。

-t --test  
     Check  integrity of the specified file(s), but don't decompress them.    
     This really performs a trial decompression and throws away the result.

結論

bzip2コマンドラインユーティリティは、さらに多くのオプションを提供していますが、ここで説明した内容は、あなたが始めるのに十分です。このチュートリアルで説明したオプションの練習が終わったら、ツールのmanページにアクセスしてさらに学ぶことができます。

Share: X/Twitter LinkedIn

新しい投稿を受信箱で受け取る

スパムはありません。いつでも購読を解除できます。