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
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
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
This will create an archive.zip in that folder containing the test.txt file.
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
For CentOS and Fedora systems, you can use Yum.
sudo yum install zip
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
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
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
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
To use a different compression method, we can pass the
zip -r -Z bzip2 archive my_directory
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
Another cool option is to encrypt the zip. This means we put a password on it.
zip -r -e archive my_directory
This will prompt you for the password twice.
# Enter password: # Verify password: