ZIP is an archive file format, that is, it allows us to store multiple files within a single file. ZIP also supports lossless data compression. This makes the ZIP files portable and storage efficient, thus, making it one of the most popular file formats on the internet.
A ZIP archive may contain one or more compressed files or folders. A variety of compression methods are supported by the ZIP file format.
In this tutorial, we’ll be showing you how to zip (or compress) files and directories together in Linux using the
zip command. Zip is a command line utility program that deals with ZIP archives.
The zip program has to be installed on our Linux system before we can use it. Most Linux distributions do not have the
zip program installed by default.
Table of Contents
- Install the Zip Utility on Linux
- Basic Usage of the Zip Command
- How to Zip Multiple Files Together
- How To See the Contents of a Zip File
- How to Zip Multiple Directories Together
- How to Unzip a Zip File
- Change Compression Levels and Methods
- Update or Add New Files to a Zip File
- Zip All Files With a Specific File Type
- Create a Password Protected Zip File
- Split Up Zip Files
Install the Zip Utility on Linux
If you’re running Debian or Ubuntu-based Linux systems, use the
apt utility to get the zip program installed. Type in the following:
sudo apt-get install zip
On Redhat or CentOS-based distros, use the dnf utility. Type in:
sudo dnf install zip
To confirm that you have the zip command installed, try accessing the help menu of zip by entering the command
zip -h. You should get all the options for the zip command listed out.
Basic Usage of the Zip Command
The zip command has the basic functionalities of compressing, extracting, and modifying a ZIP archive file. The general syntax of the zip command is as follows:
zip [-options] [archive name] [zipfile list]
[zipfile list] indicates the field to specify the list of files to be used with the command.
zipcommand will not store file/folder permissions in the archive. You should use the
tarcommand in these cases. The tar archives will preserve file permissions on Linux.
To get basic help for the zip command use
zip -h. For more options and detailed help page, type in:
Now let’s take a look at some of the tasks you can perform with the zip command.
We’ll start with how to zip files and folders, and then move on to extra functionalities.
How to Zip Multiple Files Together
To zip multiple files together, you just have to specify the zip archive name you want to create along with the names of the files to include in the archive.
The syntax for the command is as follows:
zip archive_name.zip filename1 filename2 filename3 …
Here’s an example of how the command will look like:
zip new.zip file.txt file num.txt tutorial.firstpage.php
adding: file.txt (deflated 13%) adding: file (deflated 13%) adding: num.txt (stored 0%) adding: tutorial.firstpage.php (deflated 73%)
As you can see from the output, we created a zip file named new.zip and added four files to the zip archive.
Notice that all the files have a percentage value written beside them. This is the value which indicates the degree of compression. The more the number, the more the compression.
For example, the file tutorial.firstpage.php was the file that was the most compressed, while the num.txt file was not compressed at all but only stored in the zip file.
Keep in mind, this is lossless compression: the files were compressed while keeping the quality intact.
How To See the Contents of a Zip File
You can take a look at the contents within the zip file without extracting or unzipping. To do this, you would use the
-sf or show files flag.
Let’s take a look inside of the zip file we just created:
zip -sf new.zip
Archive contains: file.txt file num.txt tutorial.firstpage.php Total 4 entries (24524 bytes)
The output shows the four files that we included in the previous command as expected.
How to Zip Multiple Directories Together
We’ve already shown you how to zip multiple files together.
Now, let’s take a look at how you can zip folders or directories as well. To do this, we will use the recursive option with the
The syntax of the command is shown below:
zip -r directory_name1 directory_name2 directory_name3
Here’s an example of creating a zip file with multiples directories and files:
zip -r new_dir.zip dir1 dir2 num.txt
adding: dir1/ (stored 0%) adding: dir1/file (deflated 13%) adding: dir1/file.log (deflated 98%) adding: dir2/ (stored 0%) adding: dir2/file.txt (deflated 13%) adding: dir2/pr_ex_creator.txt (stored 0%) adding: dir2/tutorial.firstpage.php (deflated 73%) adding: num.txt (stored 0%)
You can see from the output that the two directories (dir1 and dir2) along with the text file (num.txt) was added to the zip archive (new_dir.zip).
-r flag is used to make sure that the zip command goes through every directory recursively and adds all the files to the archive.
Let’s take a look at how the contents inside of a zip file with directories looks with the
-sf flag you learned earlier:
zip -sf new_dir.zip
Archive contains: dir1/ dir1/file dir1/file.log dir2/ dir2/file.txt dir2/pr_ex_creator.txt dir2/tutorial.firstpage.php num.txt Total 8 entries (34764 bytes)
As you can see, the dir1 contains 2 files, and the dir2 folder contains 3 files.
How to Unzip a Zip File
Now that you’ve learned how to create a zip file with files and folders, we’ll briefly go over how to extract them with the unzip command.
The unzip command comes pre-installed in most distributions of Linux.
To unzip a zip file, you just need to specify the file name followed by the unzip command:
Let’s see this in action:
Archive: new.zip inflating: file.txt inflating: file extracting: num.txt inflating: tutorial.firstpage.php
As you can see, the unzip command output shows inflating files, exactly the opposite of what the zip command did (deflate).
Change Compression Levels and Methods
You can decide how much compression will be performed for each file when zipping them together. This is known as the compression level.
The zip command allows us to specify this level with a number between
0 means no compression and 9 means best compression. When compression
level 0 is used, the zip command will use the store function and just add it to the archive. Whereas using the
level 9 will make sure that each of the file is compressed optimally.
You can specify the compression level within the command syntax.
Let’s take a look at some examples and see how much the deflate percentage (compression) changes using different compression levels:
zip -0 zero_comp.zip tutorial.firstpage.php
adding: tutorial.firstpage.php (stored 0%)
zip -5 five_comp.zip tutorial.firstpage.php
updating: tutorial.firstpage.php (deflated 72%)
zip -9 nine_comp.zip tutorial.firstpage.php
updating: tutorial.firstpage.php (deflated 73%)
As you can see, using the higher numbers lead to a higher deflate percentage or better compression.
Now, we can compare the file size of these files and you’ll see the difference:
ls -la | grep comp.zip
-rw-rw-rw- 1 edxd edxd 6878 Oct 16 17:58 five_comp.zip -rw-rw-rw- 1 edxd edxd 6833 Oct 16 17:59 nine_comp.zip -rw-rw-rw- 1 edxd edxd 24420 Oct 16 17:58 zero_comp.zip
The file sizes are different for each of the file. Notice that the zero_comp.zip file was no at all compressed, and thus, has the biggest size.
Between level 5 and level 9, the file compressed with level 9 – nine_comp.zip has the lowest file size, although the difference is very small.
Now let’s talk about different compression methods that you can use with zip.
Zip file format supports multiple compression algorithms. By default, it uses the DEFLATE algorithm. You can change the compression algorithm with the
Let’s try to use the bzip2 compression method with the zip command (Remember, you should keep the file extension .zip, as it will be a zip file, albeit with a different compression method):
zip -Z bzip2 bzip_method.zip tutorial.firstpage.php
adding: tutorial.firstpage.php (bzipped 73%)
This is how you can use different compression algorithms, as long as they are supported by the zip file format.
Update or Add New Files to a Zip File
In this section, we’ll show you how you can update a zip archive using the zip command.
Imagine you have created a zip file but you want to add another file to the already created zip archive. It could also be that you just want to replace a file of the archive with an updated one.
For this purpose, we can use the
-u or update flag.
Let’s take a look at two examples.
We created a zip file named new.zip in the beginning of the article. We’ll update that zip file by adding a new file to the archive. At first, let’s take a look at the contents of the zip file again:
zip -sf new.zip
Archive contains: file.txt file num.txt tutorial.firstpage.php Total 4 entries (24524 bytes)
Now let’s add a file to this archive:
zip -u new.zip newfile
adding: newfile (stored 0%)
To add a new directory, you could use the
-r flag combined with the
zip -ur new.zip dir1
adding: dir1/ (stored 0%) adding: dir1/file (deflated 13%) adding: dir1/file.log (deflated 98%)
Now let’s take a look at the contents of the updated zip file:
zip -sf new.zip
Archive contains: file.txt file num.txt tutorial.firstpage.php newfile dir1/ dir1/file dir1/file.log Total 8 entries (34911 bytes)
As you can see, the file named newfile and directory dir1 was added to the archive new.zip.
Zip All Files With a Specific File Type
Let’s say you want to extract all the files that have the same extension from inside of an archive. In this case, you could use a wildcard like
*.extension format. Here, you can use your specific extension. For example, if you wanted to extract all the text files, you would use
Let’s see this in action.
First, let’s take a look at our current directory:
dir1 file file_comp.zip new.zip new_file.txt newfile nine_comp.zip num.txt tutorial.firstpage.php zero_comp.zip
There are three text files in this directory. Now, let’s create a zip archive with these text files using the wildcard:
zip text_archive.zip *.txt
adding: file.txt (deflated 13%) adding: new_file.txt (stored 0%) adding: num.txt (stored 0%)
From the output, we can see that those three text files were used to create the text_archive.zip file. This is how you can use wildcards to zip all the files with a specific extension.
Create a Password Protected Zip File
You might want to limit the access of your zip files using password protection. To do this, we can use the
-e or encrypt flag with the zip command. You will be prompted to enter the password once you run the command.
Here’s how that looks like:
zip -e encrypted_archive.zip num.txt file.txt
Enter password: Verify password: adding: num.txt (stored 0%) adding: file.txt (deflated 13%)
Or you can use the
-P flag to specify the password in one command as well:
zip -e -P 'supersecretpaswd' encrypted_archive.zip num.txt file.txt
updating: num.txt (stored 0%) updating: file.txt (deflated 13%)
supersecretpaswd is the password of the archive named encrypted_archive.zip. Furthermore, notice that the zip command is showing updating instead of adding. This is because the archive already exists, and we are adding the same files to that archive. When extracting/unzipping these encrypted files you will be asked to enter the password.
Split Up Zip Files
You also have the option to split up your zip archive into smaller files based on specified size limit. For this purpose, we’ll use the
The size limit has to be specified after the flag. The size can be specified using k (kilobytes), m (megabytes), g (gigabytes), and t (terabytes).
This will become clear once you see this example below:
zip -s 100k split_archive.zip undergrad_buet.pdf
adding: undergrad_buet.pdf (deflated 8%)
This pdf file was a little bit over 600 kilobytes in size. Thus, there should be 6 split up archives in our present working directory. Let’s run the ls command with grep to find them:
ls | grep split
split_archive.z01 split_archive.z02 split_archive.z03 split_archive.z04 split_archive.z05 split_archive.zip
As you can see, the command did split up the archive in 6 parts.
In this tutorial, we showed you how to zip files and directories together with the
zip command. In brief, you can create a zip archive by specifying the filenames to be included after the name of the archive.
If you want to know more about the zip command, you can always take a look at the manual page of the
zip command by entering
If you’re still unsure how to use the zip command, you can use the graphical user interface whenever possible. Thank you for reading the article. We hope you learned something new from this tutorial. If you have any questions, feel free to leave a comment and we’ll get back to you as soon as possible.