Ubuntu: How to zip/unzip files and directories

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Compressing files is an efficient way to reduce their size and keep related files together. When it comes to Linux, specifically Ubuntu, this task can be accomplished using various tools. Among these, zip and unzip are some of the most widely used for dealing with ‘.zip’ files. This tutorial will guide you through different methods to zip and unzip files and directories in Ubuntu, starting from basic examples to more complex scenarios.

Prerequisites

Before starting, you need to ensure that the zip and unzip utilities are installed on your Ubuntu system. You can install them using the following commands:


sudo apt update
sudo apt install zip unzip

Basic Usage of zip and unzip

To create a basic zip file, you can use the following command:


zip myarchive.zip file1.txt file2.txt

The above command will create a zip file called ‘myarchive.zip’ containing ‘file1.txt’ and ‘file2.txt’.

To unzip a file, you simply use:


unzip myarchive.zip

This command will extract the contents of ‘myarchive.zip’ into the current directory.

Zipping Directories

To include a directory and all its contents in a zip file, you use the -r (recursive) option:


zip -r myarchive.zip mydirectory/

This command will zip the ‘mydirectory’ and all its subdirectories and files into ‘myarchive.zip’.

Excluding Files and Directories

If you want to exclude certain files or directories when zipping, you can use the -x option:


zip -r myarchive.zip mydirectory/ -x \*.git\

This command will zip ‘mydirectory’ but exclude any ‘.git’ folders.

Splitting Zip Files

Zip files can be split into smaller parts to fit into media like CDs or to send via email. The following example creates split archives of specified size:


zip -r -s 64k mysplitarchive.zip mylargefile.mkv

The above command will create a zip file in parts of 64KB each.

Encrypting Zip Files

To create a zip file with a password, use the -e option:


zip -e securearchive.zip file1.txt

After running the command, you will be prompted to input a password. Remember to use strong passwords to protect your file effectively.

Using unzip to List and Extract selectively

To list the content of a zip file without extracting, you can:


unzip -l myarchive.zip

If you need to extract a specific file from a zip file, specify it after the zip file name:


unzip myarchive.zip file1.txt

To exclude specific files when extracting, use the -x option:


unzip myarchive.zip -x file2.txt

Crafting zip Commands with Pipes and Redirects

Advanced users can use zip with pipes and redirects. For example, to zip the output of a command, you can:


find . -name \*.log | zip logs.zip -@

This finds all ‘.log’ files in the current directory and zips them into ‘logs.zip’.

Advanced unzip operations

The unzip utility can be used to test the integrity of a zip file:


unzip -t myarchive.zip

You can also use special flags to control the overwrite behavior during extraction:


unzip -o myarchive.zip # Always overwrite
unzip -n myarchive.zip # Never overwrite

Working with Permissions and Attributes

To preserve file permissions during zipping, use the -X option:


zip -X myarchive.zip script.sh

Conversely, unzip -X myarchive.zip restores the original permissions saved in the zip file.

Using Graphical Interface

Ubuntu also offers graphical tools to zip and unzip files. The default file manager, Nautilus, provides right-click context menus to compress and extract files with a GUI rather than the command line.

Conclusion

Understanding how to zip and unzip files is a fundamental skill in managing files and directories. With command-line tools like zip and unzip, you gain both precision and the ability to script these processes, which can be invaluable for batch jobs and automation on your Ubuntu system.