Newsletter Subscribe
Enter your email address below and subscribe to our newsletter
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:
And in this tutorial, I will walk you through both ways so you can choose what suits the best for your workflow.
Table of Contents
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.
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.
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
As you can see, it created a zip file called myzip.zip
containing the Image.png
file.
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
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/
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/
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.
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
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
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:
Once you select the Compress
option, you will have to follow the given steps to proceed further:
.zip
from the dropdown menu. You can also choose the second option to password-protect the zip file.Create
button.Sounds confusing? Here’s how you do it:
That’s it!
Similarly, if you want to unzip the file in GUI, you follow the given steps:
.zip
fileExtract here
(to unzip all the files in the current directory) and Extract to...
(to extract files in another directory)Yes, that’s all it takes to zip files and directories in Linux using GUI.
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.