ZIP is a popular way to compress files and folders to save space and network usage. While Linux systems commonly use the tape archive (tar) format, ZIP is also widely used because of its popularity.
Linux has a zip command to compress files into ZIP format. You can also create ZIP files using the graphical interface.
This guide explains how to zip files and directories in Linux using both the command line and the graphical interface.
Make sure you have zip installed
Some Linux systems might not have the zip program installed when you get them. To check if it's available, type this command:
$ zip --version
If you see the program version in the output, you're good to go. But if it says the command is not found, you can install the zip and unzip utility using this command:
# apt install zip unzip
Syntax and Option of Zip command
Zip command in Linux helps create ZIP archive files. To use it, simply type "zip" followed by various options and the names of the ZIP file and source file(s) you want to include.
Syntax
zip <options> <zip file> <source file(s)>
Here are some important options:
-u or --update: Updates and adds new files. If the archive doesn't exist, a new one is created.
-f or --freshen: Updates files without adding new ones. If the archive doesn't exist, a new one is created.
-d or --delete: Removes specific entries from an existing archive.
-U or --copy-entries: Copies chosen entries from one archive into a new archive.
-e or --encrypt: Encrypts the contents of the ZIP archive with a password. It prompts for the password entry.
-i <files> or --include <files>: Includes only the specified files in the ZIP archive.
-R or --recurse-patterns: Archives files recursively, including files in subdirectories.
-sf or --show-files: Lists the files the command would operate on and then exits.
-x <files> or --exclude <files>: Excludes the specified files from the ZIP archive.
-<number>: Regulates the compression speed from 0 to 9. Lower numbers mean faster compression.
Example of Zip command:
1. Creating a ZIP Archive:
To create a new ZIP file without any options, follow these steps:
Generate files for archiving:
Execute the following command to create five empty text files:
$ touch nick{1..5}.txt
Archive the files using the zip command:
$ zip files nick1.txt nick2.txt nick3.txt nick4.txt nick5.txt
The command will display the actions taken and generate a files.zip archive.
2. Listing ZIP File Contents:
To list the contents of a ZIP file, use the -sf option and provide the ZIP file name:
$ zip -sf files.zip
The command will output the contents of the archive and terminate.
3. Adding Specific File Types to ZIP Archive:
If you wish to add only specific file types to a ZIP file, utilize a wildcard and specify the file type extension. For instance:
$ zip files *.txt
This command will include all files with the .txt extension in the archive.
4. Adding a Directory to ZIP Archive:
To add a directory to a ZIP file with all its contents, use the -r (recursive) option. For example:
$ zip -r files <directory>
The zip command will first add the directory and then include all files.
5. Deleting Files From ZIP Archive
To remove files from a ZIP archive, first list the files from the archive using the -sf option. For example:
$ zip -sf <archive file>
Find the file you want to delete and run the zip command with the -d tag:
For example:
zip -d <archive file> <files for deletion>
This command will remove the specified files from the ZIP archive.
6. Creating and Protecting ZIP File with Password
A password adds protection to the ZIP archive to prevent unauthorized access. To encrypt a ZIP file with a password, use the -e option:
zip -e <archive file> <files>
This command will prompt you to enter a password. After confirmation, the files will be added to the ZIP archive with encryption.
7. Controlling ZIP Compression Level
The zip command allows you to control the compression level of ZIP files. The higher the compression level, the longer the compression process takes.
To control the ZIP file compression level, use the following syntax:
$ zip -<0-9> <archive file> <files>
For example:
For the fastest compression, use . For the highest level of compression, use -9. Values between 1 and 9 offer different compression levels (from fast to high compression).
Conclusion:
In Linux, the Zip command is used to make files smaller and put them together in a single .zip archive. This helps us save space on the computer and handle large amounts of data easily. We talked about different options in the Zip command, like -d, -e, -sf, and -r. All in all, it's a useful tool for Linux users to manage their files efficiently.