Enter your email address below and subscribe to our newsletter

How to Zip Files and Directories in Linux

Learn how to zip files and directories in Linux using command line and GUI.

Share your love

When you want to share hundreds of files, the most efficient option is to compress those files into one. It will compress the size of the total files and also allow you to add a passcode for security reasons.

On Linux, you have plenty of options to compress files, such as creating a tarball file. But in this tutorial, I will walk you through two ways you can zip files and directories in Linux:

  1. Using the zip command (to create and extract zip files from the command line)
  2. Using GUI (it requires a user to have zip CLI utility installed in their system)

And in this tutorial, I will walk you through both ways so you can choose what suits the best for your workflow.

Zip files and directories in Linux

Before I jump to the how-to part, it is necessary to install the required prerequisite to use any of the two methods that I’m about to share to zip files and directories.

To zip and unzip files in Linux, you have to have zip and unzip utilities installed on your system. You can follow the given commands based on what Linux distro you use.

For Ubuntu/Debian-based distros:

sudo apt install zip unzip

For Fedora/RHEL-based distros:

sudo dnf install zip unzip

For Arch-based distros:

sudo pacman -S zip unzip

Once you are done, you can refer to any of the two given methods to zip files and directories in Linux.

Method 1: Use terminal to zip files and directories

Using a terminal might seem like a huge deal, but after you get comfortable, there’s no going back. The same goes for zipping files and directories in the terminal.

To use the zip command to create a zip file of multiple files and directories, you can refer to the following command syntax:

zip [options] <File(s) or Directory(ies)>

Here,

  • [options]: here’s where you specify one or more options to fine-tune the output of the zip command. For example, you can use the -u option to update the existing zip files.
  • <File(s) or Directory(ies)>: here, you specify one or more files/directories to create a zip file. When specifying more than one file/directory, separate them with space.

Now, let’s take a look at some examples of how you can zip files in Linux through the command line.

1. Zip a single file

To zip a single file, all you have to do is specify the archive name ending with .zip and append the name of the file to the archive as shown here:

zip Archive.zip Filename

For example, if I want to create an archive Image.png file in myzip.zip, then I’ll use the given command:

zip myzip.zip Image.png
Zip a single file in Linux

As you can see, it created a zip file called myzip.zip containing the Image.png file.

2. Zip multiple files

To zip multiple files, all you have to do is append multiple filenames to the zip command in the following manner:

zip Archive.zip File1 File2 File3 

For example, here, I created a zip file named images.zip containing multiple image files:

zip images.zip image1.png image2.png image3.png
Create zip file of multiple files in linux

3. Zip a directory

If you want to create a zip file of a directory, then you have to use the -r flag to enable recursive action. Here’s how you can use the -r flag to zip directories:

zip -r Archive.zip Directory-name/

Let’s say I want to create a zip file named Kabir.zip of the GG directory, then I’ll be using the following command:

zip -r Kabir.zip GG/
Create zip file of a directory in Linux

4. Create a password-protected zip file

If you want to create a password-protected zip file, all you need to do is add the -e (for encrypting a zip file) flag with the zip command, and it will prompt you to enter the password while creating the zip file:

zip -e Archive.zip File(s)/Directory(ies)

For example, here, I created a password-protected zip file Paas.zip while zipping the GG directory:

zip -er Pass.zip GG/
Create a password protected zip file in linux

Sure, you can append the password with the -P flag, but it will be visible in your bash history, so I avoided that option for privacy reasons.

5. Unzip file

At the beginning of this tutorial, when I gave you a command to install a zip utility, I also included the unzip utility as you’ll end up installing it anyways. And as the name suggests, it is used to unzip files.

To unzip files, append the filename (ending with the .zip) to the unzip command and it will unzip all the files in the current working directory:

unzip Archive.zip

For example, here, unzipped the Images.zip file, and it extracted every file in the current working directory:

unzip Image.zip
how to unzip files in linux

But if you want to unzip a file to a specific directory, use the -d flag specifying the path where to extract the files. The best part is it will retain the directory structure while unzipping the file:

unzip -d /path/to/extract Archive.zip

For example, if I want to unzip the Pass.zip file inside the mydir directory located inside the home directory, then I’ll be using the following:

unzip -d Pass.zip ~/mydir
Unzip file to different directory while keeping the directory structure

Method 2: Use GUI to zip files and directories

If you’re a beginner and not comfortable using a terminal, then you can use a graphical user interface (GUI) to zip files and directories in Linux.

Note: Using GUI also requires users to have zip and unzip installed in their system, so make sure to execute the commands that I’ve mentioned at the beginning of this guide before you proceed any further.

To zip files and directories in Linux using GUI, first open your file manager.

Now, select one or more files/directories, right-click and select the compress option:

Compress files using GUI in Linux

Once you select the Compress option, you will have to follow the given steps to proceed further:

  1. Enter the zip file name
  2. Select the .zip from the dropdown menu. You can also choose the second option to password-protect the zip file.
  3. Once you choose between any of the two given options, click on the Create button.

Sounds confusing? Here’s how you do it:

Create a password protected zip file in GUI in linux

That’s it!

Similarly, if you want to unzip the file in GUI, you follow the given steps:

  1. Select the .zip file
  2. Choose between Extract here (to unzip all the files in the current directory) and Extract to... (to extract files in another directory)
  3. Optionally, you’d have to enter the password if the file is password-protected.

Yes, that’s all it takes to zip files and directories in Linux using GUI.

Wrapping Up…

In this tutorial, I went through how you can zip files and directories using a terminal and GUI. In brief, I also went through how you can unzip those files (sure, there are more options to do so).

If you want to learn more about how you can use the zip and unzip commands, let us know in the comments and we’ll come up with a requested tutorial.

Share your love
Kabir
Kabir

A tech journalist whose life revolves around networks.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!