zip on linux

Linux: ZIP Files and Directories (How To Tutorial)

In this tutorial, we’re going to show you how to zip files and directories/folders on Linux. This tutorial will work on most major distros, like Ubuntu, Linux Mint, Fedora, Debian, CentOS, etc.

ZIP is a utility used to compress/archive files and directories on Linux. It’s available for most major distros.

This tutorial is for the CLI/terminal. All you need is access to the command line and the root/sudo user.

If you want to zip/compress something via the GUI (graphical user interface), then just right-click on the files or folders and click on Compress or Archive.

How to install zip on Linux

The first step is to install zip (and unzip) on Linux.

On some systems, zip may already be installed. Check if zip is installed by running:

zip --v

To install zip, if you’re on Ubuntu, Mint, or Debian, run the following command:

sudo apt install zip unzip

If you’re on Fedora or CentOS, run the following command:

dnf install zip unzip

The installation process should be similar for other distros as well.

How to use zip on Linux

The basic syntax of zip is:

zip -[OPTIONS] zip_name filename1 filename2 foldername3

For a real example, if you wanted to compress your notes into a zip archive:

zip notes notesmonday.txt notestuesday.txt voicenote.mp3

This example command will create a “notes.zip” archive with the two .txt files and one .mp3 file. You’ll get an output for every file that has been added to the archive:

# zip notes notesmonday.txt notestuesday.txt voicenote.mp3
adding: notesmonday.txt (stored 0%)
adding: notestuesday.txt (stored 0%)
adding: voicenote.mp3 (stored 0%)

If you want to add directories (folders) along with their contents to the archive, you’ll need to use the -r (recurse) option.

Example:

zip -r photos Birthday_23 vacation10

This command will create a “photos.zip” archive with the directories “Birthday_23” and “vacation10” along with all the files that are in those directories.

These are the most common use cases for zip. If you need more, check the examples below.

More examples of how to use zip on Linux

If you want to add a password to the archive, use the “-e” option.

So, if you wanted to add a password to the notes.zip archive you’ll be creating, run the following command:

zip -e notes notes1.txt notes2.txt

Then the CLI will prompt you to enter the password two times.

For larger files, if you want to split the archive into multiple parts/files, you can use the “-s” option.

As an example, if you wanted to split the archive into multiple files no larger than 699MB, you need to run:

zip -s 699m -r videos june july august

This example will create a “videos.zip” archive split into multiple no larger than 699MB pieces, containing the folders “june”, “july”, and “august”, and the files in those folders.

Instead of m for MB, you can also use k for kB, g for GB, or t for TB.

If you want to test the archive, you need to use the “-T” option.

For example, if you run:

zip -T videos.zip

It will test the archive you just created and the CLI will output a result like:

test of videos.zip OK

if the test was successful and everything was ok.

To zip multiple/all the files at once, without entering them one by one, you can use the * wildcard.

For example, if you run:

zip random *

It will create a random.zip archive will all the files in the current directory.

You can also use the * wildcard to limit the files you use for zipping.

For example, if you run:

zip books *.txt

It will create a “books.zip” archive with all the .txt files in the current directory.

For more examples and help in using the zip command, run:

zip -h

How to unzip an archive on Linux

To unzip (extract) an archive on Linux, just use the unzip command you previously installed.

The basic syntax is:

unzip file.zip

And with paths and destination folders:

unzip /path/to/archive.zip -d /path/to/destination

For the most simple example, if you have a “notes.zip” archive in the directory you’re currently in and you want to extract it in the same directory, just run:

unzip notes.zip

If you’d want to unzip the archive in a different destination folder, then run:

unzip notes.zip -d JuneNotes

If the archive you want to unzip is in a different directory than the one you’re currently in, and you want to extract the archive to a different directory than the one you’re currently in, run:

unzip /opt/notes.zip -d /home/JuneNotes

That’s about it. We will go in-depth about the unzip command in a different tutorial.

Leave a comment

Your email address will not be published.