mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
Merge pull request #28585 from Chao-zhi/patch-1
Update 20230117.0 ⭐️⭐️ Learn zip Command in Linux Using Examples.md
This commit is contained in:
commit
ff0fd296fd
@ -1,181 +0,0 @@
|
||||
[#]: subject: "Learn zip Command in Linux Using Examples"
|
||||
[#]: via: "https://www.debugpoint.com/zip-command-linux-examples/"
|
||||
[#]: author: "Arindam https://www.debugpoint.com/author/admin1/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: " "
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Learn zip Command in Linux Using Examples
|
||||
======
|
||||
|
||||
**Here’s a beginner’s guide on understanding the zip command in Linux with a few examples.**
|
||||
|
||||
![][1]
|
||||
|
||||
A zip file is a compressed archive containing one or more files. It is widely used as a lossless data compression technique. Thanks to compression, it takes less disk space and requires fewer data to transfer over computer networks.
|
||||
|
||||
These compressed files can be extracted easily in Linux, Windows and macOS. Various software is available that supports zip file compression and also offers to extract them.
|
||||
|
||||
Since it is popular, almost all operating systems have this built-in.
|
||||
|
||||
In this tutorial, we will talk about several terminal-based methods to zip a file in Linux.
|
||||
|
||||
### Zip Command in Linux: Examples
|
||||
|
||||
#### Syntax
|
||||
|
||||
The program name you need to use in Linux to zip a file is `zip`. Here’s the basic syntax.
|
||||
|
||||
```
|
||||
zip [compress file name] file1 file2 file3
|
||||
```
|
||||
|
||||
And here’s the official syntax.
|
||||
|
||||
```
|
||||
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
|
||||
```
|
||||
|
||||
Ideally, it should be installed in all the major Linux distributions. If not, use the following commands to install it.
|
||||
|
||||
#### Install zip on Debian, Ubuntu and related distributions
|
||||
|
||||
```
|
||||
sudo apt install zip
|
||||
```
|
||||
|
||||
#### Install in Fedora, RHEL-based systems
|
||||
|
||||
```
|
||||
sudo dnf install zip
|
||||
```
|
||||
|
||||
#### Arch Linux
|
||||
|
||||
```
|
||||
pacman -S zip
|
||||
```
|
||||
|
||||
Let’s take a look at some examples.
|
||||
|
||||
#### How to zip files and folders
|
||||
|
||||
I have the following three files in my test directory. They are file1.txt, file2.txt and file3.txt. If I want to compress three files using zip and create a zip myfiles.zip, the following command is sufficient.
|
||||
|
||||
```
|
||||
zip myfiles.zip file1.txt file2.txt file3.mp3
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
adding: file1.txt (stored 0%)adding: file2.txt (stored 0%)adding: file3.mp3 (deflated 13%)
|
||||
```
|
||||
|
||||
![Output of Basic zip command in Linux][2]
|
||||
|
||||
A few points you should remember here.
|
||||
|
||||
- When creating a zip file, you should have the modify access to the current directory.
|
||||
- The zip file format doesn’t contain the permissions, i.e. read(4), write(2), and execute(1). So, the user who creates it becomes the owner of the file.
|
||||
- If you want to use zip with permission, try the tar command (to be explained in a later tutorial).
|
||||
- In the above output, the zip command shows the file names being added to the archive and the compression method.
|
||||
- Specifying the .zip file name extension in the target file name is not mandatory. If you omit .zip, zip will add the .zip at the end.
|
||||
|
||||
When you have hundreds or thousands of files in operation, it’s a good idea to suppress the output in the terminal. You can use -q command to suppress the output in the zip command.
|
||||
|
||||
```
|
||||
zip -q myfiles.zip file1.txt file2.txt file3.txt
|
||||
```
|
||||
|
||||
#### Recursive compression of subfolders
|
||||
|
||||
The `-r` option of zip command enables you to include subdirectories and their contents. This option recursively traverses until the last child of a directory structure and adds all of them to the zip file.
|
||||
|
||||
The following command creates a compressed file with all the contents and subdirectories inside my_folder.
|
||||
|
||||
```
|
||||
zip -r myfolder.zip my_folder
|
||||
```
|
||||
|
||||
You can also use the wild card characters (*) to include specific types of files in your zip file.
|
||||
|
||||
```
|
||||
zip -0 my_movies.zip *.mp4
|
||||
```
|
||||
|
||||
#### Adding a mix of files and directories
|
||||
|
||||
With all the above options, the zip command allows you to specify files and directories together as arguments.
|
||||
|
||||
```
|
||||
zip -r myfiles.zip file1.txt file2.txt file3.txt my_folder1 my_folder2
|
||||
```
|
||||
|
||||
### Compression algorithms
|
||||
|
||||
The default output of the zip file contains two distinct words, i.e. deflate and store. The default compression method used by zip is deflate. If it successfully compresses the file, then the output shows deflate. And when it can’t compress a file, it simply stores them inside the .zip file as is. Those file outputs show stored.
|
||||
|
||||
There any many compression algorithm present. One of them is the bzip2 compression method which is supported by zip command in Linux. You can specify the compression algo as a command option to use. Use the option -Z followed by the algorithm name, as shown below.
|
||||
|
||||
```
|
||||
zip -r -Z bzip2 myfolder.zip my_folder
|
||||
```
|
||||
|
||||
#### Compress levels
|
||||
|
||||
The zip command also allows you to specify the compression level. The compression level is how much you want the zip optimized to reduce the package size. It’s a numeric value range from 0 to 9. A value of 9 compression level is the highest compression. The default value is 6.
|
||||
|
||||
Remember, if you are using zip to compress thousands of files of varying sizes, it might use higher system resources and take time. So, if you use it in programs, or shell scripts for many files, follow proper programming standards.
|
||||
|
||||
```
|
||||
zip -9 -r myfolder.zip my_folder
|
||||
```
|
||||
|
||||
#### Password protect a zip file
|
||||
|
||||
You can also password protect a zip file using `-e` option like the one below.
|
||||
|
||||
```
|
||||
zip -e -r myfolder.zip my_folder
|
||||
```
|
||||
|
||||
It will ask for the password after you run the command.
|
||||
|
||||
Caution: Don’t use the zip command to password-protect a zip file. The encryption algorithm of the zip is PKZIP using a stream cipher. And it can be cracked easily. If you want to protect your file, use 7-Zip or other advanced tools.
|
||||
|
||||
#### Split size zip files
|
||||
|
||||
Many applications, servers, and file shares may contain restrictions of a fixed-size file upload. For example, you have a 10GB file, but the service allows only 1GB per file. Using the -s option of the zip, you can compress and split them into chunks for upload.
|
||||
|
||||
```
|
||||
zip -s 1g -r myfolder.zip my_folder
|
||||
```
|
||||
|
||||
### Wrapping Up
|
||||
|
||||
You learned some basics of the zip command. It is useful for most local cases where you need to take quick backups by compressing them on the fly. However, for more advanced options, you should use 7-Zip or other commands, which I will share in the next few articles.
|
||||
|
||||
In the meantime, you can learn more in the [zip manual][3].
|
||||
|
||||
_This article is part of the [Linux command][4] learning series._
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.debugpoint.com/zip-command-linux-examples/
|
||||
|
||||
作者:[Arindam][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.debugpoint.com/author/admin1/
|
||||
[b]: https://github.com/lkxed
|
||||
[1]: https://www.debugpoint.com/wp-content/uploads/2023/01/zip-file-head.jpg
|
||||
[2]: https://www.debugpoint.com/wp-content/uploads/2023/01/Output-of-Basic-zip-command-in-Linux.jpg
|
||||
[3]: https://linux.die.net/man/1/zip
|
||||
[4]: https://www.debugpoint.com/category/linux-commands
|
@ -0,0 +1,183 @@
|
||||
[#]: subject: "Learn zip Command in Linux Using Examples"
|
||||
[#]: via: "https://www.debugpoint.com/zip-command-linux-examples/"
|
||||
[#]: author: "Arindam https://www.debugpoint.com/author/admin1/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "Chao-zhi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
通过实例学习Linux中的zip命令
|
||||
======
|
||||
|
||||
**这里是关于理解 Linux 中的 zip 命令的初学者指南,并附有一些例子。**
|
||||
|
||||
![][1]
|
||||
|
||||
zip 文件是一个包含一个或多个文件的压缩档案。它作为一种无损数据压缩技术被广泛使用。由于压缩,它占用的磁盘空间更少,在计算机网络上传输时需要的数据也更少。
|
||||
|
||||
这些压缩文件可以在 Linux、Windows 和 macOS 中轻松提取。有各种支持压缩文件的软件,也提供提取它们的功能。
|
||||
|
||||
由于它很流行,几乎所有的操作系统都内置了这个功能。
|
||||
|
||||
在本教程中,我们将谈论几种基于终端的方法来压缩 Linux 中的文件。
|
||||
|
||||
### Linux 中的 Zip 命令,例子:
|
||||
|
||||
#### 语法
|
||||
|
||||
在 Linux 中,你需要使用的压缩文件的程序名称是`zip`。下面是基本的语法。
|
||||
|
||||
``` sh
|
||||
zip [压缩文件名] file1 file2 file3
|
||||
```
|
||||
|
||||
这里是官方的语法。
|
||||
|
||||
``` sh
|
||||
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
|
||||
```
|
||||
|
||||
理想情况下,zip 命令应该被安装在所有主要的 Linux 发行版中。如果没有,使用下面的命令来安装它。
|
||||
|
||||
#### 在 Debian, Ubuntu and 相关发行版上安装
|
||||
|
||||
```
|
||||
sudo apt install zip
|
||||
```
|
||||
|
||||
#### 在 Fedora, 基于 RHEL 的系统上安装
|
||||
|
||||
```
|
||||
sudo dnf install zip
|
||||
```
|
||||
|
||||
#### 在 Arch Linux 上安装
|
||||
|
||||
```
|
||||
pacman -S zip
|
||||
```
|
||||
|
||||
让我们继续看一些例子
|
||||
|
||||
#### 如何压缩文件和文件夹
|
||||
|
||||
我的测试目录中有以下三个文件。它们是 file1.txt、file2.txt 和 file3.txt。如果我想用 zip 压缩三个文件,并创建一个 myfiles.zip 的压缩包,用下面的命令就可以了。
|
||||
|
||||
```
|
||||
zip myfiles.zip file1.txt file2.txt file3.mp3
|
||||
```
|
||||
|
||||
输出。
|
||||
|
||||
```
|
||||
adding: file1.txt (stored 0%)
|
||||
adding: file2.txt (stored 0%)
|
||||
adding: file3.mp3 (deflated 13%)
|
||||
```
|
||||
|
||||
![Linux 中基本压缩命令的输出][2]
|
||||
|
||||
这里你应该记住几个要点。
|
||||
|
||||
- 当创建一个 zip 文件时,你应该有对当前目录的修改权限。
|
||||
- zip 文件格式不包含权限,即读(4),写(2),和执行(1)。所以,创建该文件的用户成为该文件的所有者。
|
||||
- 如果你想使用有权限的 zip,可以尝试使用 `tar` 命令(将在后面的教程中解释)。
|
||||
- 在上面的输出中,`zip` 命令显示了被添加到存档中的文件名和压缩方法。
|
||||
- 在目标文件名中指定 .zip 文件名的扩展名并不是必须的。如果你省略了 .zip,`zip` 会在最后加上 .zip。
|
||||
|
||||
当你有成百上千的文件在运行时,可以在终端中减少输出。你可以使用 `-q` 参数来抑制 `zip` 命令中的输出。
|
||||
|
||||
```
|
||||
zip -q myfiles.zip file1.txt file2.txt file3.txt
|
||||
```
|
||||
|
||||
#### 递归压缩子文件夹
|
||||
|
||||
`zip` 命令的 `-r` 选项使你能够囊括所有子目录。这个选项会递归地遍历到一个目录结构的最后一个子目录,并将它们全部加入到压缩文件中。
|
||||
|
||||
下面的命令创建了一个包含 my_folder 内所有内容和子目录的压缩文件。
|
||||
|
||||
```
|
||||
zip -r myfolder.zip my_folder
|
||||
```
|
||||
|
||||
你也可以使用通配符(*)在你的压缩文件中包含特定类型的文件。
|
||||
|
||||
```
|
||||
zip -0 my_movies.zip *.mp4
|
||||
```
|
||||
|
||||
#### 混合添加文件和目录到压缩文件
|
||||
|
||||
有了以上所有的选项,`zip` 命令允许你把文件和目录一起作为参数指定。
|
||||
|
||||
```
|
||||
zip -r myfiles.zip file1.txt file2.txt file3.txt my_folder1 my_folder2
|
||||
```
|
||||
|
||||
### 压缩算法
|
||||
|
||||
zip 压缩的默认输出包含两个不同的词,即 deflate 和 store。zip 默认使用的压缩方法是 deflate。如果它成功地压缩了文件,那么输出显示 deflate。而当它不能压缩一个文件时,它只是将它们原封不动地存储在 .zip 文件中。这些文件的输出显示为 store。
|
||||
|
||||
目前有许多压缩算法。其中一种是 bzip2 压缩法,它在 Linux 中被 `zip` 命令所支持。你可以指定压缩算法作为一个命令选项来使用。使用选项 `-Z`,后面跟上算法名称,如下所示。
|
||||
|
||||
```
|
||||
zip -r -Z bzip2 myfolder.zip my_folder
|
||||
```
|
||||
|
||||
#### 压缩级别
|
||||
|
||||
`zip` 命令还允许你指定压缩级别。压缩级别是指你想让 zip 优化多少来减少包的大小。它是一个从 0 到 9 的数值范围。压缩级别为 9 的值是最高的压缩。默认值是 6。
|
||||
|
||||
记住,如果你用 zip 压缩成千上万个大小不一的文件,它可能会占用较多的系统资源,并花费大量的时间。所以,如果你在程序中使用它,或者用 shell 脚本处理大量的文件,请遵循正确的编程标准。
|
||||
|
||||
```
|
||||
zip -9 -r myfolder.zip my_folder
|
||||
```
|
||||
|
||||
#### 用密码保护一个压缩文件
|
||||
|
||||
你也可以用下面的 `-e` 选项对压缩文件进行密码保护。
|
||||
|
||||
```
|
||||
zip -e -r myfolder.zip my_folder
|
||||
```
|
||||
|
||||
运行该命令后,它将要求输入密码。
|
||||
|
||||
> 注意。尽量不要使用 zip 命令来对压缩文件进行密码保护。zip 的加密算法是使用流密码的 PKZIP。而它很容易被破解。如果你想保护你的文件,请使用 7-Zip 或其他高级工具。
|
||||
|
||||
#### 分割较大的压缩文件
|
||||
|
||||
许多应用程序、服务器和文件共享可能包含固定大小的文件上传限制。例如,你有一个 10GB 的文件,但服务只允许每个文件 1GB。使用 `zip` 的 `-s` 选项,你可以将其压缩并分割成几块进行上传。
|
||||
|
||||
```
|
||||
zip -s 1g -r myfolder.zip my_folder
|
||||
```
|
||||
|
||||
### 总结
|
||||
|
||||
你学到了一些 `zip` 命令的基本知识。它对大多数本地情况很有用,在这些情况下,你需要通过即时压缩来进行快速备份。然而,对于更高级的选项,你应该使用 7-Zip 或其他命令,我将在接下来的几篇文章中分享。
|
||||
|
||||
同时,你可以在 [zip 手册][3]中了解更多。
|
||||
|
||||
_这篇文章是 [Linux 命令][4]学习系列的一部分。_
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.debugpoint.com/zip-command-linux-examples/
|
||||
|
||||
作者:[Arindam][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[Chao-zhi](https://github.com/Chao-zhi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.debugpoint.com/author/admin1/
|
||||
[b]: https://github.com/lkxed
|
||||
[1]: https://www.debugpoint.com/wp-content/uploads/2023/01/zip-file-head.jpg
|
||||
[2]: https://www.debugpoint.com/wp-content/uploads/2023/01/Output-of-Basic-zip-command-in-Linux.jpg
|
||||
[3]: https://linux.die.net/man/1/zip
|
||||
[4]: https://www.debugpoint.com/category/linux-commands
|
Loading…
Reference in New Issue
Block a user