How to zip files and folders in Linux

Did you know Phil Katz and Gary Conway designed the ZIP file format, which supports lossless data compression and is pervasive among computer users? The “PK” in PKZIP stands for his initials, Phil Katz.

Key takeaways:

  • ZIP files: This is a popular format for lossless data compression, commonly used to reduce file size and bundle multiple files together.

  • Benefits of ZIP: It has a smaller file size for faster transfers and easier storage.

  • Example commands:

    • Zip a single file: zip zip_file_name.zip file_name    

    • Zip a folder recursively: zip -r zip_folder_name.zip folder_name   

    • Zip all files in the current directory: zip zip_file_name.zip *    

    • Update a ZIP file: zip -u zip_file_name.zip file_name   

    • Unzip a ZIP file: unzip zip_file_name.zip

Migrating data is a common task nowadays. Imagine sending gigabytes of project files to a client over a slow internet connection. Painful, isn’t it? How can we speed up the transfer?

Internet speed might not be in our control. The other important factor determining transfer times is the data size that needs to be transmitted. Can we reduce the size of data without loss of information? Yes. Zip is a tool or method that can reduce file size and ensure everything is neatly bundled in one place. It achieves all this without any loss of information.

Learning how to compress data in Linux will simplify file management and lossless data compression and make file transfers an easy process. Linux provides the zip command to compress the data in the .zip extension.

Syntax for the zip command

The following is the syntax for the zip command

zip [options] zipfile_name filename
//Syntax for folder
zip [options] zipfolder_name foldername
  • zipfile_name is the name of the zip file.
  • filename is the file that we want to compress. If we want to compress multiple files, separate the file names by space.
  • [options] can be as follows:
    • -d is used to remove files from the zip

    • -m delete the original files

    • -u allows us to update the zip

    • -r recursively zip a directory and subdirectories

    • -v is used to enable verbose mode

    • -x excludes specific files during the zipping

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

sudo apt install zip

Zipping files and folders

Suppose we are in the folder project, 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

How to zip files in Linux

To zip files, we need the file name and the files to be included in the zip file. In the following example, the zip file name is test.zip, and the file that we want to compress is README.md.

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. The following message will display when we run the zip command.

adding: README.md (stored 77%)

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

zip -q test.zip README.md

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

How to zip folder in Linux

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

zip folders.zip folder_1

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

The -r command compresses all files in the folder and its subfolders.

zip -r folders.zip folder_1

The above code will zip all the files recursively in folder_1, maintaining the folder structure in the zip file.

How to compress multiple folders and files

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

zip -r multiplefolder.zip folder_1 folder_2 README.md

How to zip all files and folders in the current directory

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

zip allfiles.zip *

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

Question

How to unzip files into the current directory?

Show Answer

Hands-on exercise

The terminal given below has a folder named project, following the same directory structure we provided at the beginning. Your task is to recursively compress all files in folder_2 and the README.md file.

Terminal 1
Terminal
Loading...

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

zip -d multiplefolder.zip README.md

Removing files after zipping

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

zip -m test.zip README.md

The command above will delete README.md.

Creating a password-protected zip file

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

zip -e secret.zip README.md

The command above will provide password encryption and ask for a password.

Enter password: 
Verify password: 

Creating split zip file

When we need to handle large files that exceed the system's storage or transfer constraints, splitting a ZIP archive into multiple parts can be helpful. We can use the zip command with the -s option in Linux to create a split ZIP file, which is a ZIP archive split into multiple parts.

zip -r -s 1g zipfile_name filename

Here, -s 1g specifies that the archive should be split into parts of 1GB each. We can adjust this size depending on our needs.

Changing compression levels and methods

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

zip -9 optimized.zip *

The code above 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.

The compression method can be changed using the -Z option followed by the desired compression method. By default compression method is deflate. Other common compression methods are store, bzip2 and lzma. Let’s see compression ratios, speed, and use cases for common compression methods in the following table.

Compression Methods

Compression Ratio

Speed

Use Case

deflate

Good

Fast

General-purpose compression

store

None

Fastest

No compression required

bzip2

High

Slower

Large text or data files

lzma

Highest

Slowest

Large datasets

In the following snippet, -Z store sets the compression method.

zip -r -Z store zipfile_name filename

Hands-on exercise

The terminal given below has a folder named project, following the same directory structure we provided at the beginning. Your task is to recursively compress all files and folders in the project directory. Please make sure to delete the original files when compressing them. Next, delete the README.md file from the compressed folder and unzip the folder using unzip zipfoldername.zip to confirm whether README.md is deleted or not.

Terminal 1
Terminal
Loading...

In summary, the zip command in Linux provides a powerful way to manage file sizes and simplify data transfers.

Quiz!

1

How do we zip all files and folders in Linux?

A)

zip allfilesfolders.zip *

B)

zip -r allfilesfolders.zip *

C)

zip -u allfiles.zip *

D)

zip -d allfiles.zip *

Question 1 of 40 attempted

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How do we create a zip file on Apple Mac?

  • Right-click on the file or folder we want to zip.
  • Select “Compress (file/folder name)” from the drop-down menu This will create a .zip file in the same directory.

How can we create a zip file on Windows?

  • Right-click on the file or folder.
  • Select “Send to,” and then select “Compressed (zipped) folder.” This will create a .zip file in the same directory.

How do we create archives larger than 2GiB?

We can use the -s option in the zip command to create a split zip archive. Other than it, we can use zip archive tool such as 7-Zip or WinRAR.


How can we unzip a directory and its files in Linux?

unzip filenname.zip

How do we gz or gzip a file and folder in Linux?

We can use the following command to gzip a file and folder:

tar -czvf [zipname].tar.gz [folder_name]

Check out our detailed Answer on How to gzip a directory in Linux


What is a compressed zip folder?

A compressed zip folder consists of compressed files in the form of a single file with the .zip extension.


How do we extract multiple zip files at once

The following are two possible ways to extract multiple zip files at once:

  • Using a zip archive tool like 7-Zip or WinRAR
  • Using a loop in the Linux terminal to extract all zip files in the directory

How can we uncompress a zip file

  • Linux: Use the command unzip [filename].zip
  • Mac: Automatically extract the zip file to the same directory by double-clicking it.
  • Windows: Right-click the zip file and select “Extract All” to uncompress it.

How do we uncompress a password-protected ZIP file

In Linux, use unzip [zippedfilename].zip and enter the password when prompted to extract the files.


Free Resources