用示例讲解 Linux gunzip 命令
gzip gunzip
![](/data/attachment/album/201804/15/212640jqqkkiqqnonu2fqm.jpg)


我们已经讨论过 [Linux 下 gzip 命令的用法](https://www.howtoforge.com/linux-gzip-command/)。对于初学者来说，`gzip` 工具主要用于压缩或者展开文件。解压时，在 `gzip` 命令后添加 `-d` 选项即可，使用示例如下：



```
gzip -d [compressed-file-name]

```

不过，在解压或扩展 gzip 创建的压缩文件时，有另一款完全不同的工具可供使用。谈及的这款工具就是 `gunzip`。在本文中，我们会使用一些简单、易于理解的例子来解释 `gunzip` 命令的用法。文中所有示例及指南都在 Ubuntu 16.04 环境下测试。


### Linux gunzip 命令


我们现在知道压缩文件可以用 `gzip -d` 或 `gunzip` 命令解压。基本的 `gunzip` 语法为：



```
gunzip [compressed-file-name]

```

以下的 Q&A 例子将更清晰地展示 `gunzip` 工具如何工作：


### Q1. 如何使用 gunzip 解压压缩文件？


解压命令非常简单，仅仅需要将压缩文件名称作为参数传递到 `gunzip` 命令后。



```
gunzip [archive-name]

```

比如：



```
gunzip file1.gz

```

[![如何使用 gunzip 解压压缩文件？](/data/attachment/album/201804/15/212653typvantzntmvpptj.png)](https://www.howtoforge.com/images/linux_gunzip_command/big/gunzip-basic-usage.png)


### Q2. 如何让 gunzip 不删除原始压缩文件？


正如你已注意到的那样，`gunzip` 命令解压后会删除原始压缩文件。如果你想保留原始压缩文件，可以使用 `-c` 选项。



```
gunzip -c [archive-name] > [outputfile-name]

```

比如：



```
gunzip -c file1.gz > file1

```

[![如何让 gunzip 不删除原始压缩文件？](/data/attachment/album/201804/15/212654mab33srbbs7r71s7.png)](https://www.howtoforge.com/images/linux_gunzip_command/big/gunzip-c.png)


使用这种方式，原压缩文件不会被删除。


### Q3. 如何用 gunzip 解压文件到其他路径？


在 Q&A 中我们已经讨论过 `-c` 选项的用法。 使用 gunzip 解压文件到工作目录外的其他路径，仅需要在重定向操作符后添加目标目录的绝对路径即可。



```
gunzip -c [compressed-file] > [/complete/path/to/dest/dir/filename]

```

示例如下：



```
gunzip -c file1.gz > /home/himanshu/file1

```

### 更多信息


以下从 gzip/gunzip 的 man 页中摘录的细节，对于想了解更多的人会有所助益。



> 
> `gunzip` 在命令行接受一系列的文件，并且将每个文件内容以正确的魔法数开始，且后缀名为 `.gz`、`-gz`、`.z`、`-z` 或 `_z` （忽略大小写）的压缩文件，用未压缩的文件替换它，并删除其原扩展名。 `gunzip` 也可识别一些特殊扩展名的压缩文件，如 `.tgz` 和 `.taz` 分别是 `.tar.gz` 和 `.tar.Z` 的缩写。在压缩时，`gzip` 在必要情况下使用 `.tgz` 作为扩展名，而不是只截取掉 `.tar` 后缀。
> 
> 
> `gunzip` 目前可以解压 `gzip`、`zip`、`compress`、`compress -H`（`pack`）产生的文件。`gunzip` 自动检测输入文件格式。在使用前两种压缩格式时，`gunzip` 会检验 32 位循环冗余校验码（CRC）。对于 pack 包，`gunzip` 会检验压缩长度。标准压缩格式在设计上不允许相容性检测。不过 `gunzip` 有时可以检测出坏的 `.Z` 文件。如果你解压 `.Z` 文件时出错，不要因为标准解压没报错就认为 `.Z` 文件一定是正确的。这通常意味着标准解压过程不检测它的输入，而是直接产生一个错误的输出。SCO 的 `compress -H` 格式（lzh 压缩方法）不包括 CRC 校验码，但也允许一些相容性检查。
> 
> 
> 


### 结语


到目前为止提到的 `gunzip` 基本用法，并不需要过多的学习曲线。我们已包含了一个初学者开始使用它所必须了解的几乎全部知识。想要了解更多的用法，去看 [man 页面](https://linux.die.net/man/1/gzip) 吧。




---


via: <https://www.howtoforge.com/linux-gunzip-command/>


作者：[Himanshu Arora](https://www.howtoforge.com) 译者：[erialin](https://github.com/erialin) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Linux Gunzip Command Explained with Examples

We have [already discussed](https://www.howtoforge.com/linux-gzip-command/) the **gzip** command in Linux. For starters, the tool is used to compress or expand files. To uncompress, the command offers a command line option **-d**, which can be used in the following way:

gzip -d [compressed-file-name]

However, there's an entirely different tool that you can use for uncompressing or expanding archives created by gzip. The tool in question is **gunzip**. In this article, we will discuss the gunzip command using some easy-to-understand examples. All examples/instructions mentioned in the tutorial have been tested on Ubuntu 22.04.

## Linux gunzip command

So now we know that compressed files can be restored using either 'gzip -d' or the gunzip command. The basic syntax of gunzip is:

gunzip [compressed-file-name]

The following Q&A-style examples should give you a better idea of how the tool works:

## Q1. How to uncompress archives using gunzip?

This is very simple - just pass the name of the archive file as an argument to gunzip.

gunzip [archive-name]

For example:

gunzip file1.gz

## Q2. How to make gunzip not delete the archive file?

As you'd have noticed, the gunzip command deletes the archive file after uncompressing it. However, if you want the archive to stay, you can do that using the **-c** command line option.

gunzip -c [archive-name] > [outputfile-name]

For example:

gunzip -c file1.gz > file1

So you can see that the archive file wasn't deleted in this case.

## Q3. How to make gunzip put the uncompressed file in some other directory?

We've already discussed the **-c** option in the previous Q&A. To make gunzip put the uncompressed file in a directory other than the present working directory, just provide the absolute path after the redirection operator.

gunzip -c [compressed-file] > [/complete/path/to/dest/dir/filename]

Here's an example:

gunzip -c file1.gz > /home/himanshu/file1

## More info

The following details - taken from the common manpage of gzip/gunzip - should be beneficial for those who want to know more about the command:

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.

## Conclusion

As far as basic usage is concerned, there isn't much of a learning curve associated with Gunzip. We've covered pretty much everything that a beginner needs to learn about this command in order to start using it. For more information, head to its [man page](https://linux.die.net/man/1/gzip).