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
Note: Installing unzip is not mandatory, but the command goes hand-in-hand with zip .
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.
The zip command helps create ZIP archive files. The general syntax for the zip command is:
Without any options, the command creates a new ZIP file. The additional options and syntax change the behavior and provide different functionalities.
The 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 |
---|---|---|
-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 —include | 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 —exclude | 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.
The 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.
The -sf option lists the contents of a ZIP file. Provide only the ZIP file name, for example:
The command lists the archive’s contents and exists.
To add only specific file types to a ZIP file, use a wildcard and provide the filetype extension. For example:
The command adds all files with the .txt extension to the archive.
Use the -r (recursive) option to add a directory to a ZIP file. For instance:
The zip command adds the empty directory first, then populates it with the files.
1. To delete files from a ZIP archive, list the files from the archive using the -sf option first. For example:
2. Locate the file for deletion and run zip with the -d tag:
The command removes the specified files from the ZIP archive.
A password protects the ZIP archive from extraction. To add password encryption to a ZIP file, use the -e option:
The command starts a password entry prompt. After confirming, the files are added to the ZIP archive.
The 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:
For the fastest compression, use -1 . For the highest level of compression, use -9 . Values between 1 and 9 provide in-between results (fast vs. level of compression).
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.
Adblock