There are many different archiving solutions for both Windows and Linux. However, most people are readily familiar with the “zip” format which does two things – archives a number of files or folders into a single file, and compresses them. Traditionally, these have been performed by two separate packages in Linux, which is why you often see archived and compressed files ending with something like “tar.gz”. The “tar” portion is for archiving. The “gz” portion is for compression. Zip files do both.

Some of us might be familiar with the old “PKZIP” utility that created zip files almost 3 decades ago. I used to use it with MSDOS. Today though, “zip” is the common name given by laymen for archived and compressed files. Software downloads are very often distributed through this format as well. The layperson knows nothing about “tar” and “gz”. So if you’re offering a download to the general public on your Linux server, it’s useful to know how to zip and unzip files.

Verifying whether or not the Packages are Installed

The relevant packages for creating and extracting zip files are just “zip” and “unzip”. Depending on your installation, you can either use “yum”, or “apt-get” to check and see if they’re installed or not as shown here:

If not, just do a search and install them as appropriate for your Linux distribution.

Creating a Test Directory Structure and Files

For this demo, I’ll first create five files and a directory:

Furthermore inside the “foldertobezipped” directory, I create another subdirectory:

The reason for this extra step will become apparent in a moment. So now we have 5 regular files, a folder, and a subdirectory inside that.

Zipping the Contents of a Directory

Now we’re ready to zip them all up! We to the folder containing the files we want, and enter the following command:

zip archived *

This creates a zip file called “archived.zip”. Note that there’s no need to enter the extension in the command. With the directory structure we just created, here is the output of that command:

How to Zip a Folder in Linux

You can see that it’s stored our files and the folder. But has it stored the subdirectory as well? Let’s find out!

Looking Inside a Zip Archive on Linux

Often you just want to see what’s inside a zip file before unzipping it. Some zips can be huge and contain hundreds or even thousands of files. Looking inside lets us save resources and verify that everything is fine before committing ourselves. For the archive that we just created called “archived.zip”, we can look inside it using the following:

unzip -l archived.zip

This shows us the following:

Here we can see the 5 files and folder. But the subdirectory is missing! This is because the “zip” command by default only archives the contents of the folder 1 level deep. It doesn’t go recursively and also include the subdirectories. To make this happen, we need to run the zip command with the “-r” option as follows:

zip -r archived *

This time when we create the archive with the “-r” parameter, looking inside the file gives us this:

This time, the subdirectory has been zipped up as well.

Unzipping Files into a Directory

To unzip an archive contents into the current directory, use:

unzip archived.zip

To unzip the file into a specified directory, use the “-d” parameter like this:

unzip archived.zip -d ./unzippedlocation

This gives us the following output:

All the zipped files have been extracted to the folder “unzippedlocation”. You can also extract specific files from the archive by specifying the file name like this:

unzip archived.zip file1

This way, you can extract and view individual files like “Readme.txt” etc before unzipping the whole thing. There are many more options and advanced techniques for zip file manipulation. But these basics should help you get started and give you a base for more advanced operations.

Is your website slow?

Enter its URL below to find out now:

About the Author

Bhagwad Park

Comments