How to Zip and Unzip Files & Folders in Linux/Ubuntu (Full Guide with Examples)

 

How to Zip and Unzip Files & Folders in Linux/Ubuntu (Full Guide with Examples)

Compressing and extracting files is one of the most common tasks in Linux. Whether you want to reduce file size, transfer multiple files easily, or secure your data with a password, Linux provides powerful tools to Zip and UnZip files.

In this complete guide, you’ll learn:

✔ What ZIP files are
✔ How to install zip/unzip tools
✔ How to zip files & folders
✔ How to unzip safely
✔ Password-protecting archives
✔ Excluding files
✔ Overwriting behavior
✔ Listing ZIP content
✔ Practical examples


What is a ZIP File?

A ZIP file is a compressed archive that contains one or more files or folders.
Benefits:

  • ✔ Reduced file size

  • ✔ Easy to share multiple files

  • ✔ Supports password protection

  • ✔ Can split files into smaller parts

  • ✔ Lossless compression

Linux uses zip to compress and unzip to extract files.


🟩 Installing Zip and Unzip in Linux

Most Linux distributions do not come with zip/unzip installed. Install them using:


Install on Ubuntu/Debian

sudo apt install zip unzip

Install on CentOS/Fedora/RHEL

sudo yum install zip unzip

🟦 How to Zip Files and Folders in Linux

Let’s learn how to CREATE zip files first.


1️⃣ Zip a Single File

zip myfile.zip file.txt

2️⃣ Zip Multiple Files

zip archive.zip file1 file2 file3

3️⃣ Zip a Folder

zip -r project.zip project_folder/

-r means recursive → includes all files & sub-folders.


4️⃣ Create a ZIP with Password

zip -r -e secret.zip myfolder/

This command will prompt for a password.


5️⃣ Exclude Files While Zipping

zip -r backup.zip /home/user --exclude '*.mp4' '*.tmp'

6️⃣ Zip with Maximum Compression

zip -9 final.zip file.txt

🟥 How to Unzip (Extract) ZIP Files in Linux

Now let’s extract ZIP files using the unzip command.


1️⃣ Unzip to Current Directory

unzip filename.zip

You must have write permission in the directory.


2️⃣ Suppress Output (Silent Mode)

unzip -q filename.zip

3️⃣ Unzip to a Different Directory

unzip filename.zip -d /path/to/folder

Example:

sudo unzip latest.zip -d /var/www

Note: Using sudo makes extracted files owned by root.


4️⃣ Unzip a Password Protected ZIP

✔ Using password in command:

unzip -P MyPassword secret.zip

Not recommended — password is visible.

✔ More secure method:

unzip secret.zip

It will ask for a password:

[secret.zip] file.txt password:

5️⃣ Exclude Files When Unzipping

unzip filename.zip -x "*.git/*"

6️⃣ Overwrite Files Automatically

unzip -o filename.zip

⚠ This overwrites existing files without warning.


7️⃣ Unzip Without Overwriting (Skip Existing Files)

unzip -n filename.zip

Useful when:

  • You modified some files

  • You want to restore deleted files only


8️⃣ Unzip Multiple ZIP Files at Once

unzip '*.zip'

✔ Use single quotes → prevents shell expansion.


9️⃣ List the Contents of a ZIP File

unzip -l filename.zip

Example output:

Archive: latest.zip Length Date Time Name --------- ---------- ----- ---- 3065 2021-08-31 18:31 wordpress/xmlrpc.php ... 27271400 1648 files

🔵 Useful Zip & Unzip Tips

✔ Test ZIP File Integrity

unzip -t filename.zip

✔ Extract Only Specific File

unzip archive.zip file.txt

✔ Extract to stdout (show file content)

unzip -p archive.zip file.txt

🟣 Conclusion

ZIP files are an efficient way to compress, secure, and transfer files on Linux.
Using the zip and unzip commands, you can:

  • Create ZIP archives

  • Extract files safely

  • Protect ZIPs with passwords

  • Exclude or overwrite files

  • List ZIP contents

  • Extract anywhere

Linux provides full control and flexibility over compression tools.

Previous Post Next Post

نموذج الاتصال

Telegram