TranslateProject/sources/tech/20230117.0 ⭐️⭐️ Learn zip Command in Linux Using Examples.md

6.9 KiB
Raw Blame History

Learn zip Command in Linux Using Examples

Heres a beginners guide on understanding the zip command in Linux with a few examples.

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. Heres the basic syntax.

zip [compress file name] file1 file2 file3

And heres 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.

sudo apt install zip

Install in Fedora, RHEL-based systems

sudo dnf install zip

Arch Linux

pacman -S zip

Lets 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

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 doesnt 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, its 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 cant 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. Its 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: Dont 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.

This article is part of the Linux command learning series.


via: https://www.debugpoint.com/zip-command-linux-examples/

作者:Arindam 选题:lkxed 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出