本页内容
- Linux gunzip 命令
- Q1. 如何使用 gunzip 解压归档文件?
- Q2. 如何让 gunzip 不删除归档文件?
- Q3. 如何让 gunzip 把解压后的文件放到其他目录?
- 更多信息
- 结论
我们已经讨论过 Linux 中的 gzip 命令。首先,该工具用于压缩或展开文件。要解压,该命令提供了一个命令行选项 -d,可以按如下方式使用:
gzip -d [compressed-file-name]
不过,还有一个完全不同的工具可用于解压或展开由 gzip 创建的归档文件。这个工具就是 gunzip。在本文中,我们将通过一些易于理解的示例来讨论 gunzip 命令。本教程中提到的所有示例/说明均已在 Ubuntu 22.04 上测试过。
Linux gunzip 命令
现在我们知道,压缩文件可以用 'gzip -d' 或 gunzip 命令来还原。gunzip 的基本语法是:
gunzip [compressed-file-name]
下面这些问答式示例应该能让你更好地了解该工具的工作原理:
Q1. 如何使用 gunzip 解压归档文件?
这非常简单——只需把归档文件的名称作为参数传给 gunzip 即可。
gunzip [archive-name]
例如:
gunzip file1.gz

Q2. 如何让 gunzip 不删除归档文件?
你可能已经注意到,gunzip 命令在解压后会删除归档文件。不过,如果希望保留归档文件,可以使用 -c 命令行选项来实现。
gunzip -c [archive-name] > [outputfile-name]
例如:
gunzip -c file1.gz > file1

可以看到,在这种情况下归档文件没有被删除。
Q3. 如何让 gunzip 把解压后的文件放到其他目录?
我们在上一个问答中已经讨论过 -c 选项。要让 gunzip 把解压后的文件放到当前工作目录之外的目录中,只需在重定向运算符之后提供绝对路径即可。
gunzip -c [compressed-file] > [/complete/path/to/dest/dir/filename]
这里有一个示例:
gunzip -c file1.gz > /home/himanshu/file1
更多信息
以下细节摘自 gzip/gunzip 共用的 man 手册页,对那些想进一步了解该命令的人应该会有所帮助:
gunzip takes a list of files on its command line and replaces each file whose name ends with .gz, -gz, .z, -z, or _z (ignoring case) and which begins with the correct magic number with an uncompressed file without the original extension. gunzip also recognizes the special extensions .tgz and .taz as shorthands for .tar.gz and .tar.Z respectively. When compressing, gzip uses the .tgz extension if necessary instead of trun cating a file with a .tar extension. gunzip can currently decompress files created by gzip, zip, compress, compress -H or pack. The detection of the input format is automatic. When using the first two formats, gunzip checks a 32 bit CRC. For pack and gunzip checks the uncompressed length. The standard compress format was not designed to allow consistency checks. However gunzip is some times able to detect a bad .Z file. If you get an error when uncom pressing a .Z file, do not assume that the .Z file is correct simply because the standard uncompress does not complain. This generally means that the standard uncompress does not check its input, and happily gen erates garbage output. The SCO compress -H format (lzh compression method) does not include a CRC but also allows some consistency checks.
结论
就基本用法而言,Gunzip 并没有太大的学习曲线。我们已经涵盖了初学者开始使用该命令前需要了解的几乎所有内容。欲了解更多信息,请查阅它的 man 手册页。