zip unzip命令行
In this guide, we will focus on zip and unzip commands in Linux. zip command is a utility commonly used to compress files, while unzip command is used for uncompressing or unzipping files. Let’s have a look at each of these commands in detail.
在本指南中,我们将重点介绍Linux中的zip和unzip命令。 zip命令是通常用于压缩文件的实用程序,而unzip命令用于解压缩或解压缩文件。 让我们详细了解每个命令。
使用zip命令压缩文件 (Compressing files using the zip command)
To create a compressed file with a .zip
file extension, use the zip syntax as shown
要创建带有.zip
文件扩展名的压缩文件,请使用zip语法,如下所示
zip {options} zipfile file_name
如何使用zip命令压缩文件 (How to compress files with zip command)
To compress a single file to a zip file without any arguments use the syntax below
要将单个文件压缩为zip文件而没有任何参数,请使用以下语法
$ zip zipfile file_name
Let’s create a file file1.doc
using touch command
让我们使用touch命令创建文件file1.doc
$ touch file1.doc
Next, we are going to zip or compress it to files.zip
接下来,我们将其压缩或压缩为files.zip
$ zip files.zip file1.doc
Output
输出量
To verify the creation of the zipped file use the ls
command as shown
要验证压缩文件的创建,请使用ls
命令,如下所示
ls -l
Output
输出量
Similarly, you can zip multiple files into one zipped file. Suppose we have 4 files as shown
同样,您可以将多个文件压缩为一个压缩文件。 假设我们有4个文件,如图所示
file1.doc
file2.doc
file3.doc
file4.doc
To zip the files
压缩文件
$ zip files.zip file1.doc file2.doc file3.doc file4.doc
Output
输出量
Once again, if you wish to verify the results, use the ls
command as shown below
再一次,如果您想验证结果,请使用ls
命令,如下所示
Output
输出量
将新文件添加到预先存在的压缩文件中 (Adding a new file to a pre-existing compressed file)
Let’s assume you have created 2 more files; file5.doc
and file6.doc
and you want to include it to the zipped file.
假设您还创建了2个文件; file5.doc
和file6.doc
,您想将其包括在压缩文件中。
Use the -u
argument as shown below
使用-u
参数,如下所示
$ zip files.zip file5.doc file6.doc
Output
输出量
从压缩文件中删除文件 (Deleting a file from a compressed file)
To delete a file from an archive or a compressed file, use the -d
flag as shown
要从归档文件或压缩文件中删除文件,请使用-d
标志,如下所示
$ zip -u compressed_file file_name
Suppose you want to delete file2.doc
and file3.doc
from the compressed file. The command will be
假设您要从压缩文件中删除file2.doc
和file3.doc
。 该命令将是
$ zip -d files.zip file2.doc file3.doc
Output
输出量
归档或压缩后删除原始文件 (Deleting original files after arching or compressing )
To delete files after compressing , use the -m
option as shown
要在压缩后删除文件,请使用-m
选项,如下所示
$ zip -m archive file_name
In our example where we have the following files,
在具有以下文件的示例中,
file1.doc
file2.doc
file3.doc
file4.doc
The command shall be
该命令应为
$ zip -m files.zip *.doc
OR
要么
$ zip -m files.zip file1.doc file2.doc file3.doc file4.doc
Output
输出量
递归压缩目录 (Zipping a directory recursively )
If you wish to zip a directory recursively, use the -r
option. This command will zip all the files in the directory and save on space.
如果希望递归压缩目录,请使用-r
选项。 此命令将压缩目录中的所有文件并节省空间。
The syntax for this will be
语法将是
$ zip –r filename.zip directory_name
suppose we have a directory called data
the following files
假设我们有一个名为data
的目录,以下文件
file1.doc
file2.doc
file3.doc
file4.doc
file5.doc
To zip it into a zip file called data_files.zip
the command will be
要将其压缩为一个名为data_files.zip
的压缩文件,命令将为
$ zip -r data_files.zip data
Output
输出量
排除文件的压缩或压缩 (Exclude a file from being zipped or compressed )
To exclude a file from being compressed use the -x
option as shown
要从压缩中排除文件,请使用-x
选项,如图所示
$zip –x filename.zip file_to_be_excluded
In the previous example, to exclude file2.doc
from getting compressed, run
在上一个示例中,要从压缩中排除file2.doc
,请运行
$ zip -x data_files.zip file1.doc
获得有关zip命令的帮助 (Getting help with zip commands)
If you are stuck and want to know more about the usage of zip command run
如果您卡住了并且想了解更多有关zip命令运行的用法
$ zip --help
Output
输出量
Additionally, you can visit the command’s man pages as shown
此外,您可以访问命令的手册页,如下所示
$ man zip
Output
输出量
解压缩命令 (Unzip command )
Unzip command is the converse of zip command. It is used for decompressing or unzipping compressed files and comes with several options. The syntax for unzipping files is
解压缩命令与压缩命令相反。 它用于解压缩压缩文件或将其解压缩,并带有多个选项。 解压缩文件的语法是
$ unzip {option} file.zip
解压缩前检查压缩目录的内容 (Checking the contents of a zipped directory before unzipping)
If you wish to display the contents of an archive before unzipping, use the -l
option as shown
如果希望在解压缩之前显示档案的内容,请使用-l
选项,如下所示
$ unzip -l file.zip
For instance
例如
$ unzip -l data_files.zip
Output
输出量
解压缩具有详细输出的文件 (Unzipping files with verbose output)
To unzip a zipped file/directory with verbose output, use the -v
option as shown
要解压缩具有详细输出的压缩文件/目录,请使用-v
选项,如图所示
$ unzip -v data_files.zip
Output
输出量
将文件解压缩到特定目录 (Unzipping files to a specific directory)
If you want to unzip an archive to a specified directory , use the -d
option as shown
如果要将存档解压缩到指定目录,请使用-d
选项,如下所示
$ unzip -d data_files.zip directory_name
To unzip the zip files into a directory called james
, execute
要将zip文件解压缩到名为james
的目录中,请执行
$ unzip -d data_files.zip james
Output
输出量
检查档案中的错误 (Checking for errors in an archive)
If you wish to check for any errors in the archive before uncompressing, use the -t
flag as shown
如果希望在解压缩之前检查存档中是否有任何错误,请使用-t
标志,如下所示
$ unzip -t data_files.zip
Output
输出量
获得解压缩命令的帮助 (Getting help with unzip commands)
If you are stuck and want to know more about the usage of unzip command run
如果您卡住了并且想了解更多有关unzip命令运行的用法
$ unzip --help
Output
输出量
Additionally, you can visit the command’s man pages as shown
此外,您可以访问命令的手册页,如下所示
$ man unzip
Output
输出量
That’s all we had for today. Your feedback is highly welcome.
这就是我们今天要做的。 非常欢迎您提供反馈。
zip unzip命令行