了解 7z 命令开关（一）
7z
![](/data/attachment/album/201708/19/143244kypyp9rln99qyl9v.png)


7z 无疑是一个功能强大的强大的归档工具（声称提供最高的压缩比）。在 HowtoForge 中，我们已经[已经讨论过](https://www.howtoforge.com/tutorial/how-to-install-and-use-7zip-file-archiver-on-ubuntu-linux/)如何安装和使用它。但讨论仅限于你可以使用该工具提供的“功能字母”来使用基本功能。


在本教程中，我们将扩展对这个工具的说明，我们会讨论一些 7z 提供的“开关”。 但在继续之前，需要说明的是，本教程中提到的所有说明和命令都已在 Ubuntu 16.04 LTS 上进行了测试。


**注意**：我们将使用以下截图中显示的文件来执行使用 7zip 的各种操作。


 [![ls from test directory](/data/attachment/album/201708/19/143302xyqgjbbptpfwf3pu.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/ls.png) 


### 包含文件


7z 工具允许你有选择地将文件包含在归档中。可以使用 `-i` 开关来使用此功能。


语法：



```
-i[r[-|0]]{@listfile|!wildcard}

```

比如，如果你想在归档中只包含 “.txt” 文件，你可以使用下面的命令：



```
$ 7z a ‘-i!*.txt’ include.7z

```

这是输出：


 [![add files to 7zip](/data/attachment/album/201708/19/143304tyy5g74w77ypm5ah.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/include.png) 


现在，检查新创建的归档是否只包含 “.txt” 文件，你可以使用下面的命令：



```
$ 7z l include.7z

```

这是输出：


 [![Result](/data/attachment/album/201708/19/143305c0gbebnwsrbb0l1k.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/includelist.png) 


在上面的截图中，你可以看到 “testfile.txt” 已经包含到归档中了。


### 排除文件


如果你想要，你可以排除不想要的文件。可以使用 `-x` 开关做到。


语法：



```
-x[r[-|0]]]{@listfile|!wildcard}

```

比如，如果你想在要创建的归档中排除 “abc.7z” ，你可以使用下面的命令：



```
$ 7z a ‘-x!abc.7z’ exclude.7z

```

这是输出：


 [![exclude files from 7zip](/data/attachment/album/201708/19/143306hxsm44cls1vmc1md.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/exclude.png) 


要检查最后的归档是否排除了 “abc.7z”, 你可以使用下面的命令：



```
$ 7z l exclude.7z

```

这是输出：


 [![result of file exclusion](/data/attachment/album/201708/19/143309famooayyvjs0u1o0.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/excludelist.png) 


上面的截图中，你可以看到 “abc.7z” 已经从新的归档中排除了。


**专业提示**：假设任务是排除以 “t” 开头的所有 .7z 文件，并且包含以字母 “a” 开头的所有 .7z 文件。这可以通过以下方式组合 `-i` 和 `-x` 开关来实现：



```
$ 7z a '-x!t*.7z' '-i!a*.7z' combination.7z

```

### 设置归档密码


7z 同样也支持用密码保护你的归档文件。这个功能可以使用 `-p` 开关来实现。



```
$ 7z a [archive-filename] -p[your-password] -mhe=[on/off]

```

**注意**：`-mhe` 选项用来启用或者禁用归档头加密（默认是“off”）。


例子：



```
$ 7z a password.7z -pHTF -mhe=on

```

无需多说，当你解压密码保护的归档时，工具会向你询问密码。要解压一个密码保护的文件，使用 `e` 功能字母。下面是例子：



```
$ 7z e password.7z

```

 [![protect 7zip archive with a password](/data/attachment/album/201708/19/143311wspxbbnnpln572pp.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/password.png) 


### 设置输出目录


工具同样支持解压文件到你选择的目录中。这可以使用 `-o` 开关。无需多说，这个开关只在含有 `e` 或者 `x` 功能字母的时候有用。



```
$ 7z [e/x] [existing-archive-filename] -o[path-of-directory]

```

比如，假设下面命令工作在当前的工作目录中：



```
$ 7z e output.7z -ohow/to/forge

```

如 `-o` 开关的值所指的那样，它的目标是解压文件到 ./how/to/forge 中。


这是输出：


 [![7zip output directory](/data/attachment/album/201708/19/143315xpy5s5mysko8p484.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/output.png) 


在上面的截图中，你可以看到归档文件的所有内容都已经解压了。但是在哪里？要检查文件是否被解压到 ./how/to/forge，我们可以使用 `ls -R` 命令。


 [![result](/data/attachment/album/201708/19/143318au8pirl8oz8rte8l.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/ls_-R.png) 


在上面的截图中，我们可以看到 .7z 中的内容都被解压到 ./how/to/forge 中。


### 创建多个卷


借助 7z 工具，你可以为归档创建多个卷（较小的子档案）。当通过网络或 USB 传输大文件时，这是非常有用的。可以使用 `-v` 开关使用此功能。这个开关需要指定子档案的大小。


我们可以以字节（b）、千字节（k）、兆字节（m）和千兆字节（g）指定子档案大小。



```
$ 7z a [archive-filename] [files-to-archive] -v[size-of-sub-archive1] -v[size-of-sub-archive2] ....

```

让我们用一个例子来理解这个。请注意，我们将使用一个新的目录来执行 `-v` 开关的操作。


这是目录内容的截图：


 [![7zip volumes](/data/attachment/album/201708/19/143319xwdwkof1ks4sk0kd.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/volumels.png) 


现在，我们运行下面的命令来为一个归档文件创建多个卷（每个大小 100b）：



```
7z a volume.7z * -v100b

```

这是截图：


 [![compressing volumes](/data/attachment/album/201708/19/143322z0cdkdg275zpdezu.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/volume.png) 


现在，要查看创建的子归档，使用 `ls` 命令。


 [![list of archives](/data/attachment/album/201708/19/143324hz2um0i3wzbr2sbu.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/volumels2.png) 


如下截图所示，一共创建四个卷 - volume.7z.001、volume.7z.002、volume.7z.003 和 volume.7z.004


**注意**：你可以使用 .7z.001 归档文件来解压。但是，要这么做，其他所有的卷都应该在同一个目录内。


### 设置归档的压缩级别


7z 允许你设置归档的压缩级别。这个功能可以使用 `-m` 开关。7z 中有不同的压缩级别，比如：`-mx0`、`-mx1`、`-mx3`、`-mx5`、`-mx7` 和 `-mx9`。


这是这些压缩级别的简要说明：


* `mx0` = 完全不压缩 - 只是复制文件到归档中。
* `mx1` = 消耗最少时间，但是压缩最小。
* `mx3` = 比 `-mx1` 好。
* `mx5` = 这是默认级别 （常规压缩）。
* `mx7` = 最大化压缩。
* `mx9` = 极端压缩。


**注意**：关于这些压缩级别的更多信息，阅读[这里](http://askubuntu.com/questions/491223/7z-ultra-settings-for-zip-format)。



```
$ 7z a [archive-filename] [files-to-archive] -mx=[0,1,3,5,7,9]

```

例如，我们在目录中有一堆文件和文件夹，我们每次尝试使用不同的压缩级别进行压缩。作为一个例子，这是当使用压缩级别 “0” 时创建存档时使用的命令。



```
$ 7z a compression(-mx0).7z * -mx=0

```

相似地，其他命令也这样执行。


以下是输出档案（使用 “ls” 命令生成）的列表，其名称表示其创建中使用的压缩级别，输出中的第五列显示压缩级别对其大小的影响。


 [![7zip compression level](/data/attachment/album/201708/19/143326b7l7pzmm1lxx7w0j.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/compression.png) 


### 显示归档的技术信息


如果需要，7z 还可以在标准输出中显示归档的技术信息 - 类型、物理大小、头大小等。可以使用 `-slt` 开关使用此功能。 此开关仅适用于带有 `l` 功能字母的情况下。



```
$ 7z l -slt [archive-filename]

```

比如：



```
$ 7z l -slt abc.7z

```

这是输出：


 [![](/data/attachment/album/201708/19/143328p2gkd0z2vtnok0s2.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/slt.png) 


### 指定创建归档的类型


如果你想要创建一个非 7z 的归档文件（这是默认的创建类型），你可以使用 `-t` 开关来指定。



```
$ 7z a -t[specify-type-of-archive] [archive-filename] [file-to-archive]

```

下面的例子展示创建了一个 .zip 文件：



```
7z a -tzip howtoforge *

```

输出的文件是 “howtoforge.zip”。要交叉验证它的类型，使用 `file` 命令：


 [![](/data/attachment/album/201708/19/143329hoq7skkmo1k7o8mn.png)](https://www.howtoforge.com/images/understanding_7z_command_switches_part_i/big/type.png) 


因此，howtoforge.zip 的确是一个 ZIP 文件。相似地，你可以创建其他 7z 支持的归档。


### 总结


你将会认识到， 7z 的 “功能字母” 以及 “开关” 的知识可以让你充分利用这个工具。我们还没有完成开关的部分 - 其余部分将在第 2 部分中讨论。




---


via: <https://www.howtoforge.com/tutorial/understanding-7z-command-switches/>


作者：[Himanshu Arora](https://www.howtoforge.com/tutorial/understanding-7z-command-switches/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


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


OK
# Understanding 7z command switches - part I

7z is no doubt a feature-rich and powerful archiver (claimed to offer the highest compression ratio). Here at HowtoForge, we have [already discussed](https://www.howtoforge.com/tutorial/how-to-install-and-use-7zip-file-archiver-on-ubuntu-linux/) how you can install and use it. But the discussion was limited to basic features that you can access using the 'function letters' the tool provides.

Expanding our coverage on the tool, here in this tutorial, we will be discussing some of the 'switches' 7z offers. But before we proceed, it's worth sharing that all the instructions and commands mentioned in this tutorial have been tested on Ubuntu 16.04 LTS.

**Note**: We will be using the files displayed in the following screenshot for performing various operations using 7zip.


Include files

The 7z tool allows you selectively include files in an archive. This feature can be accessed using the *-i* switch.

Syntax:

-i[r[-|0]]{@listfile|!wildcard}

For example, if you want to include only ‘.txt’ files in your archive, you can use the following command:

$ 7z a ‘-i!*.txt’ include.7z

Here is the output:


Now, to check whether the newly-created archive file contains only ‘.txt’ file or not, you can use the following command:

$ 7z l include.7z

Here is the output:


In the above screenshot, you can see that only ‘testfile.txt’ file has been added to the archive.

## Exclude files

If you want, you can also exclude the files that you don’t need. This can be done using the *-x* switch.

Syntax:

-x[r[-|0]]]{@listfile|!wildcard}

For example, if you want to exclude a file named ‘abc.7z’ from the archive that you are going to create, then you can use the following command:

$ 7z a ‘-x!abc.7z’ exclude.7z

Here is the output:


To check whether the resulting archive file has excluded ‘abc.7z’ or not, you can use the following command:

$ 7z l exclude.7z

Here is the output:

In the above screenshot, you can see that ‘abc.7z’ file has been excluded from the new archive file.

**Pro tip**: Suppose the task is to exclude all the .7z files with names starting with letter ‘t’ and include all .7z files with names starting with letter ‘a’ . This can be done by combining both ‘-i’ and ‘-x’ switches in the following way:

$ 7z a '-x!t*.7z' '-i!a*.7z' combination.7z

## Set password for your archive

7z also lets you password protect your archive file. This feature can be accessed using the *-p* switch.

$ 7z a [archive-filename] -p[your-password] -mhe=[on/off]

**Note**: *The -mhe option enables or disables archive header encryption (default is off)*.

For example:

$ 7z a password.7z -pHTF -mhe=on

Needless to say, when you will extract your password protected archive, the tool will ask you for the password. To extract a password-protected file, use the 'e' function letter. Following is an example:

$ 7z e password.7z

## Set output directory

The tool also lets you extract an archive file in the directory of your choice. This can be done using the *-o* switch. Needless to say, the switch only works when the command contains either the ‘e’ function letter or the ‘x’ function letter.

$ 7z [e/x] [existing-archive-filename] -o[path-of-directory]

For example, suppose the following command is run in the present working directory:

$ 7z e output.7z -ohow/to/forge

And, as the value passed to the *-o* switch suggests, the aim is to extract the archive in the *./how/to/forge* directory.

Here is the output:

In the above screenshot, you can see that all the contents of existing archive file has been extracted. But where? To check whether or not the archive file has been extracted in the *./how/to/forge* directory or not, we can use the ‘ls -R’ command.


In the above screenshot, we can see that all the contents of output.7z have indeed been extracted to ./how/to/forge.

## Creating multiple volumes

With the help of the 7z tool, you can create multiple volumes (smaller sub-archives) of your archive file. This is very useful when transferring large files over a network or in a USB. This feature can be accessed using the *-v* switch. The switch requires you to specify size of sub-archives.

We can specify size of sub-archives in bytes (b), kilobytes (k), megabytes (m) and gigabytes (g).

$ 7z a [archive-filename] [files-to-archive] -v[size-of-sub-archive1] -v[size-of-sub-archive2] ....

Let's understand this using an example. Please note that we will be using a new directory for performing operations on the *-v* switch.

Here is the screenshot of the directory contents:

Now, we can run the following command for creating multiple volumes (sized 100b each) of an archive file:

7z a volume.7z * -v100b

Here is the screenshot:

Now, to see the list of sub-archives that were created, use the ‘ls’ command.

As seen in the above screenshot, a total of four multiple volumes have been created - volume.7z.001, volume.7z.002, volume.7z.003, and volume.7z.004

**Note**: You can extract files using the .7z.001 archive. But, for that, all the other sub-archive volumes should be present in the same directory.

## Set compression level of archive

7z also allows you to set compression levels of your archives. This feature can be accessed using the *-m* switch. There are various compression levels in 7z, such as -mx0, -mx1, -mx3, -mx5, -mx7 and -mx9.

Here's a brief summary about these levels:

-**mx0** = Don't compress at all - just copy the contents to archive.

-**mx1** = Consumes least time, but compression is low.

-**mx3** = Better than -mx1.

-**mx5** = This is default (compression is normal).

-**mx7** = Maximum compression.

-**mx9** = Ultra compression.

**Note**: For more information on these compression levels, head [here](http://askubuntu.com/questions/491223/7z-ultra-settings-for-zip-format).

$ 7z a [archive-filename] [files-to-archive] -mx=[0,1,3,5,7,9]

For example, we have a bunch of files and folders in a directory, which we tried compressing using a different compression level each time. Just to give you an idea, here's the command used when the archive was created with compression level '0'.

$ 7z a compression(-mx0).7z * -mx=0

Similarly, other commands were executed.

Here is the list of output archives (produced using the 'ls' command), with their names suggesting the compression level used in their creation, and the fifth column in the output revealing the effect of compression level on their size.


Display technical information of archive

If you want, 7z also lets you display technical information of an archive - it's type, physical size, header size, and so on - on the standard output. This feature can be accessed using the *-slt* switch. This switch only works with the ‘l’ function letter.

$ 7z l -slt [archive-filename]

For example:

$ 7z l -slt abc.7z

Here is the output:

# Specify type of archive to create

If you want to create a non 7zip archive (which gets created by default), you can specify your choice using the *-t* switch.

$ 7z a -t[specify-type-of-archive] [archive-filename] [file-to-archive]

The following example shows a command to create a .zip file:

7z a -tzip howtoforge *

The output file produced is 'howtoforge.zip'. To cross verify its type, use the 'file' command:

So, howtoforge.zip is indeed a ZIP file. Similarly, you can create other kind of archives that 7z supports.

# Conclusion

As you would agree, the knowledge of 7z 'function letters' along with 'switches' lets you make the most out of the tool. We aren't yet done with switches - there are some more that will be discussed in part 2.