Learn How to Zip and Unzip Files in Linux and be a Zip Master

Have you ever wondered how to decompress files on Linux? Or how to create and extract a zip file? Then this tutorial is all you need to manage your zip files effectively.

Compression is one of the best and most popular techniques to compress files by reducing their size without losing quality. And in this tutorial, you will learn how to compress and decompress zip files on Linux, along with other tips and tricks. Ready?

Dive in and you’ll become a “zip master” by the end of this tutorial!

Prerequisites

Stop having problems with password reset calls and account lockouts in Active Directory. Get a FREE trial of Specops uReset.

Demos throughout this tutorial were done in Kali Linux 2021.2. Still, it could also work with other Linux distributions, such as Debian (such as Unbuntu or Kali Linux), CentOS or Fedora.

Installing the Zip and Unzip

packages

To compress and decompress into ZIP files with Linux, you will need a couple of packages called zip and unzip. Now let’s see how to install these packages with apt package manager.

To install the zip and unzip packages, first launch the terminal on your desktop and run the following commands depending on the Linux distributions you are working on.

Each of the following commands begins with the sudo command to handle elevated permissions.

For

Debian

The following commands are using the apt package manager to install the zip and unzip packages.

For Fedora and CentOS

The following commands use the yum package management tool to install the zip and unzip packages

.

Compress files with Zip

package

Now that you have installed all the necessary packages, it’s time to learn how to compress files. To do so, you will run the zip command, which allows you to compress files into ZIP packages, both through the command line and through the GUI. Let’s go over each method!

Compress

files via command line

On Linux, you’ll typically use the command line quite a bit, so let’s first look at how to compress files via the command line

.

Notice the command syntax below. You will see that after the zip command, outpufile represents the name of the zip file. Then followed by the name of the files and folder (file1 file2 folder) to include in the zip file.

To demonstrate file compression on the command line, first open a terminal and create a directory named ~/mydir. Next, create two files and a folder named test1, test2, and myfolder inside ~/mydir.

1. Run the mkdir command below to create the mydir directory.

2. The following commands create (touch) the test1.txt and test2.txt files. Next, create (mkdir) the myfolder directory.

3. Run the following command to enumerate (ls) the contents of the working directory (~/mydir), as shown below.

Now that you have created the files and folders, compress the ~/mydir folder by running the following command.

The following zip command recursively searches (-r) in the mydir directory for all files and subdirectories. When it’s complete, you’re compressing all those files into a ZIP file called newdir.zip.

You may only plan to compress the ~/mydir/test1.txt, ~/mydir/test2.txt, and ~/mydir/myfolder directories. To do that, run the following command.

The next zip command is recursively (-r) to find all files and folders in the working directory (mydir) named test1.txt, test2.txt and myfolder and create a ZIP file named newfiles.zip.

Adding the .zip extension for the zip file is optional, as it is added automatically after the compression is complete. In the example above, the zip command will create a ZIP file named newfiles.zip instead of just newfiles.

Delete the original

file after archiving

In the previous section, you learned how to compress files into a ZIP archive, but you will have noticed that the original files were not deleted after doing so. Add the -m option in the zip command to delete the original files, as shown below.

After creating the ZIP file, start a terminal, and then run the following command to delete the original files automatically. The following zip command compresses the files and folder named test1.txt, test2.txt, and myfolder into the ZIP file .zip newsfiles, and then deletes (-m) the original files.

After running the command, you will notice that the zip command deletes the original files.

Add

new files to an existing zip file

Maybe you forgot to include an archive when compressing files into a ZIP archive. How would you add a new archive to an existing zip file? You can delete the zip file and create another one, but there is a better way.

To add a new file to the existing ZIP file (newfiles.zip), run the following command. The following command updates (-u) the existing ZIP file package (newfiles.zip) by recursively searching (-r) for all files in the ~/mydir directory, and then adding them to the ZIP package.

Delete files from a Zip archive

You previously learned how to add files to existing compressed files. But what if you unintentionally added a file to a zip file? You’ll obviously want to remove that file from the zip file, and adding the -d option to the zip command would do the trick.

To remove an archive from the ZIP

archive, run the zip command by removing (-d) the test3 file from the ZIP file (newfiles.zip).

Compress files via

GUI By now, you have learned how to compress files using the command line. But if you’re more comfortable working with a graphical interface, you’re in luck. The GNU Network Object Model Environment (GNOME) allows you to create a zip file with a few clicks. GNOME is a desktop GUI on top of your operating system. So, let’s get started!

1. Switch to the GNOME environment, then open the file manager.

2. Search for the files to compress by navigating inside the ~/mydir directory.

3. To create the ZIP package, select all files to compress, then right-click on any of the files and choose Compress from the context menu.

Now choose the format for the compressed file in the pop-up window below. Note that there are three format options, .zip, .tar.xz, and .7z. These formats are all compression formats.

Since you are creating a zip file, select the .zip option. Then create the zip file by giving it a name (newfiles) and click Create.

The zip file is

automatically created inside the ~/mydir folder, where the files you are compressing are

located.

You will notice below that Linux creates a file called newfiles.zip from the ones you selected

. Unzip files with the

Unzip package

In the previous examples, you learned how to compress files. But maybe you need to extract or decompress the contents of the zip file. How would you do it?

Similar to file compression, you get two methods to decompress files, through the command line and the GUI. Both methods take advantage of the decompression package you previously installed to decompress ZIP files.

Decompress

files via command line

Since you will continue to run commands in a command line environment throughout this tutorial, let’s start decompressing files through the command line method

.

To unzip a ZIP file, launch your terminal and run the following command. Note that the following command will unzip the contents of the ZIP file (newdir.zip). By default, the contents are extracted to the same directory (~/mydir) as the ZIP file (newdir.zip).

If you want to unzip the contents of a ZIP file to

a directory other than the working directory, specify the -d followed by the directory to extract the contents of the ZIP file to as: unzip newdir.zip -d anotherdir.

Decompress

files via GUI If decompressing files

via the command line seems like a lot of work, try decompressing files with a few clicks with the GUI method. And similar to the command-line method, you could also choose where to extract the files.

To unzip files, open File Manager, as explained in the Compress files through the GUI section. Right-click the ZIP package you want to extract and select Extract Here, as shown below.

Once you click Extract Here, Linux will extract all files from the ZIP package to the working directory.

If you prefer to extract all files from another directory, choose the Extract To option from the context menu.

Verifying the integrity of

the Zip file

In some cases, ZIP files may get corrupted, such as downloading the ZIP file from the internet. Or when the ZIP file was transferred to a corrupted storage device. You may not know that they are damaged on the surface, so it is important to check their integrity.

To check the integrity of the zip file, run the following command. The zip command then recursively searches (-r) in the working directory for the zip file (newfiles.zip) and then checks (-T) if it is in good condition.

The following command only checks the integrity of the ZIP file, but not the files and subdirectories it contains.

As shown below, you will see an indication that the test was successful.

Test compressed files before extracting

Extracting a ZIP file with

corrupted files would only waste time and lead to errors, especially when it comes to a large zip file with multiple files. Therefore, it is a good idea to test the compressed files in the ZIP archive before extracting them.

Testing the compressed files is similar to checking the integrity of the ZIP file, but this time you are testing every file within the ZIP file and not just the ZIP file itself.

Now, run the unzip command to test (-t) the state of each file within the zip file (newdir.zip).

As you can see in the image below, the decompression package found no errors.

View the contents of the zip file without

extracting

You may prefer to see what’s inside the ZIP file instead of extracting the files. Luckily for you, the unzip command has the -l option that displays the contents of a zip file without extracting it.

To view the contents of a ZIP file, run the unzip command to enumerate (-l) the contents of the zip file (newdir.zip) without extracting it.

Notice the screenshot below showing a list of tables of the contents within the ZIP file (newdir.zip).

To

demonstrate how to script some common use cases with the zip utility, maybe you have a directory with many different files, but you only need to compress those that match a specific extension. To do that, you need a script that will repeatedly search for all files with the same extension. When you find them, add them to a zip file.

Let’s go over what the following script does and how to run it.

The following script declares a variable (files) to contain the file extension value (*.txt), then another variable (targetDir) for the destination directory (anotherDir).

The script then executes a for loop function to search for all files with the extension .txt (files) within the destination directory (anotherDir). When a .txt file is found, the zip command adds (-u) that file to the ZIP file (mynewfiles.zip). Ready to automate file compression?

1. Open a text editor and save the script with the .sh extension (for example, script.sh), which is the extension for files written with the shell programming language.

2. Make sure you can run the script by modifying your permission. To do this, run the chmod command to modify (+x) the execute permission (script.sh) of the script.

3. Now run the script by running the following command. The following command runs the script (script.sh) as long as the script is inside the current working directory.

As you may notice, there is a warning message telling you that ~/mynewfiles.zip does not exist. But don’t worry, as the script will automatically create the zip file.

Conclusion

Reduce service desk calls and update cache credentials for remote users, even outside of VPN with a self-service password reset solution. Get a demo of Specops uReset!

Throughout this tutorial, you have learned how to manage zip files (compress and decompress) and a practical example of how to automate the file compression process. As promised, you are now a “zip master”!

Now, would you resort to manual or automated processes the next time you’re compressing?