How to Zip Files in the Linux Terminal

This guide walks you through everything — from basic usage to advanced compression techniques — using only the Linux terminal.

Zipping files in Linux via the terminal is not only efficient but often essential for organizing, compressing, or sharing files in a single package. The zip command-line utility is the most common tool for this purpose and offers powerful options for compressing files and directories.

This guide walks you through everything — from basic usage to advanced compression techniques — using only the Linux terminal.

Prerequisites

Before starting, ensure the zip utility is installed. Most modern distributions come with it, but if it’s missing, you can install it with:

Debian/Ubuntu:
bash
CopyEdit
sudo apt install zip

    Red Hat/Fedora/CentOS:
    bash
    CopyEdit
    sudo dnf install zip

      Arch Linux:
      bash
      CopyEdit
      sudo pacman -S zip

        1. Basic Syntax of the zip Command

        The most basic usage of the zip command looks like this:

        bash

        CopyEdit

        zip archive_name.zip file1 file2 file3

        What It Does:

        • Creates a file called archive_name.zip.
        • Adds file1, file2, and file3 to it.

        Example:

        bash

        CopyEdit

        zip mydocs.zip resume.pdf notes.txt budget.xlsx

        2. How to Zip an Entire Directory (Recursive)

        If you want to compress a whole folder (including its subfolders and contents), use the -r (recursive) option:

        bash

        CopyEdit

        zip -r archive_name.zip directory_name

        Example:

        bash

        CopyEdit

        zip -r project_backup.zip my_project/

        This compresses the my_project folder and everything inside it.

        3. How to Zip Files by File Type

        You can compress files of a specific type using wildcards:

        bash

        CopyEdit

        zip archive_name.zip *.txt *.pdf

        Example:

        bash

        CopyEdit

        zip text_docs.zip *.txt

        This command adds all .txt files in the current directory to text_docs.zip.

        4. Adjusting Compression Level

        The zip command lets you control the level of compression:

        • -0: No compression (store only)
        • -9: Maximum compression

        bash

        CopyEdit

        zip -9 archive_name.zip file1 file2

        Use Case:
        Use -9 when you need the smallest possible ZIP file (e.g., for email attachments).

        5. Suppress Output for Quiet Mode

        If you want to zip files without displaying progress in the terminal, use the -q option:

        bash

        CopyEdit

        zip -q archive_name.zip file1 file2

        This keeps your terminal output clean, especially in scripts or automation.

        6. Practical Example — Step-by-Step

        Let’s walk through a real scenario to reinforce what you’ve learned:

        Step 1: Create a Test Directory

        bash

        CopyEdit

        mkdir test_folder

        cd test_folder

        Step 2: Create Sample Files

        bash

        CopyEdit

        touch sample1.txt sample2.c sample3.md

        Step 3: Go Back and Zip the Folder

        bash

        CopyEdit

        cd ..

        zip -r my_test_archive.zip test_folder

        Step 4: List Contents of the ZIP Archive

        bash

        CopyEdit

        unzip -l my_test_archive.zip

        This displays all files and folders inside the archive without extracting them.

        7. How to Unzip Files in Linux

        To extract contents of a ZIP archive:

        bash

        CopyEdit

        unzip archive_name.zip

        Example:

        bash

        CopyEdit

        unzip my_test_archive.zip

        This will extract all contents to the current working directory.

        If you only want to test the archive's integrity (without extracting):

        bash

        CopyEdit

        unzip -t archive_name.zip

        Bonus: Useful Flags at a Glance

        Option

        Description

        -r

        Recursive: include directories

        -q

        Quiet mode (suppress output)

        -9

        Max compression

        -0

        No compression

        -v

        Verbose output

        -e

        Encrypt archive with password

        Final Thoughts

        Mastering the zip command in Linux empowers you to:

        • Save space on disk
        • Package and share files easily
        • Automate archiving in scripts
        • Secure files with password-protected ZIPs (using -e)

        Whether you’re a system administrator, developer, or casual Linux user, knowing how to use zip effectively from the terminal is a valuable skill in your toolkit.

        Support.Com Can Help!

        If you’re still having trouble, consider reaching out to Support.Com for a personalized solution to all technical support issues.