Tutorials  /  Linux Basics

How to Use the zip Command in Linux: Complete Guide

Ccentron Redaktion · August 2025 ·7 min read ·Linux Basics, Tutorial

The zip command in Linux is a versatile tool designed to compress both files and directories into a unified .zip archive. This compression helps reduce file size and conveniently bundles multiple items for easier storage or faster data transmission. The .zip format is widely supported across different operating systems including Windows, macOS, Linux, and Unix, making it ideal for cross-platform file handling.

This guide covers how to leverage the zip command in Linux to efficiently compress and manage your files.

VM

Try it yourself: cloud servers from €3.12/month

ccloud³ VMs from German data centers – deployed in seconds, billed by the hour, with a €200 starting credit for new accounts. Launch a server →

Linux zip Command: Syntax Overview

Here's the general syntax used for the zip command:

Code
zip [options] zipfile files

Explanation of components:

  • [options]: Optional flags that adjust how the command functions.
  • zipfile: The name you assign to the output archive.
  • files: The specific files or directories you want to compress.

Creating Test Files and Directories

Follow these steps to generate sample files and directories to test out the zip command on your Linux system:

  1. Navigate to your user’s home directory:
Console
$ cd
  1. Create several text files like file.txt, file1.txt, file2.txt, newfile.txt, and file.log:
Console
$ touch file.txt file1.txt file2.txt newfile.txt file.log
  1. Generate directories named dir and dir1. Inside dir1, add a sub-directory called sub_dir:
Console
$ mkdir -p dir dir1/sub_dir
  1. Change into the dir directory:
Console
$ cd dir
  1. Inside the dir folder, create files like file1.txt, file2.txt, sys.log, and auth.log:
Console
$ touch file1.txt file2.txt sys.log auth.log

Frequently Used zip Command Flags

Below is a list of commonly used zip command options and their functions:

Option Description
-r Recursively compress directories and all their contents.
-j Exclude directory names from the archive; only include files.
-m Move files into the archive, deleting originals after compression.
-u Update archive contents with newer versions of files.
-d Remove specific entries from the archive.
-x Exclude files or patterns you don't want to compress.
-q Enable quiet mode to suppress command output.
-v Activate verbose mode to show detailed process information.
-9 Apply the highest level of compression for minimal archive size.
-e Encrypt the archive with a password for extra security.

Run Practical zip Command in Linux with Examples

Follow these instructions to apply real-world examples of the zip command in Linux:

Compressing a Single File

Package a file named file.txt into a zip archive called archive.zip:

Console
$ zip archive.zip file.txt

Compressing Multiple Files

Combine file.txt, file1.txt, and file2.txt into one archive:

Console
$ zip archive.zip file.txt file1.txt file2.txt

Compressing a Directory

Zip the entire dir directory, including its subfolders and files:

Console
$ zip -r archive.zip dir/

Excluding Specific Files

Create an archive without including files that have the .log extension:

Console
$ zip -r archive.zip dir/ -x "*.log"

Appending Files to an Archive

Include newfile.txt into the existing archive.zip file:

Console
$ zip -u archive.zip newfile.txt

Listing Archive Contents

Display the internal file list of archive.zip using the -sf option:

Console
$ zip -sf archive.zip

Overwriting or Updating Files

Refresh file1.txt and file2.txt in the archive if newer versions are available:

Console
$ zip -o archive.zip file1.txt file2.txt

Removing Files from Archive

Erase file1.txt from the archive.zip file:

Console
$ zip -d archive.zip file1.txt

Unzipping Archive

Extract all files stored in archive.zip to the current directory:

Console
$ unzip archive.zip

Applying Best Compression

Use maximum compression on file1.txt and file2.txt:

Console
$ zip -9 archive.zip file1.txt file2.txt

Encrypting the Archive

Create a password-protected zip archive for file1.txt and file2.txt:

Console
$ zip -e archive.zip file1.txt file2.txt

Use Advanced zip Command Options

Excluding Directory Structure

Compress files without retaining their original folder paths:

Console
$ zip -j archive.zip /path/to/file1.txt /path/to/file2.txt

Wildcard Compression

Compress all .txt files using the wildcard symbol:

Console
$ zip archive.zip *.txt

Verbose Mode

Enable verbose output while compressing files:

Console
$ zip -v archive.zip file1.txt file2.txt

Encrypt and Verbose Output

Encrypt and display compression details simultaneously:

Console
$ zip -e -v archive.zip file1.txt file2.txt

Archive and Delete Originals

Compress files and remove the source versions post-archiving:

Console
$ zip -m archive.zip file1.txt file2.txt

Extract to Target Directory

Unzip archive.zip contents into the dir1 directory:

Console
$ unzip archive.zip -d dir1/

Use Multiple Compression Levels

Create an archive using recursive and highest compression together:

Console
$ zip -r -9 archive.zip dir/

Managing Archive Conflicts

Update only .txt files in the archive if they are newer, using the -n flag:

Console
$ zip -u archive.zip *.txt -n .txt

Combine zip with Other Commands

You can integrate the zip command with other Linux commands to address more advanced scenarios. Below are several examples:

Compress Recently Modified Files

Use the find and zip commands together to identify and compress files that have been updated within the past 24 hours in the /path/to/your/files directory. The -@ option reads file paths from the output of find and includes them in the archive.zip file:

Console
$ find /path/to/your/files -type f -mtime -1 -print | zip archive.zip -@

Compress Files Based on Size

Locate and compress files that are larger than 10MB using find and zip. The resulting archive is named large_files.zip:

Console
$ find /path/to/your/files -type f -size +10M -print | zip large_files.zip -@

Stream Output into zip

Combine the output of ls -l /path/to/your/files with zip. This approach lists the directory contents and compresses the result into archive.zip using the hyphen - to read from standard input:

Console
$ ls -l /path/to/your/files | zip archive.zip -

Conclusion

In this tutorial, you learned how to use the zip command on Linux to compress files and directories. This tool helps minimize disk space usage, accelerates file transfers, and simplifies file sharing. To explore additional options and configuration settings, run man zip to open the manual page.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren