Sling Academy
Home/DevOps/Ubuntu: How to zip/unzip files and directories

Ubuntu: How to zip/unzip files and directories

Last updated: January 28, 2024

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.

Next Article: Ubuntu: How to edit files with ‘nano’ text editor

Previous Article: cURL: How to Send API key and Auth Credentials in Requests

Series: Linux Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide