How to Zip a File in Linux?

How to Zip a File in Linux?

5 mins read225 Views Comment
Updated on Mar 17, 2023 17:35 IST

zip command is a Linux utility used to compress and package one or more files or directories into a single ZIP archive.

2023_03_MicrosoftTeams-image-220.jpg

The ZIP archive format is popular for compressing and storing one or more files in a single container file. It was developed in 1989 by Phil Katz for his PKZIP utility and has since become a widely used format for file compression and archiving.

ZIP archives use lossless compression algorithms to reduce the size of archived files. This makes them a convenient way to package and transmit multiple files, especially over the internet where bandwidth and storage space is limited. The format supports a range of compression levels and file formats, making it versatile and widely compatible.

ZIP archives can be created and extracted using a variety of proprietary and open-source software tools. They are widely supported by operating systems, including Windows, macOS, and Linux, and are commonly used for software distribution, backup, and storage, or simply general file archiving purposes.

Prerequisites

  1. Access to the terminal.
  2. Access to create files.

The zip Command in Linux

The zip command is a Linux utility used to compress and package one or more files or directories into a single ZIP archive. zip command also provides a range of options to control the behavior of the archive creation process, including specifying the compression level, adding or updating files to an existing archive, and setting passwords or encryption.

Syntax:

The syntax for the zip command is as follows:

 
zip [options] archive.zip file1 file2 dir1 dir2
Copy code

Here, archive.zip is the name of the resulting ZIP archive file, and file1, file2, dir1, dir2, and so on are the files and directories to be included in the archive. Take a look at the below image for reference:

Using the SED Command in Linux
Using the SED Command in Linux
Discover the versatility of the sed command in Linux for text manipulation in files and streams. Our guide covers basic and advanced features to enhance your productivity. This comprehensive guide...read more
How to Set Environment Variables in Linux?
How to Set Environment Variables in Linux?
Here you will understand how to set environment variables in Linux. We have covered topics such as setting, unsetting, listing environment variable etc. Let’s explore.
Know About Bash Command in Linux
Know About Bash Command in Linux
Looking for a comprehensive list of Bash commands in Linux? Look no further! This guide covers all the major Bash commands, including ls, cd, mkdir, rm, grep, sed, awk, and...read more

zip Command Options

The zip command in Linux provides a range of options to control the behavior of the archive creation process. Here are some of the most commonly used options:

  • -r: Recursively include all files and subdirectories in the specified directories.
  • -q: Quiet mode; suppresses output messages.
  • -9: Set the compression level to maximum.
  • -e: Encrypt the archive with a password.
  • -r-: Do not include the contents of subdirectories when recursing.
  • -j: Store only the file names in the archive, without any directory information.
  • -u: Update the specified archive with any files that have been modified or added since the last update.
  • -v: Verbose mode; display output messages during archive creation.
  • -T: Test the integrity of the specified archive.
  • -m: Delete the original files after adding them to the archive.
  • -P: Set the password for the archive (without displaying it on the screen).

You can see a full list of options available with the zip command by running man zip in the Linux terminal, which will display the manual page for the command.

What is Awk Command in Linux?
What is Awk Command in Linux?
Here we will understand the concept of Awk command in Linux. You will also find its features, AWK Script, along with examples.
What is Grep Command in Linux?
What is Grep Command in Linux?
Here you will understand the Grep command in Linux. We have also explained options description and sample commands. Let’s check out.
Learning About Time Sharing Operating System
Learning About Time Sharing Operating System
Time sharing operating system can execute multiple processes at the same time. They leverage CPU and reduce its idle time. Several users can use a single computer simultaneously in case...read more

Create a zip File

To create a ZIP archive named myarchive.zip containing all files and directories in the current directory, you can run the following command:

 
zip myarchive.zip *
Copy code

This will create a new archive file myarchive.zip that contains all the files and directories in the current working directory as shown below:

How to list files in Linux?
How to list files in Linux?
In this article we will study about How to list files in Linux?And this is done Using ls Command with flags.
How to Find Files in Linux?
How to Find Files in Linux?
The find command in Linux is powerful tool that allows to quickly and easily scan through the file system to find files and directories. In this article, we will discuss...read more
How to List Files in a Directory – ls command
How to List Files in a Directory – ls command
If you are a user of the LINUX operating system, you might be familiar with the Command Line Interface (CLI) and ls command. CLI comes in with a set of...read more

List ZIP File Contents

We can make use of the –sf option to list the contents of a zip file in Linux as shown below:

 
zip -sf <filename>
Copy code

Take a look at the below image for reference:

Add Specific File Types to ZIP Archive

We can make use of the wildcard option provided by zip command as shown below:

 
zip <zip_file_name> files *<file_type>
Copy code

Take a look at the below example for reference, where we just want to include the .py files within the current directory into a zip file called python_files.zip:

 
zip python_files.zip files *.py
Copy code

Output:

Difference between Linux and Unix
Difference between Linux and Unix
Linux and Unix are operating systems similar to Windows, Ubuntu, and Apple macOS. Operating Systems contain and manage all the programs and applications that run on the desktop or mobile....read more
How to Delete a File in Linux?
How to Delete a File in Linux?
Need to clear some space on your Linux system? It’s easier than you might think! With a simple command, you can swiftly delete any file. But what’s the right way...read more
Learn Basic Linux Commands (With Syntax)
Learn Basic Linux Commands (With Syntax)
"Linux commands" refer to the instructions or inputs that users provide to perform specific operations in the Linux operating system. Linux, being a command-line-oriented operating system, relies heavily on these...read more

Add a Directory to ZIP Archive

We can use the -r option to add a directory to zip Archive as shown below:

 
zip <filename> -r files <directory_name>
Copy code

Take a look at the below image for reference where we have added the directory test1 to a zip file with name directory.zip.  For this, we have used the following command:

 
zip directory.zip -r files test1
Copy code

Output:

2023_03_image-49.jpg

Create and Encrypt ZIP File

To create and encrypt a ZIP file in Linux, you can use the zip command with the -e option to specify the password for the archive. 

Syntax:

 
zip -e archive.zip file1 file2 dir1 dir2
Copy code

This will create a new ZIP file named archive.zip, which will contain the specified files and directories, and will be encrypted with the password that you enter when prompted.

For example, to create an encrypted ZIP file named myarchive.zip containing all files and directories in the current directory, you can run the following command:

 
zip -e myarchive.zip *
Copy code

Take a look at the below image for reference:

After running this command, you will be prompted to enter a password for the archive. Enter a strong password and then confirm it when prompted. The resulting ZIP file myarchive.zip will be encrypted with the password that you entered.

Manage ZIP Compression Level

In Linux, you can manage the compression level of a ZIP file using the zip command’s -X option, which allows you to set the compression level.

 The compression level is a value between 0 (no compression) and 9 (maximum compression).

Syntax:

 
zip -X level archive.zip file1 file2 dir1 dir2
Copy code

Here, the level is the desired compression level, and can be any integer between 0 and 9. A lower value means less compression and faster operation, while a higher value means more compression and slower operation.

For example, to create a ZIP file named myarchive.zip with a compression level of 5, containing all files and directories in the current directory, you can use the following command:

 
zip -X 5 myarchive.zip *
Copy code

Output:

Delete Specific Files from zip File

We can use the zip command to delete files from a ZIP archive by using the -d option. 

Syntax:

 
zip -d archive.zip file1 file2 dir1/dir2
Copy code

Here, archive.zip is the name of the ZIP archive from which you want to delete files, and test1.txt, test2.txt, test1 are the names of the files and directories you want to remove from the archive.

For example, to delete the file test1.txt from the archive.zip file, you can use the following command:

 
zip -d archive.zip test1.txt
Copy code

Output:

Similarly, to delete all files with the extension .txt  from the archive.zip file, you can use the following command:

 
zip -d archive.zip "*.txt"
Copy code

Output:

This will remove all files with the .docx extension from the archive.zip file.
Note that the -d option does not update the archive’s compression level or other settings. If you want to make additional changes to the archive after deleting files, you will need to re-compress it using the zip command with appropriate options.

Conclusion

In this article, we have discussed how to zip a file in linux. Hope you will like the article.

Keep Learning!!

Keep Sharing!!

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio