Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to zip files and folders in Linux

Programming Bytes

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

Syntax for the zip command

zip [options] zipfile_name filename_separated_by_space

If the zip command is not present in your system, install it with:

sudo apt install zip

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

│   file1.txt    
│   │   file011.txt
│   │   file012.txt
│   │
│   └───subfolder_1
│       │   file1_1_1.txt
│       │   file1_1_2.txt
│       │   ...
    │   file2_1.txt
    │   file2_2.txt

Zipping files and folders

To zip files, we need the zipped filename and the files to be included in the zip file.


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: (stored 77%)

To hide the above message, we can use the -q option.

zip -q

The above code will not print the files added or the compression method in the terminal.

To zip all the files in a folder, we can use * instead of specifying all the filenames.

zip *

The above command will zip all the files and folders (files inside the folder are not included) in the current directory.

To zip a folder, we can add the following folder name.

zip folder_1

The above code will only zip the files in the folder_1. The files in the subfolders of folder_1 will not be compressed.

To add all files in the folder and subfolder, we can use the -r command, which will compress all the files in the folder.

zip -r folder_1

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

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

zip -r folder_1 folder_2

Updating or adding a file to the zip file

We can use -u to:

  • update the modified file to the zip file
  • add a new file in the zip file

Let’s add file1.txt and update in

zip -u file1.txt

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 compressed file, we can use -d.

zip -d

Removing files after zipping

We can use the -m command to delete the original files after the zip file is created.

zip -m

The above command will delete

Creating a password-protected zip file

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

zip -e

The above command will ask for a password.

Enter password: 
Verify password: 

Changing compression levels

The compression level defines how optimally the files can be compressed. The compression levels range from 0-9 but, by default, the compression level is 6. To change the compression level, we can use -compression_number.

zip -9 *

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



View all Courses

Keep Exploring