How to Zip a File in Linux?
zip command is a Linux utility used to compress and package one or more files or directories into a single ZIP archive.
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
- Access to the terminal.
- Access to create files.
Best-suited Linux courses for you
Learn Linux with these high-rated online courses
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
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:
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.
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 *
This will create a new archive file myarchive.zip that contains all the files and directories in the current working directory as shown below:
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>
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>
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
Output:
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>
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
Output:
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
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 *
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
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 *
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
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
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"
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!!
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