Chat Zalo Chat Messenger Phone Number Đăng nhập
How to zip files and folders in Linux - Educative.io

How to zip files and folders in Linux – Educative.io

To compress a file on Linux or macOS, we can use the zip command.

zip zip command

syntax

[options] zipfile_name filename_separated_by_space If the

zip command is not present on your system, install it with:

Suppose we are in the project folder and the following is the structure of the project folder.

Project │ README.md │ File1.txt │ │ └───folder_1 │ │ File011.txt │ │ File012.txt │ │ │ └───subfolder_1 │ │ file1_1_1.txt │ │ │ │ │ file1_1_2.txt│ │ │ │ … │ └────folder_2 │ file2_1.txt │ file2_2.txt

Compress

files and folders

To compress files, we need

the compressed file name and the files to be included in the zip file. zip test.zip README.md

The zip command prints the names of the files added to the zip file and the compression methodThe default compression method is deflated. Now, a new zip file will be created.

Adding: README.md (stored 77%) To hide the previous message, we can use the -q option. zip test -q.zip README.md The above code will not print the added files or compression method in the terminal.

To compress

all files in one folder

, we can use

* instead of specifying all file names. zip allfiles.zip * The above

command will compress all files and folders (files inside the folder are not included) into the current directory

.

To compress a folder, we can add the following folder name

. zip folders.zip folder_1 The above code will only compress the files on the folder_1.

Files in the folder_1 subfolders will not be compressed.

To add all the files in the folder

and subfolder, we can use the -r command, which

will compress all the files in the folder. zip -r folders.zip folder_1

The above code will compress all files recursively in folder_1. The folder structure is maintained in the zip file.

We can also combine multiple folders and files by compressing files recursively.

zip -r multiplefolder.zip folder_1 folder_2 README.md Update or add a file to the zip file

We can use -you to:

Update the modified file to the zip file Add

a

new file in the zip file

Let’s add file1.txt and update README.md

in text.zip. zip -u text.zip file1.txt README.md The zip file

will only be updated if the existing file is modified or if the file is not present

  • in the zip

.

Deleting

a file from a compressed file To delete a file

from the archive, we

can use -d.zip -d multiplefolder.zip README.md Deleting files after

compressing

We can use the -m command to

delete the original files after creating the

zip file. zip test -m.zip README.md

The above command will remove README.md

. Creating a password-protected zip file

We can use -e to create a password-protected zip file.zip secret -e

.zip README.md

The above command will prompt you for a password.

Enter password: Verify password:

Changing

compression levels

The compression level defines the optimal way to compress files. Compression levels range from 0 to 9, but by default, the compression level is 6. To change the compression level, we can use -compression_number.

zip -9 optimized.zip *

The above code will compress the files with optimal compression. If we give -9, the CPU takes longer to create the zip, but the size of the zip file will be smaller. If we specify -0, zip files are created without compression.

Contact US