Introduction
ZIP is the most popular archive file format for compressing files and directories. Compressing files into an archive format helps conserve space and network bandwidth. While the tape archive (tar) format is more common on Linux systems, ZIP is also widely used due to its popularity.
Linux offers the zip
command for compressing files into ZIP format. Additionally, you can create ZIP files through the GUI as well.
This guide demonstrates how to zip files and directories using both the command line and GUI in Linux.
Prerequisites
- Access to the terminal.
- Commands for creating example files and directories.
Check if zip Is Installed
Not all Linux systems have the zip program installed by default. To check if the utility is available, verify the version with the following command:
zip --version
If the output displays the program version, proceed to the next section. If the output indicates that the command is not found, install the zip
and unzip
utilities with the following command:
sudo apt install zip unzip
Note: Installing unzip
is not mandatory, but it complements the zip
command.
If you need to unzip files, refer to our guide on how to unzip a ZIP file.
Enter your sudo
password and wait for the installation to complete.
Zip Files in Linux With the zip Command
The zip
command is used to create ZIP archive files. The general syntax for the zip
command is:
zip <options> <zip file> <source file(s)>
By default, the command creates a new ZIP file. Additional options and syntax modify its behavior to provide various functionalities.
zip Command Options
The zip
command provides various modes and options to facilitate creating ZIP files. The table below offers a brief overview of the available options.
Tag | Option or Mode | Description |
---|---|---|
-u --update | Mode | Updates and adds new files. Creates a new archive if not found. |
-f --freshen | Mode | Updates files without adding new ones. Creates a new archive if not found. |
-d --delete | Mode | Chooses entries in an existing archive and removes them. |
-U --copy-entries | Mode | Chooses entries from an existing archive and copies them into a new archive. |
-e --encrypt | Option | Encrypts ZIP archive contents with a password. Starts a password entry prompt. |
-i <files> --include <files> | Option | Includes only specified files. |
-R --recurse-patterns | Option | Archives files recursively. |
-sf --show-files | Option | Lists files the command would operate on, then exits. |
-x <files> --exclude <files> | Option | Excludes specified files. |
-<number> | Option | Regulates compression speed (0-9). Lower numbers are faster. |
The zip
command includes many additional options, which you can view using the man
command.
Create a ZIP Archive
The zip
command, when used without any options, creates a new file. To test the command, follow these steps:
1. Create files for archiving:
touch file{1..5}.txt
The command generates five empty text files.
2. Use the zip
command to archive the files:
zip files file1.txt file2.txt file3.txt file4.txt file5.txt
The command outputs the actions taken and creates a files.zip archive.
List ZIP File Contents
The -sf
option displays the contents of a ZIP file. Simply provide the ZIP file name, like so:
zip -sf files.zip
The command lists the archive’s contents and exists.
Add Specific File Types to ZIP Archive
To include only specific file types in a ZIP file, utilize a wildcard and specify the filetype extension. For example:
zip files *.txt
The command adds all files with the .txt extension to the archive.
Add a Directory to ZIP Archive
Utilize the -r
(recursive) option to add a directory to a ZIP file. For example:
zip -r files <directory>
The zip
command first adds the empty directory and then populates it with the files.
Delete Files From ZIP Archive
1. To remove files from a ZIP archive, begin by listing the files from the archive using the -sf
option. For example:
zip -sf <archive file>
2. Locate the file for deletion and run zip
with the -d
tag:
zip -d <archive file> <files for deletion>
For example:
zip -d files.zip file5.txt
The command removes the specified files from the ZIP archive.
Create and Encrypt ZIP File
To secure a ZIP archive from unauthorized extraction, you can add password encryption. Employ the -e
option to apply password encryption to a ZIP file:
zip -e <archive file> <files>
The command starts a password entry prompt. After confirming, the files are added to the ZIP archive.
Control ZIP Compression Level
The zip
command enables control over the compression level of ZIP files. Compression level and speed in ZIP files are inversely proportional: higher levels result in longer compression times.
To adjust the compression level of a ZIP file, use the following syntax:
zip -<0-9> <archive file> <files>
For example:
zip -5 files *.txt
For the quickest compression, utilize -1
. For the maximum compression level, employ -9
. Values between 1 and 9 offer varying degrees of compression (speed versus level of compression).
Create a ZIP Archive Using The GUI
To create a ZIP file in Linux using the GUI, follow these steps:
- Open Files and navigate to the desired directory.
- Select the files you want to archive, right-click on them, and choose Compress.
3. Enter the archive name and choose the .zip format from the dropdown menu.
4. Click Create to create the ZIP file.
This process generates a ZIP archive in the current location.
Conclusion
Upon completing this guide, you should be proficient in creating a ZIP file in Linux using both the zip
command and the GUI.