Knowledgebase

Create Zip File With Command in Linux With Examples

Compressing files isn’t just about saving space; it’s about convenience, speed, and efficiency. Imagine sending ten separate files in an email instead of a single, neatly packed zip file. It’s faster, simpler, and more professional!

Whether you’re sharing documents or archiving old data, knowing how to create a zip file in Linux is essential for beginners and seasoned users alike. Linux offers powerful, straightforward zip commands to transform your file management in seconds!

This guide will walk you through the process step by step, making it easy to compress files like a pro!

Let’s get started!

Prerequisites

➔ You should have basic knowledge of the Linux Terminal

➔ You have a Linux distribution installed (like Ubuntu, CentOS, Debian, etc.).

➔ Minimal understanding of file paths

➔ The zip command-line utility is installed.


Also Read: A Guide to Echo Command in Linux With Examples


Installing the Zip Utility

Most Linux distributions come with zip pre-installed. If not, you can install it using:

For Ubuntu/Debian:

sudo apt-get install zip unzip

For CentOS/RHEL:

sudo yum install zip unzip

Also Read: How To Remove Directory in Linux With Command?


How to Create a Zip File?

1. Basic Syntax

The basic syntax of the zip command is:

zip [options] zipfile_name file1 file2 file3

zipfile_name: The name of the zip file you want to create.

file1, file2, file3: The files or folders you want to compress.

Example – 1: Zipping Single File

➔ Let’s say you have a file named example.txt. To create a zip file:

zip example.zip example.txt

This will create a file named example.zip containing example.txt.

Example – 2: Zipping Multiple Files

If you have multiple files, such as file1.txt, file2.txt, and file3.txt, you can zip them together:

zip my_files.zip file1.txt file2.txt file3.txt

This will create a zip archive named my_files.zip containing the three files.

Example – 3: Zipping a Folder

➔ To compress an entire folder, use the -r (recursive) option:

zip -r folder.zip my_folder

My_folder is the folder you want to compress, and folder.zip is the resulting archive.


Also Read: How to Log into SSH using Password Authentication?


Additional Tasks You Can Perform With Zip Command

Create a ZIP Archive

➔ To zip multiple files into a single archive:

zip archive.zip file1.txt file2.txt file3.txt

List ZIP File Contents

➔ You can view the contents of a zip file without extracting it using:

unzip -l archive.zip

This will list all the files inside archive.zip.

Add Specific File Types to the ZIP Archive

➔ If you want to add files of a specific type, such as .txt files, use:

zip text_files.zip *.txt

This will add all .txt files in the current directory to the archive text_files.zip.

Delete Files From ZIP Archive

➔ To delete a file from an existing zip archive, use the -d option:

zip archive.zip -d file1.txt

This removes file1.txt from archive.zip.

Create and Encrypt ZIP File

➔ For better security, you can create a password-protected zip file with:

zip -e secure.zip file1.txt file2.txt

You will be prompted to set a password; the files will only be accessible with that password.


Also Read: How to Create a User in Linux & Add it to the sudoer File?


Advanced Zip Options

Compression Levels

➔ Zip supports compression levels from 0-9, allowing fine-tuned control over file compression:

Here:

  • 0: No compression
  • 9: Maximum compression

Example – 1:

# No compression (fastest)

zip -0 nocompression.zip largefile.iso

Example – 2:

# Default compression

zip -6 defaultcompression.zip documents.txt

Example – 3:

# Maximum compression (slowest)

zip -9 maxcompression.zip largefile.tar

Excluding Files and Patterns

➔ You can customise your zip by excluding specific files or using wildcards:

Example – 1:

# Exclude log files

zip -r project.zip /path/to/project –exclude=*.log

Example – 2:

# Multiple exclusion patterns

zip -r backup.zip /home/user –exclude=*.tmp –exclude=*.cache

Example – 3:

# Exclude entire directories

zip -r code.zip /project/source –exclude=*/node_modules/* –exclude=*/venv/*

Also Read: How To Access Linux Server Using PuTTY SSH Terminal?


Conclusion

The zip command is a powerful and simple tool for compressing files and folders in Linux. Whether you’re managing single files or entire directories, using the zip command can save space and make file transfers easier.

You can take full advantage of the zip utility by exploring additional options like encryption and file management.

Start practising with the examples in this guide; soon, you will be zipping files like a pro!

Hidden-Costs-and-Fees
Affordable Hosting, No Extra Burdens!

Get Pocket-friendly hosting without the headaches