How to compress/decompress ZIP files in Linux?

ZIP files are perhaps the most popular compressed files that exist. This mythical compression format has been with us for many years. So when using a complete operating system like Linux you need to know how to work with these files since many programs are distributed that way. So thanks to this post, you will learn how to compress/decompress ZIP files in Linux.

Much of the content that we find on the Internet are compressed and once downloaded must be decompressed. Besides sending attachments by email, it is more efficient to send them packaged and compressed, and the simple fact of making backups of developments or personal folders is greatly simplified. There are many compression formats but perhaps the best known and most popular is ZIP.

So knowing how to use and manipulate ZIP files in Linux is vital for daily work. Especially from the terminal where the resources are better used.

Install the required packages

To work with ZIP files in Linux it is necessary to install some previous packages. To create the ZIP files you have to install the zip package and to decompress them you have to install the unzip package

For Debian, Ubuntu, Linux Mint, and derivatives, it is necessary to execute this command in the terminal:

sudo apt install zip unzip

In the case of RHEL, Fedora, and CentOS:

sudo dnf install zip unzip

They are quite lightweight packages so it won’t take long.

Compress/decompress ZIP files in Linux

Once the commands are installed, we can start with them. First, we will do it with the zip command that allows us to create files in that format

The syntax is quite simple:

zip [outputfile.zip] [file] [file2]

That is, we invoke the zip command, then define a name for the output file and then specify the files to be compressed.

Remember that you can also use absolute paths.

A real example of using this command is the following:

zip example.zip index.png

Sample output:

adding: index.png (deflated 10%)

As you can see, the zip command will print all the names of the files to be compressed by default. If they are few it is quite useful, but if they are many it could be annoying. You can change this by adding the -q option

zip -q example.zip index.png

In case you want to compress a folder then you will have to use the -r option

zip -r [outputfile.zip] [folder_path]

In this way, the entire tablet folder is recursively arranged. You can also add the -q option to avoid listing all the files.

When compressing very large files it is convenient to play with the compression level that goes from 0 to 9. Where 0 is the lowest compression value and 9 is the highest. That is, when you set the value to 0 the files are packed without compression.

zip -9 -r [output.zip] [directory]

The above command takes a directory and compresses it as much as possible. Note that this will result in increased system resource usage.

To add a password, just add the -e option in the command. For example:

zip -e example.zip index.png

Sample output

Enter password:
Verify password:
adding: index.png (deflated 10%)

Decompress ZIP files

Now we need to do the reverse process which is to decompress the ZIP file.

To do this there is the unzip command that we have previously installed.

To decompress a ZIP file, just follow this syntax:

unzip [zipfile]

Similarly, you can specify the absolute path of the ZIP file

By default, the file will be unpacked at the same location as the prompt, but you can specify another directory with the -d option for example

unzip [zipfile.zip] -d [destination]

This is quite useful on servers where we can save a few steps when unpacking into a specific folder.

At the moment of unzipping all files will be shown on the output screen, with unzip you can also use the -q option to remove this.

unzip -q [zipfile.zip] -d [destination]

In case the file is password protected you will be asked to decompress it.

unzip [zipfile.zip]

Sample Output

Archive: example.zip
[example.zip] index.png password:

But if you know it in advance, you can add the -P option and specify the password.

unzip -P [password] [file.zip]

Useful to save time or in scripts.

Conclusion

In this post, you could learn how to compress/uncompress ZIP files in Linux from the terminal using the basic operations of those commands. It’s a pretty simple process to do and it’s useful given the number of ZIP files on the Internet.

So, have you used these commands? do you like them? tell us your experience in the comments.

More info: ZIP command man page unzip command man page

Scroll to Top