如何在 Unix 命令行中压缩/解压文件

linuxoperating systemopen source

简介

在 Linux 中,压缩和解压文件非常常见。出于以下原因,我们需要将多个文件压缩成一个文件。

  • 这样可以节省系统磁盘空间。

  • 我们可以将多个文件保存在一个文件中。这也有助于将压缩文件复制到另一个系统。

当我们有一个压缩文件时,我们还应该知道如何解压它以获取所有需要的文件。因此,压缩和解压命令在 Linux 中非常重要。在本文中,我们将学习各种用于压缩和解压的命令。

使用基本的 zip 和 unzip 命令

首先,Linux 中应该有"zip"和"unzip"命令。如果这些命令不存在,请使用以下命令安装。

$ sudo apt install zip
$ sudo apt install unzip

现在,让我们创建 4 个文本文件。

$ touch 1.txt 2.txt 3.txt 4.txt
$ ls
1.txt 2.txt 3.txt 4.txt

将所有 4 个文件压缩成一个文件"all"

$ zip all *.txt
   adding: 1.txt (stored 0%)
   adding: 2.txt (stored 0%)
   adding: 3.txt (stored 0%)
   adding: 4.txt (stored 0%)

我们可以看到,all.zip 文件已创建,其中包含 1.txt、2.txt、3.txt 和 4.txt。

$ ls
1.txt 2.txt 3.txt 4.txt all.zip

现在删除所有 4 个文件并解压 all.zip。我们可以看到所有 4 个文本文件。

$ rm *.txt

$ unzip all.zip
Archive:  all.zip
   extracting: 1.txt
   extracting: 2.txt
   extracting: 3.txt
   extracting: 4.txt

$ ls
1.txt  2.txt  3.txt  4.txt  all.zip

将文件解压到目标目录

在前面的示例中,我们已经看到所有文本文件都被提取到当前路径中。始终建议将文件解压到其他目录,以免解压后的文件与当前路径下的其他文件混淆。

我们可以使用以下命令将"all.zip"解压到目录"new-all"

$ unzip -q all.zip -d new-all
$ ls new-all/
1.txt 2.txt 3.txt 4.txt

压缩时排除特定文件

在某些情况下,如果我们需要排除某些文件进行压缩,可以使用以下命令。

$ zip all 1.txt 2.txt 3.txt -x 4.txt
updating: 1.txt (stored 0%)
updating: 2.txt (stored 0%)
updating: 3.txt (stored 0%)

从输出中我们可以看到 4.txt 根本没有包含在 zip 文件中。

Let us unzip "all.zip".

$ unzip -q all.zip -d 4.txt-not-there

$ ls 4.txt-not-there/
1.txt  2.txt  3.txt

从上面的输出中,我们可以看到"4.txt"不存在。

无需解压即可查找压缩文件中的所有文件

在某些情况下,如果我们需要查看压缩文件中包含哪些文件,可以使用以下命令。

$ unzip -l all.zip

Archive:  all.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2016-02-11 22:25   1.txt
        0  2016-02-11 22:25   2.txt
        0  2016-02-11 22:25   3.txt
---------                     -------
        0                     3 files

已使用密码保护您的压缩文件

在某些情况下,如果我们需要使用密码保护压缩文件,可以使用以下命令。

$ zip -e -r password-protected new-all/

Enter password:
Verify password:
   adding: new-all/ (stored 0%)
   adding: new-all/4.txt (stored 0%)
   adding: new-all/2.txt (stored 0%)
   adding: new-all/3.txt (stored 0%)
   adding: new-all/1.txt (stored 0%)

如我们所见,系统提示用户输入密码,并创建了"password-protected.zip"。

$ ls
1.txt 2.txt 3.txt 4.txt 4.txt-not-there all.zip new-all password-protected.zip

现在让我们使用正确的密码解压"password-protected.zip"。

$ unzip password-protected.zip

Archive:  password-protected.zip
[password-protected.zip] new-all/4.txt password:
replace new-all/4.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
   extracting: new-all/4.txt
replace new-all/2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
   extracting: new-all/2.txt
replace new-all/3.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
   extracting: new-all/3.txt
replace new-all/1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
   extracting: new-all/1.txt

现在尝试使用错误的密码执行相同的"unzip"命令。

$ unzip password-protected.zip
Archive:  password-protected.zip
[password-protected.zip] new-all/4.txt password:
password incorrect--reenter:
password incorrect--reenter:
   skipping: new-all/4.txt           incorrect password
[password-protected.zip] new-all/2.txt password:
password incorrect--reenter:
password incorrect--reenter:
   skipping: new-all/2.txt           incorrect password
[password-protected.zip] new-all/3.txt password:
password incorrect--reenter:
password incorrect--reenter:
   skipping: new-all/3.txt           incorrect password
[password-protected.zip] new-all/1.txt password:
password incorrect--reenter:
password incorrect--reenter:
   skipping: new-all/1.txt           incorrect password

我们可以看到它没有成功。

结论

通过本文,我们学习了如何使用"zip"和"unzip"命令。此外,我们还学习了如何使用各种参数来执行受密码保护的压缩文件、将压缩文件解压到目标目录以及在压缩过程中排除某些文件。


相关文章


有用资源