Table of content:
Linux Zip Command Explained: Syntax, Working, Options, & Examples

Ever felt overwhelmed by a messy directory full of files and folders on your Linux system? Whether you're organizing your workspace, saving storage, or preparing files for sharing, compression is a quick and efficient solution. How? Well, surely we are talking about the zip command in Linux–a simple yet powerful tool to bundle and compress files in seconds.
In this article, we’ll walk you through everything you need to know about the Linux zip command–how it works, key options, real examples, and pro tips to make the most of it.
What Is Linux Zip Command?
The zip command in Linux is a widely used utility that compresses one or more files or directories into a single .zip archive file. This not only reduces the size of the files but also makes them easier to store, transfer, or back up. Whether you're organizing logs, sharing project files, or prepping for deployment, Linux zip command will be a blessing.
Basic Syntax of the zip Command in Linux
zip [options] archive_name.zip file1 file2 …
Here,
- zip: The command to invoke the zip utility.
- [options]: Optional flags that modify the behavior of the command (like recursion, compression level, encryption, exclusion, etc.).
- archive_name.zip: The name you want to give your compressed file (with the .zip extension).
- file1 file2 ...: One or more files or directories you want to compress.
Basic Example of Linux Zip Command
zip project.zip index.html style.css script.js
This command creates a compressed archive named project.zip that contains the three listed files. Next, we will discuss how to ensure the zip command is actually available on your system and how to install it if it’s not.
Check If Linux Zip Is Installed / How To Install It
Before using the zip command, it's important to verify whether it's already installed on your Linux system. Most modern Linux distributions, like Ubuntu 20.04 and CentOS 8, come with the zip utility pre-installed. However, if it's missing, you can easily install it using your system's package manager.
How to Check if zip is Installed?
To check if zip is installed, run the following command in your terminal:
zip --version
If zip is installed, this command will display the version information, such as:
Zip 3.0 (July 5th 2008), by Info-ZIP
If it’s not installed, you'll see an error like:
Command 'zip' not found
How to Install zip in Linux?
If the zip command isn’t installed, you can install it by running the appropriate command for your Linux distribution.
➤ For Ubuntu / Debian-based systems:
sudo apt update
sudo apt install zip
➤ For Red Hat / CentOS / Fedora:
sudo yum install zip
Or, if you're using dnf (on newer Fedora versions):
sudo dnf install zip
➤ For Arch Linux:
sudo pacman -S zip
Once installed, you can verify again using:
zip --version
Also read: Introduction To Linux Shell, Features, Types, Scripting & More
How Linux Zip Command Works
The zip command in Linux is used to compress files and directories into a .zip archive. This archive can then be stored, transferred, or backed up more easily. Let’s break down how it works step by step.
Basic Flow of the Linux zip Command:
- Choose Files or Directories to Compress: You can specify multiple files or even whole directories.
- Run the zip Command: Use the syntax to compress your chosen files into a .zip archive.
- View the Archive Contents (Optional): Use additional options to verify what’s inside without extracting.
Example 1: Compressing Files into a Zip Archive
To create a .zip archive, simply run:
zip archive_name.zip file1.txt file2.txt
What happens:
- This command compresses file1.txt and file2.txt into a .zip file named archive_name.zip.
- The resulting file, archive_name.zip, will contain the two text files in compressed form.
Example 2: Compressing a Directory
To compress an entire directory, use the -r (recursive) option:
zip -r archive_name.zip directory_name
What happens:
- The -r option tells zip to compress the directory and all its contents (subdirectories, files, etc.).
- After running this, you’ll get a .zip archive containing everything inside directory_name.
Example 3: Adding More Files to an Existing Zip Archive
You can even add files to an existing .zip archive without overwriting it:
zip archive_name.zip file3.txt
What happens:
- This adds file3.txt to the archive_name.zip without modifying the other files already in the archive.
Linux Zip Options (Detailed Explanations With Examples)
The zip command in Linux offers a variety of options (or flags) that allow you to customize how files are archived and compressed. These options help you update, exclude, include, encrypt, or change compression behavior as per your needs.
Whether you're zipping a few files or managing large archives, understanding these options will help you tailor the zip command precisely. Below are the most commonly used zip options in Linux, each with a specific purpose.
Linux Zip -u Option (Update Existing Files)
The -u option allows you to update files in an existing ZIP archive by adding new files or replacing outdated versions. This is particularly useful when you need to modify or append files to an existing archive without creating a new one from scratch. It eliminates the need to re-compress everything, saving time and space.
Command:
$ zip -u file_name.zip file.txt
Explanation:
In this example, file_name.zip is an existing ZIP archive. The file.txt file will be checked.
- If it already exists in the archive and is different from the current version, it will be updated.
- If the file doesn't exist in the archive, it will be added.
Linux Zip -d Option (Delete File From Archive)
The -d option is used to delete files from an existing ZIP archive. This option is useful when you remove unwanted or obsolete files from a ZIP archive, without the need to manually extract and recreate the archive (like to clean up or reorganize the contents).
Command:
$ zip -d file_name.zip file.txt
Explanation:
In this example, file_name.zip is the target ZIP file, and file.txt is the file to be removed. After running the command, file.txt will be deleted from the archive, but not from the original file system.
Linux Zip -m Option (Move Files After Zipping)
This Linux zip options will move the original files to the archive, meaning that the original files are deleted after being successfully added to the ZIP archive. This option is useful when you want to compress files and free up space on your system in one step.
Command:
$ zip -m file_name.zip file.txt
Explanation:
In this case, file_name.zip is the archive you're creating, and file.txt is the file to be compressed and moved. After the ZIP operation completes, the original file.txt will be removed from the directory, leaving only the compressed archive.
Linux Zip -x Option (Exclude Specific Files)
The -x Linux zip option lets you exclude certain files from being added to the archive. This is helpful when you want to compress an entire directory but need to omit specific files or file types, like log files or temporary files.
Command:
$ zip -x "*.log" archive.zip *
Explanation:
Here, archive.zip is the target archive, and * is a wildcard that tells zip to include everything in the current directory. The -x "*.log" portion excludes any .log files from being added to the archive as they might not be relevant to the archive's purpose.
Linux Zip -r Option (Recursive Compression)
The -r option enables recursive zipping, meaning that it will compress not just files in the specified directory, but also files within any subdirectories. This is essential for archiving directories with a hierarchical structure, ensuring everything gets included.
Command:
$ zip -r archive.zip directory_name
Explanation:
In this example, archive.zip will be the final compressed archive, and directory_name is the folder to be compressed. The -r option tells zip to include all files and subdirectories within directory_name, so the archive will contain the entire folder structure.
Linux Zip -v Option (Verbose Output)
The Li=nux zip option -v is used to display verbose output during the zipping process. That is, it provides more detailed information about the compression progress and the structure of the zip file. It is helpful for advanced users or those troubleshooting specific issues during compression.
Command:
$ zip -v archive.zip file.txt
Explanation:
In this example, archive.zip is the target zip file and file.txt is the file being added. The -v flag triggers detailed information about the compression process, including the compression ratio, file size, and other diagnostic data. If you’re working with larger files or need more insight into the process, this flag can help you track what’s happening in real-time.
Linux Zip -q Option (Quiet Mode)
The -q option is used to suppress the output of the zip command, running it silently. This Linux zip options is ideal when you want to create an archive without cluttering the terminal with progress messages or other informational details.
Command:
$ zip -q archive.zip file.txt
Explanation:
Here, archive.zip is the resulting archive and file.txt is the file to be compressed. By using the -q option, the terminal will not show the usual progress output, keeping it quiet.
Linux Zip -z Option (Add a Comment to Archive)
The -z option allows you to add a comment to your zip archive. This can be helpful when you want to document the contents or purpose of the archive. The comment is stored within the zip file and can be viewed by anyone extracting it.
Command:
$ zip -z archive.zip file.txt
Explanation:
In this case, archive.zip is the zip file being created and file.txt is the file being added. After running the command, you will be prompted to enter a comment for the archive. This can serve as a reminder or additional information for others who may use the archive in the future.
Linux Zip -e Option (Encrypt Archive with Password)
The -e option encrypts the contents of your zip archive, requiring a password to extract the files. This adds an extra layer of security to your archived files, making it ideal when you’re dealing with sensitive data that needs to be protected. But keep in mind that if you forget the password, the files inside the archive will remain inaccessible.
Command:
$ zip -e archive.zip file.txt
Explanation:
In this example, archive.zip is the archive being created and file.txt is the file being added. After running this command, you will be prompted to enter a password. The contents of the archive will be encrypted, and anyone who attempts to extract it will need the password to do so.
Linux Zip -l Option (Convert Line Endings for DOS Compatibility)
The -l option ensures that Unix-style line endings (LF) are converted to DOS/Windows-style line endings (CR LF). This Linux zip option is useful when you need to ensure compatibility for text files across different operating systems, particularly if you plan to extract the archive on a Windows system.
Command:
$ zip -l archive.zip file.txt
Explanation:
In this example, archive.zip is the destination archive, and file.txt is the text file being compressed. When the archive is extracted on a DOS/Windows system, the -l option ensures that the line endings in file.txt are properly converted to CR LF format, ensuring that the text file will open correctly on Windows without issues.
Linux Zip -o Option (Overwrite Existing Files in Archive)
The -o option allows you to overwrite existing files in a ZIP archive without being prompted for confirmation. This is useful for automating processes where you don't want to manually approve each overwrite action. For example, with automated tasks or scripts that need to update archives without user interaction.
Command:
$ zip -o archive.zip file.txt
Explanation:
Here, archive.zip is the target archive, and file.txt is the file being added. If file.txt already exists in archive.zip, the -o option will overwrite it without asking for confirmation.
Linux Zip -j Option (Store Files Without Directory Paths)
The -j option removes the directory structure and only stores the files with their original names in the archive. This is useful when you want to compress files but don’t need to preserve the original folder hierarchy.
Command:
$ zip -j archive.zip directory/file.txt
Explanation:
In this case, archive.zip will contain only the file file.txt without any path information. If file.txt is located in the directory/ folder, using -j will store just the file name in the zip archive.
Linux Zip -i Option (Include Specific Files)
The -i option is used to include only certain files or directories when creating a zip archive. This Linux zip option is helpful when you need to be very specific about which files should be included in the archive, especially when using wildcards.
Command:
$ zip -r archive.zip . -i "*.txt"
Explanation:
In this example, the archive.zip archive will contain only files that match the *.txt pattern (i.e., all text files in the directory).
Linux zip -0 to -9 Options (Setting the Level of Compression in Zip Files)
The zip command in Linux lets you control how much compression is applied to the files you're archiving using flags from -0 to -9. This is helpful when balancing speed versus storage savings.
Level |
Compression |
Speed |
Use Case |
-0 |
None |
Fastest |
Already-compressed files (e.g., .mp4, .zip) |
-1 |
Low |
Very Fast |
When speed matters more than size |
-6 |
Default |
Balanced |
General-purpose compression |
-9 |
Maximum |
Slowest |
When minimizing file size is a priority |
These flags let you fine-tune how zip behaves, especially when working with large files or when optimizing for speed vs. space.
Example (Maximum Compression):
$ zip -9 files1.zip *.txt
Explanation:
This compresses all .txt files into files1.zip using the most aggressive compression. It will take more time but produce the smallest possible archive–great for saving space or uploading over a slow network.
Example (No Compression):
$ zip -0 files2.zip *.txt
Explanation:
This adds all .txt files to files2.zip without compressing them. Useful if the files are already compressed (e.g., images, videos) or when speed matters more than archive size.
Linux Zip Options: A Quick Reference Table
Option |
Purpose |
Example |
-u |
Update archive |
zip -u archive.zip file.txt |
-d |
Delete file from archive |
zip -d archive.zip file.txt |
-m |
Move (zip + delete original) |
zip -m archive.zip file.txt |
-x |
Exclude files |
zip -x archive.zip "*.log" |
-r |
Recursively zip folders |
zip -r archive.zip folder |
-v |
Verbose output |
zip -v archive.zip file.txt |
-q |
Quiet mode |
zip -q archive.zip file.txt |
-z |
Add a comment |
zip -z archive.zip file.txt |
-e |
Encrypt the archive |
zip -e archive.zip file.txt |
-l |
Convert line endings |
zip -l archive.zip file.txt |
-o |
Overwrite existing files |
zip -o archive.zip file.txt |
-j |
Strip path info (junk the path) |
zip -j archive.zip dir/file.txt |
-i |
Include specific files |
zip -r archive.zip . -i "*.txt" |
-0–9 |
Set compression level |
zip -9 archive.zip file.txt |
Advantages Of Using Zip Command In Linux
The zip command remains a go-to utility for compressing files in Linux due to its simplicity and cross-platform compatibility. Here are some key benefits:
- Cross-platform support: ZIP files are widely used and easily opened on Windows, macOS, and Linux, making them ideal for sharing files across systems.
- Built-in compression: It reduces file size, saving disk space and speeding up file transfers–especially useful for backups and emails.
- Selective control: Options like -u, -d, and -x give you precise control over what to update, remove, or exclude in an archive.
- Supports encryption: With the -e option, you can password-protect ZIP files for added security.
- Simple to automate: Works well with shell scripts and can be combined with other commands for automated workflows.
Whether you're packaging logs, bundling reports, or prepping a project folder, the zip command is a reliable and efficient choice.
How To Display Content Of A File Without Unzipping
Sometimes, you may want to quickly check the contents of a file inside a ZIP archive without extracting it. This is especially useful when you're not sure which file you need or just want to verify something before unzipping. Fortunately, Linux provides simple ways to view archived content directly from the terminal.
Using the zipinfo Command (Detailed View of ZIP Contents)
The zipinfo command gives even more detail than unzip -l. It displays file permissions, compression ratio, modification times, and more.
Command:
zipinfo archive.zip
Explanation:
- zipinfo is a separate utility designed to analyze ZIP files.
- It shows detailed metadata about each file within the archive–ideal for reviewing archive contents before deciding what to extract.
Can You View File Contents Inside a ZIP Without Extracting?
Unlike .tar.gz or .gz files where you can use commands like zcat or less, ZIP archives do not support direct in-terminal content preview of individual files (like viewing text files line-by-line) without some form of extraction.
However, you can extract a single file to a temporary location and display it, like this:
unzip -p archive.zip file.txt | less
Explanation:
- -p extracts the file to standard output (stdout) without saving it to disk.
- file.txt is the specific file you want to preview inside archive.zip.
- | less allows you to scroll through the output interactively in the terminal.
Use unzip -p when you want to read a file inside a ZIP archive directly in the terminal without permanently extracting it.
Using the unzip -l Command (List Files in Archive)
The -l option with the unzip command lists all the files and directories contained in a ZIP archive without extracting them. This gives you a clear overview of what's inside before performing any further action.
Command:
unzip -l archive.zip
Explanation:
- unzip is the command-line utility used to work with ZIP archives.
- -l stands for “list.” It lists the contents of archive.zip including filenames, sizes, and timestamps.
- This command does not extract any files; it simply shows the internal structure of the archive.
How To Unzip A File In Linux
Unzipping a file in Linux is a straightforward task with the unzip command. Whether you’re dealing with a single file or an archive containing multiple files, the process is quick and efficient.
To unzip a ZIP archive in Linux, simply use the following command:
unzip archive.zip
Explanation:
- unzip is the command used to extract files from a ZIP archive.
- archive.zip is the name of the ZIP file you want to extract.
This will extract the contents of archive.zip into the current directory.
For more detailed steps, troubleshooting tips, and additional options, check out: How To Unzip A File In Linux | Unzip Syntax | 8 Unzip Commands
Conclusion
The zip command in Linux is a powerful and flexible tool for compressing and managing files. Whether you're looking to save disk space, transfer files efficiently, or create backups, mastering the zip command is essential for streamlining your workflow. With various options for customization, from excluding specific files to adjusting compression levels, it offers everything you need for effective file management.
If you're new to Linux or the Linux zip command, you now have the foundation to get started. To further enhance your skills, explore other related tools like unzip and dive into more advanced options as needed.
Frequently Asked Questions
Q1. Can we combine Linux zip with other commands?
Yes, the zip command can be combined with other Linux commands for advanced use cases. For example, you can use it with find to zip files based on specific conditions or with tar to first compress a group of files and then archive them. Here's an example that uses find:
$ find . -name "*.txt" | zip archive.zip -@
This command zips all .txt files found in the current directory.
Q2. How do I add a password to a ZIP file in Linux?
To add a password to a ZIP archive, use the -e option with the zip command. This option will prompt you to enter a password when the archive is created:
$ zip -e archive.zip file1.txt file2.txt
Q3. Can I zip files with different compression levels?
Yes, you can adjust the compression level using the -0 to -9 options, where -0 represents no compression (fastest) and -9 represents maximum compression (slowest). For example:
$ zip -9 archive.zip *.txt
This command uses the highest compression level.
Q4. How can I exclude specific files from being added to a ZIP archive?
To exclude files, use the -x option. For example, if you want to exclude all .log files:
$ zip -r archive.zip * -x "*.log"
This command will zip all files except for .log files.
Q5. How can I see the contents of a ZIP file without extracting it?
You can view the contents of a ZIP file without unzipping it by using the -l (list) option:
$ zip -l archive.zip
This will display a list of files contained in the ZIP archive.
Suggested reads:
- How To Open A File In Linux Using 3 Different Approaches
- How To Create A File In Linux: Touch, Cat, Echo, Printf Command
- How To Kill Process In Linux | Ways And Commands To Locate & Kill
- Process Management In Linux | Stages, Types & Commands
- Linux Kernel In A Nutshell To Help You Prepare For Linux Interview Questions