ZIP is the most popular archive file format for compressing files and directories. Compressing files into an archived format helps conserve space and network bandwidth. Even though the tape archive (tar) format is more common on Linux systems, ZIP is also used often due to its popularity.
Linux offers the
zip command for compressing files into ZIP format. Alternatively, creating ZIP files is possible through the GUI, too.
This guide shows how to zip files and directories through the command line and GUI in Linux.
- 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. See whether the utility is available by checking the version:
If the output prints the program version, continue to the following section. However, if the output shows the command is not found, install the zip and unzip utility with the following command:
sudo apt install zip unzip
unzip is not mandatory, but the command goes hand-in-hand with
If you're looking to unzip files, check out our guide on how to unzip a ZIP file.
Enter the sudo password and wait for the installation to complete.
Zip Files in Linux With the zip Command
zip command helps create ZIP archive files. The general syntax for the
zip command is:
zip <options> <zip file> <source file(s)>
Without any options, the command creates a new ZIP file. The additional options and syntax change the behavior and provide different functionalities.
zip Command Options
zip command offers various work modes and options to help create ZIP files. The table below shows a short overview of the available options.
|Tag||Option or Mode||Description|
|Mode||Updates and adds new files. Creates a new archive if not found.|
|Mode||Updates files without adding new ones. Creates a new archive if not found.|
|Mode||Chooses entries in an existing archive and removes them.|
|Mode||Chooses entries from an existing archive and copies them into a new archive.|
|Option||Encrypts ZIP archive contents with a password. Starts a password entry prompt.|
|Option||Includes only specified files.|
|Option||Archives files recursively.|
|Option||Lists files the command would operate on, then exits.|
|Option||Excludes specified files.|
|Option||Regulates compression speed (0-9). Lower numbers are faster.|
The zip command offers many other options you can view using the man command.
Create a ZIP Archive
zip command, without any options, creates a new file. To test the command, do the following:
1. Create files for archiving:
The command creates 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
-sf option lists the contents of a ZIP file. Provide only the ZIP file name, for example:
zip -sf files.zip
The command lists the archive's contents and exists.
Add Specific File Types to ZIP Archive
To add only specific file types to a ZIP file, use a wildcard and provide 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
-r (recursive) option to add a directory to a ZIP file. For instance:
zip -r files <directory>
zip command adds the empty directory first, then populates it with the files.
Delete Files From ZIP Archive
1. To delete files from a ZIP archive, list the files from the archive using the
-sf option first. For example:
zip -sf <archive file>
2. Locate the file for deletion and run
zip with the
zip -d <archive file> <files for deletion>
zip -d files.zip file5.txt
The command removes the specified files from the ZIP archive.
Create and Encrypt ZIP File
A password protects the ZIP archive from extraction. To add password encryption to a ZIP file, use the
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
zip command allows controlling the compression level. The ZIP file compression level and speed are reversely proportional. As the level increases, the compression takes longer.
To control the ZIP file compression level, use the following syntax:
zip -<0-9> <archive file> <files>
zip -5 files *.txt
For the fastest compression, use
<b>-1</b>. For the highest level of compression, use
-9. Values between 1 and 9 provide in-between results (fast vs. level of compression).
Create a ZIP Archive Using The GUI
To create a ZIP file in Linux through the GUI, do the following:
1. Open Files and navigate to the appropriate directory.
2. Select the files for archiving, right-click the files, 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.
The process creates a ZIP archive in the current location.
After reading this guide, you should know how to create a ZIP file in Linux with the
zip command and GUI. For other file compression and archiving commands, check out the tar command.