DEV Community

Cover image for Linux zip files
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Linux zip files

Today we'll be looking at how to zip up one or multiple files on Linux systems.
Zipping files can come in super handy when dealing with multiple files or backups that need to be transferred or moved.

In my case, this situation occurred because I wanted to email attach multiple log files.

The Linux ZIP command

Let's first look at the command itself.

zip options zip_name files
Enter fullscreen mode Exit fullscreen mode

Where this works as follows:

  • zip: The Linux command to zip things
  • options: We can pass in arguments for the end zip results
  • zip_name: The output name, without the .zip part
  • files: One or more files comma separated

To show you the most simple zip command for one file, we could do this.

zip archive test.txt
Enter fullscreen mode Exit fullscreen mode

This will create an archive.zip in that folder containing the test.txt file.

Installing zip on Ubuntu or Centos

To install the zip command on Ubuntu or Debian systems, you can run the following command.

sudo apt install zip
Enter fullscreen mode Exit fullscreen mode

For CentOS and Fedora systems, you can use Yum.

sudo yum install zip
Enter fullscreen mode Exit fullscreen mode

Zipping multiple files in Linux systems

To add multiple files, we can specify them with a space between like this.

zip archive test.txt test2.txt test3.txt
Enter fullscreen mode Exit fullscreen mode

However, in most cases, you want to zip a whole folder and its contents, right?

To achieve this, we can pass the -r option (Recursive).

zip -r archive my_directory
Enter fullscreen mode Exit fullscreen mode

This will take all the files inside my_directory and zip them into one archive.

As we did above, we can still add separate files in the zip by passing them along.

zip -r archive my_directory test.txt
Enter fullscreen mode Exit fullscreen mode

Linux zip options

So far, we have seen the -r option to add files in a folder recursively.

As you may know, there are multiple compressions levels for zip files. The default one is deflate. But we can also zip files using the bzip2 compression.

To use a different compression method, we can pass the -Z option.

zip -r -Z bzip2 archive my_directory
Enter fullscreen mode Exit fullscreen mode

We can also specify the level of compression we want.
This is passed with a number from 0-9.
Where 0 is no compression and 9 the highest.
The default compression is -6.

To use it:

zip -r -9 archive my_directory test.txt
Enter fullscreen mode Exit fullscreen mode

Another cool option is to encrypt the zip. This means we put a password on it.

zip -r -e archive my_directory
Enter fullscreen mode Exit fullscreen mode

This will prompt you for the password twice.

# Enter password:
# Verify password:
Enter fullscreen mode Exit fullscreen mode

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (0)