7zip is a popular tool and compression format. 7zip supports a wide range of platforms and operations systems like Windows, Linux, BSD, macOS, etc.
7zip是一种流行的工具和压缩格式。 7zip支持各种平台和操作系统,例如Windows,Linux,BSD,macOS等。
句法 (Syntax)
We will use following syntax for 7z
command.
我们将对7z
命令使用以下语法。
7z <command> [<switches>...] <archive_name> [<file_names>...]
帮帮我 (Help)
First before starting using the 7z command we can get usage and help information by using -h
option. We can see that different options like adding file to archive, benchmark, delete files from archive etc. listed with the -h option output.
首先,在开始使用7z命令之前,我们可以使用-h
选项获取使用情况和帮助信息。 我们可以看到-h选项输出列出了不同的选项,例如将文件添加到归档文件,进行基准测试,从归档文件中删除文件等。
$ 7z -h
Alternatively, we can list usage and help information without providing any option to the 7z command like below.
另外,我们可以列出用法和帮助信息,而无需为7z命令提供任何选项,如下所示。
$ 7z
7z命令(7z Commands)
While using 7z there is two-component as we can see in the syntax. One of them is commands. Commands are used to specify the operations like:
使用7z时,我们可以从语法中看到两个组成部分。 其中之一是命令。 命令用于指定类似的操作:
a
: Add files to archivea
:将文件添加到存档b
: Benchmarkb
:基准d
: Delete files from archived
:从存档中删除文件e
: Extract files from archive (without using directory names)e
:从存档中提取文件(不使用目录名)h
: Calculate hash values for filesh
:计算文件的哈希值i
: Show information about supported formatsi
:显示有关支持的格式的信息l
: List contents of archivel
:列出档案内容rn
: Rename files in archivern
:重命名存档中的文件t
: Test integrity of archivet
:测试档案的完整性u
: Update files to archiveu
:更新文件以存档x
: eXtract files with full pathsx
:具有完整路径的eXtract文件
开关(Switches)
Switches are used to specify some minor options like create SFX archive, compress shared files, set output log level. Here are some of them
开关用于指定一些次要选项,例如创建SFX存档,压缩共享文件,设置输出日志级别。 这里是其中的一些
--
: Stop switches parsing--
:停止开关解析-ai[r[-|0]]{@listfile|!wildcard}
: Include archives-ai[r[-|0]]{@listfile|!wildcard}
:包括存档-ax[r[-|0]]{@listfile|!wildcard}
: eXclude archives-ax[r[-|0]]{@listfile|!wildcard}
:排除档案–
ao{a|s|t|u}
: set Overwrite mode–
ao{a|s|t|u}
:设置覆盖模式-an
: disable archive_name field-an
:禁用archive_name字段
压缩文件和文件夹(Compress Files and Folders)
7z can compress files and folders. We will use a
command to create a new archive. The new archive file name will be mybackup.7z
and we will put files named a.out
, body.txt
and folder cups
. Here the
7z可以压缩文件和文件夹。 我们将使用a
命令来创建一个新的存档。 新的存档文件名为mybackup.7z
,我们将放置名为a.out
, body.txt
和文件夹cups
文件。 在这里
$ 7z a mybackups.7z a.out body.txt cups
Here while starting 7z applications show some information about the 7z application like build number, version, developer. And then provided files and folders are scanned and the scan result is printed like 56 folders, 1550 files, and a total 142 MB. After compression is completed new archive size is printed as 96 MB.
在启动7z应用程序时,此处显示有关7z应用程序的一些信息,例如内部版本号,版本,开发人员。 然后扫描提供的文件和文件夹,并打印扫描结果,例如56个文件夹,1550个文件和总共142 MB。 压缩完成后,新的存档大小将打印为96 MB。
将文件添加到存档 (Add Files To The Archive)
In previous example we have created the archive file. Some time we need to add new files to the existing archive file. In this situations we will use same a
option to add files.
在前面的示例中,我们创建了存档文件。 一段时间后,我们需要将新文件添加到现有存档文件中。 在这种情况下,我们将使用相同a
选项来添加文件。
$ 7z a mybackups.7z fruits.txt
In this operation mybackup.7z
archive file already exists. Some information about the file is printed like path, compression type, size, etc. Then a new file is added to the existing archive or compressed file named mybackup.7z
.
在此操作中, mybackup.7z
存档文件已存在。 打印有关文件的一些信息,例如路径,压缩类型,大小等。然后,将新文件添加到名为mybackup.7z
的现有存档或压缩文件中。
从存档中删除文件 (Delete Files From Archive)
Another useful operation is removing one or more files from an existing archive file. We will use d
command with the file we want to delete. In this example, we want to remove the file named fruits.txt from the archive file mybackups.7z
.
另一项有用的操作是从现有存档文件中删除一个或多个文件。 我们将对要删除的文件使用d
命令。 在此示例中,我们要从存档文件mybackups.7z
删除名为fruits.txt的文件。
$ 7z d mybackups.7z fruits.txt
从存档/压缩文件中提取文件(Extract Files From The Archive/Compressed File)
We can extract all files from an archive or compressed file by using x
command or option. We will also provide the archive or compressed file. In this example, we will extract the mybackups.7z .
我们可以使用x
命令或选项从存档文件或压缩文件中提取所有文件。 我们还将提供存档或压缩文件。 在此示例中,我们将提取mybackups.7z。
$ 7z x mybackups.7z
As we can see from the screenshot there are some existing files which will be overwritten. There question whether we want to overwrite, skip, rename, etc. We answer this as Always. After extraction, some statistical information like total folder count, file count size, compressed size is printed.
正如我们从屏幕快照中看到的,有一些现有文件将被覆盖。 有人问我们是否要覆盖,跳过,重命名等。我们将其回答为始终。 提取后,将打印一些统计信息,例如文件夹总数,文件计数大小,压缩大小。
列出档案内容 (List Contents of Archive)
We can also list files of an archive with l command. We will list all files of mybackup.7z
archive.
我们还可以使用l命令列出档案文件。 我们将列出mybackup.7z
存档的所有文件。
$ 7z l mybackups.7z
In the output, we can see that files and folders are listed. While listing information like date, time, attributes, size, and name are provided.
在输出中,我们可以看到列出了文件和文件夹。 在列出信息时,会提供日期,时间,属性,大小和名称等信息。
翻译自: https://www.poftut.com/7z-command-tutorial-examples-compress-extract-files-linux/