Friday, September 20, 2024
Google search engine
HomeGuest BlogsHow to Zip a File in Linux

How to Zip a File in Linux

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 LinuxHow 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 outputzip --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.

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 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 outputCreate 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 outputzip -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 outputzip 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 outputzip -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 outputzip -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 outputzip -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 outputzip 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 optionUbuntu GUI Compress option

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

Create archive ZIP GUI UbuntuCreate 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

RELATED ARTICLES

Most Popular

Recent Comments