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.
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
The most basic usage of the zip command looks like this:
bash
CopyEdit
zip archive_name.zip file1 file2 file3
Example:
bash
CopyEdit
zip mydocs.zip resume.pdf notes.txt budget.xlsx
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.
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.
The zip command lets you control the level of 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).
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.
Let’s walk through a real scenario to reinforce what you’ve learned:
bash
CopyEdit
mkdir test_folder
cd test_folder
bash
CopyEdit
touch sample1.txt sample2.c sample3.md
bash
CopyEdit
cd ..
zip -r my_test_archive.zip test_folder
bash
CopyEdit
unzip -l my_test_archive.zip
This displays all files and folders inside the archive without extracting them.
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
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
Mastering the zip command in Linux empowers you to:
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.
If you’re still having trouble, consider reaching out to Support.Com for a personalized solution to all technical support issues.