Linux unzip 命令实用示例

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,你将学习如何使用 Linux 的 unzip 命令从压缩的 ZIP 归档文件中提取文件。实验内容涵盖了 unzip 命令的用途和语法、如何从 ZIP 归档文件中提取文件,以及如何解压受密码保护的 ZIP 文件。实验中的步骤将引导你完成这些实际示例,帮助你在 Linux 操作系统中提升压缩和归档技能。

Linux 命令速查表


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux(("Linux")) -.-> linux/CompressionandArchivingGroup(["Compression and Archiving"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("Directory Changing") linux/CompressionandArchivingGroup -.-> linux/tar("Archiving") linux/CompressionandArchivingGroup -.-> linux/zip("Compressing") linux/CompressionandArchivingGroup -.-> linux/unzip("Decompressing") subgraph Lab Skills linux/cd -.-> lab-422981{{"Linux unzip 命令实用示例"}} linux/tar -.-> lab-422981{{"Linux unzip 命令实用示例"}} linux/zip -.-> lab-422981{{"Linux unzip 命令实用示例"}} linux/unzip -.-> lab-422981{{"Linux unzip 命令实用示例"}} end

理解 unzip 命令的用途和语法

在这一步中,你将学习 Linux 中 unzip 命令的用途和基本语法。unzip 命令用于从压缩的 ZIP 归档文件中提取文件。

要了解 unzip 命令的用途和语法,可以运行以下命令:

unzip -h

示例输出:

UnZip 6.0 (Unix), by Info-ZIP. Released under the GNU General Public License.

Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
  Default action is to extract files in list, except those in xlist, to exdir.
  -Z                      show zipfile comment
  -f  -u  -o  -n          various extract options (see below)
  -x  -X                  exclude files that follow (in xlist)
  -d  exdir               extract files into exdir

unzip 命令提供了多个选项,允许你自定义提取过程。以下是一些最常用的选项:

  • -Z:显示 ZIP 文件的注释。
  • -f:更新现有文件,不替换较新的文件。
  • -u:更新文件,必要时创建新文件。
  • -o:覆盖现有文件而不提示。
  • -n:从不覆盖现有文件。
  • -x:从提取中排除指定的文件。
  • -d:将文件提取到指定目录。

要从 ZIP 归档文件中提取所有文件,可以使用以下命令:

unzip archive.zip

这将从 archive.zip 文件中提取所有文件到当前目录。

从压缩的 ZIP 归档文件中提取文件

在这一步中,你将学习如何使用 unzip 命令从压缩的 ZIP 归档文件中提取文件。

首先,让我们创建一个示例 ZIP 归档文件以便操作。运行以下命令来创建一个名为 example.zip 的 ZIP 文件,其中包含几个文本文件:

cd ~/project
mkdir example
cd example
echo "This is file1.txt" > file1.txt
echo "This is file2.txt" > file2.txt
echo "This is file3.txt" > file3.txt
zip -r example.zip .

现在,要从 example.zip 归档文件中提取文件,可以使用以下命令:

unzip example.zip

示例输出:

Archive:  example.zip
 extracting: file1.txt
 extracting: file2.txt
 extracting: file3.txt

这将从 example.zip 归档文件中提取所有文件到当前目录。

如果你想将文件提取到不同的目录,可以使用 -d 选项,后跟目标目录:

unzip example.zip -d ~/project/extracted

这将从 example.zip 中提取文件到 ~/project/extracted 目录。

解压受密码保护的 ZIP 文件

在这一步中,你将学习如何使用 unzip 命令从受密码保护的 ZIP 归档文件中提取文件。

首先,让我们创建一个受密码保护的 ZIP 归档文件以便操作。运行以下命令来创建一个名为 protected.zip 的 ZIP 文件,其中包含一个文本文件:

cd ~/project
mkdir protected
cd protected
echo "This is a protected file." > protected_file.txt
zip -e protected.zip protected_file.txt

当提示输入密码时,为 ZIP 文件设置一个密码。在本示例中,我们使用密码 "mypassword"。

现在,要从 protected.zip 归档文件中提取文件,可以使用以下命令:

unzip protected.zip

示例输出:

Archive:  protected.zip
[protected.zip] protected_file.txt password:

unzip 命令会提示你输入 ZIP 文件的密码。输入你在创建归档文件时使用的密码(在本例中为 "mypassword")。

如果密码正确,文件将被提取:

inflating: protected_file.txt

如果密码错误,unzip 命令将无法提取文件。

总结

在本实验中,你首先学习了 Linux 中 unzip 命令的用途和基本语法,该命令用于从压缩的 ZIP 归档文件中提取文件。你探索了 unzip 命令提供的各种选项,例如更新现有文件、覆盖文件、排除特定文件以及将文件提取到指定目录。接着,你学习了如何使用 unzip 命令从压缩的 ZIP 归档文件中提取文件,创建了一个示例 ZIP 文件并提取了其内容。最后,你了解了如何解压受密码保护的 ZIP 文件,通过提供正确的密码来访问归档文件。

Linux 命令速查表