在本文中,我们将学习如何使用 bz2 文件压缩工具(bzip2 linux 命令)压缩文件和解压缩 bz2 压缩文件。
bzip2 是一个开源压缩工具,类似于 zip 和 gzip 压缩工具,用于压缩大型文件以减小其大小。
bzip2 可以压缩文件而不是目录。
与 gzip 和 zip 相比,bzip2 提供了更多的压缩。
bzip2 Linux 命令由 Julian Seward 于 1996 年开发,并在 BSD 风格许可下发布。
我们可以使用 bz2 文件压缩工具(bzip2 linux 命令)来压缩文件以减小文件的大小。
bz2 文件压缩工具的一些主要功能包括:
- 它和跨平台应用程序可用于主要操作系统,例如:Linux、Microsoft Windows、MacOS。
- 可以从损坏的 bz2 文件中恢复数据。
- 适用于 32 位和 64 位操作系统。
- 可以创建文件的快速和最佳压缩。
现在让我们通过示例来看看 bzip2 Linux 命令:
压缩文件
使用 bzip2 Linux 命令压缩文件。
$ bzip2 myfile.txt # Compress a File
输出:
$ ls myfile.txt.bz2
使用标准输出压缩文件
带有 -c 的 bzip2 命令将使用标准输出压缩文件。
$ bzip2 -c myfile.txt > myfile.txt.bz2 # Compress a file with standard output
输出:
$ ls myfile.txt myfile.txt.bz2
通过保留输入文件(源文件)来压缩文件
通常 bzip2 命令会压缩文件并删除源文件,但带有参数 -k 的 bzip2 命令将通过保持源文件不被删除来压缩文件。
$ bzip2 -k myfile.txt # Compress file without delete Input file
输出:
$ ls myfile.txt myfile.txt.bz2
一次压缩多个文件
bz2 文件压缩工具(bzip2 linux 命令)可以一次压缩多个文件。
为此,请使用以下命令。
$ bzip2 myfile.txt myfile1.txt myfile2.txt myfile3.txt # Compress multiple files at once
输出:
$ ls myfile1.txt.bz2 myfile2.txt.bz2 myfile3.txt.bz2 myfile.txt.bz2
检查指定文件的完整性
带有参数 -t 的 bzip2 linux 命令检查指定 bz2 文件的完整性。
bz2 文件是否为有效文件的完整性。
我们可以使用以下命令执行此操作。
如果文件有效,那么我们将不会得到任何输出。
$ bzip2 -t myfile.txt.bz2 # Check the Integrity of the bz2 file
但如果 bz2 文件不是有效文件,我们将收到错误消息。
为了证明这一点,让我们使用 touch 命令创建一个 bzip2 文件,然后检查完整性。
$ touch myfile.txt.bz2
$ bzip2 -t myfile.txt.bz2 bzip2: myfile.txt.bz2: file ends unexpectedly
我们可以使用“bzip2recover”程序尝试从损坏文件的未损坏部分恢复数据。
正如你在上面看到的,我们得到了错误,因为我们没有使用 bzip2 linux 命令创建这个文件,因此它不是一个有效的 bz2 文件。
解压/解压/解压 bz2 文件
带参数 -d 的 bzip2 命令将解压缩 bzip2 文件。
$ bzip2 -d myfile.txt.bz2 # Uncompress a file
输出:
$ ls myfile.txt
我们也可以使用 bunzip2 命令解压 bz2 文件。
参考下面的命令。
$ bunzip2 myfile.txt.bz2
输出:
$ ls myfile.txt
使用 bzcat 命令检查 bz2 压缩文件的内容
我们可以在不解压缩压缩文件的情况下检查其内容。
为此,我们必须使用 bzcat 命令。
参考下面的命令。
$ bzcat myfile.txt.bz2 # Check content of a Compressed file Welcome to onitroad.com
强制压缩文件
带参数 -f 的 bzip2 命令将强制创建一个 bz2 文件。
参考下面的命令。
$ bzip2 -f myfile.txt # Compress a file forcefully
$ ls myfile.txt.bz2
用bz2文件压缩工具压缩目录
通过使用唯一的 bzip2 linux 命令,我们无法压缩目录,但带有 tar 命令的 bzip2 命令可以压缩目录。
使用以下命令执行相同操作。
$ tar -cjvf data.tar.bz2 data/ # Compress a Directory data/ data/myfile3.txt data/myfile2.txt data/myfile1.txt
输出:
$ ls data data.tar.bz2
这里的参数 -j 用于 bzip2 压缩。
压缩级别
每个压缩工具都有 9 级压缩可用。
级别为 1,2…9.
在这里,将介绍两个级别。
例如:Level 1 & Level 9.
级别 1 提供快速压缩输出。
参考下面的命令。
$ bzip2 -1 myfile.txt # 1 for Fast Compression
级别 9 提供最佳压缩输出。
参考下面的命令。
$ bzip2 -9 myfile.txt # 9 for Best Compression
检查 bzip2 Linux 命令的许可证和包版本
使用带参数 -L 的 bzip2 命令检查 bz2 文件压缩工具的许可证和包版本。
$ bzip2 -L # Check Version & License bzip2, a block-sorting file compressor. Version 1.0.6, 6-Sept-2010. Copyright (C) 1996-2010 by Julian Seward. This program is free software; you can redistribute it and/or modify it under the terms set out in the LICENSE file, which is included in the bzip2-1.0.6 source distribution. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE file for more details.
有关 bzip2 Linux 命令的更多帮助和信息,我们可以使用以下命令。
$ man bzip2 # Access bzip2 bananaal page