Use of Linux bzip2 Command

  • 2021-08-28 21:40:20
  • OfStack

1. Brief introduction to commands

bzip2 is used to compress and decompress files. It is a command that is often used in Linux system to compress and decompress files. Burrow-Wheeler block sorting text compression algorithm and Huffman coding are used to compress files into bzip2 files with suffix. bz2. Compression ratio 1 is generally much better than the compression software based on LZ77/LZ78, and its performance is close to that of PPM statistical compression software.

bzip2 can not only be used to compress large, less-used files to save disk space, but can also be used with tar command 1 to complete the packaging and compression of files. Reducing file size has two obvious benefits: 1. It can reduce storage space, and 2. It can reduce the transfer time when transferring files over the network.

2. Command format


bzip2 [OPTIONS] [FILES...]

The bzip2 command-line parameters are intentionally designed to be similar to, but not identical to, GNU and gzip. bzip2 reads the entry and file name from the command line. Each file is replaced by a compressed file named "Original Filename. bz2". Each compressed file has the same modification time, permissions and, if possible, the same owner as the original file, so these features will be restored correctly when uncompressed.

bzip2 does not overwrite existing files by default. If you want to overwrite an existing file, specify the-f option.

3. Description of options


-c, --stdout
  将数据压缩或解压缩输出至标准输出
-d, --decompress
  强制解压缩。 bzip2, bunzip2 以及 bzcat 实际上是同1个程序,进行何种操作将根据程序名确定。 指定该选项后将不考虑这1机制,强制 bzip2 进行解压缩
-z, --compress
	-d 选项的补充:强制进行压缩操作,而不管执行的是哪个程序
-t, --test
  检查指定文件的完整性,但并不对其解压缩。 实际上将对数据进行实验性的解压缩操作,而不输出结果
-f, -force
  强制覆盖输出文件。通常 bzip2 不会覆盖已经存在的文件。该选项还强制 bzip2 打破文件的硬连接,缺省情况下 bzip2 不会这么做。
-k, --keep
  在压缩或解压缩时保留输入文件(不删除这些文件)
-s, --small
  在压缩、解压缩及检查时减少内存用量。 采用1种修正的算法进行压缩和测试, 每个数据块仅需要 2.5 个字节。 这意味着任何文件都可以在 2300K 的内存中进行解压缩, 尽管速度只有通常情况下的1半。在压缩时,-s 将选定 200K 的块长度,内存用量也限制在 200K 左右, 代价是压缩率会降低。 总之,如果机器的内存较少(8MB 或更少), 可对所有操作都采用 -s 选项
-q, --quiet
  压制不重要的警告信息。属于 I/O 错误及其它严重事件的信息将不会被压制
-v, --verbose
  详尽模式――显示每个被处理文件的压缩率。 命令行中更多的 -v 选项将增加详细的程度, 使 bzip2 显示出许多主要用于诊断目的信息
-L, --license, -V, --version
	显示显示软件版本、许可证条款及分发条件
-1 (or --fast) to -9 (or --best)
  在压缩时将块长度设为 100 k、200 k ... 900 k。 对解压缩没有影响
--
  将所有后面的命令行变量看作文件名,即使这些变量以减号 - 打头。 可用这1选项处理以减号 - 打头的文件名, 例如:bzip2 -- -myfilename
--repetitive-fast, --repetitive-best
  这些选项在 0.9.5 及其以上版本中是多余的。 在较早的版本中,这两个选项对排序算法的行为提供了1些粗糙的控制,有些情况下很有用。 0.9.5 及其以上版本采用了改进的算法而与这些选项无关

4. Common examples

(1) Do not keep the original file compressed.


bzip2 /etc/passwd

After compression, the/etc/passwd becomes the/etc/passwd. bz2.

(2) Keep the original file compressed.


bzip2 -k /etc/passwd
#  Or 
bzip2 -c /etc/passwd > /etc/passwd.bz2

(3) Display the instruction execution process when compressing.


bzip2 -v /etc/passwd
/etc/passwd: 2.256:1, 3.546 bits/byte, 55.67% saved, 1552 in, 688 out.

(4) Unzip the. bz2 file without retaining the original file.


bzip2 -d /etc/passwd.bz2

(5) Extract the. bz2 file and keep the original file.


bzip2 -dk /etc/passwd.bz2
#  Or 
bzip2 -dc /etc/passwd.bz2 > /etc/passwd

(6) Tests the integrity of the. bz2 compressed file without actually extracting it.


bzip2 -tv /etc/passwd.bz2
/etc/passwd.bz2: ok

The above is the use of Linux bzip2 command details, more information about Linux bzip2 command please pay attention to other related articles on this site!


Related articles: