How to Zip a File in Linux

October 31, 2022

Introduction

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.

How To Zip A File 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. See whether the utility is available by checking the version:

zip --version
zip --version terminal output

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.

Zip Files in Linux With the zip Command

The 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

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.

TagOption or ModeDescription
-u
--update
ModeUpdates and adds new files. Creates a new archive if not found.
-f
--freshen
ModeUpdates files without adding new ones. Creates a new archive if not found.
-d
--delete
ModeChooses entries in an existing archive and removes them.
-U
--copy-entries
ModeChooses entries from an existing archive and copies them into a new archive.
-e
--encrypt
OptionEncrypts ZIP archive contents with a password. Starts a password entry prompt.
-i <files>
--include <files>
OptionIncludes only specified files.
-R
--recurse-patterns
OptionArchives files recursively.
-sf
--show-files
OptionLists files the command would operate on, then exits.
-x <files>
--exclude <files>
OptionExcludes specified files.
-<number>OptionRegulates 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

The zip command, without any options, creates a new file. To test the command, do the following:

1. Create files for archiving:

touch file{1..5}.txt

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
Create and zip files terminal output

The command outputs the actions taken and creates a files.zip archive.

List ZIP File Contents

The -sf option lists the contents of a ZIP file. Provide only the ZIP file name, for example:

zip -sf files.zip
zip -sf show files terminal output

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
zip all text files terminal output

The command adds all files with the .txt extension to the archive.

Add a Directory to ZIP Archive

Use the -r (recursive) option to add a directory to a ZIP file. For instance:

zip -r files <directory>
zip -r directory with files terminal output

The 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 -d tag:

zip -d <archive file> <files for deletion>

For example:

zip -d files.zip file5.txt
zip -d delete files terminal output

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 -e option:

zip -e <archive file> <files>
zip -e encrypted file terminal output

The command starts a password entry prompt. After confirming, the files are added to the ZIP archive.

Control ZIP Compression Level

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:

zip -<0-9> <archive file> <files>

For example:

zip -5 files *.txt
zip files compression 5 terminal output

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.

Ubuntu GUI Compress option

3. Enter the archive name and choose the .zip format from the dropdown menu.

Create archive ZIP GUI Ubuntu

4. Click Create to create the ZIP file.

The process creates a ZIP archive in the current location.

Conclusion

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.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.
Next you should read
tar Command in Linux With Examples
November 9, 2021

The tar command is is used to create and extract archived and compressed packages on...
Read more
How to Unzip a ZIP File in Ubuntu / Linux
December 2, 2019

Zip files are ubiquitously used on all operating systems, including Linux. This simple guide explains how to unzip files...
Read more
How to Extract or Unzip tar.gz Files from Linux Command Line
February 12, 2024

This article shows which commands best to use when...
Read more
Linux File Command: How to Determine File Type in Linux
March 3, 2022

The file command in Linux determines the type of a file. Learn how to use the file command in this...
Read more