The zip command is used to compress files for ease and portability, on Linux/Unix operating systems. There are various other ways to compress your files, but the zip command is among the most popular.

In this tutorial we’ll explore the various ways you can zip files by using the zip command, and the unzip command to extract your compressed files. We’ll also see how to unzip archives zipped through different methods .zip, .gz, .tar, .bz, .7z, .xz , and .rar.

How to Zip Files in Linux

The following are examples of typical uses of the zip command.

If you’re on a fresh install of your operating system, if you don’t have it installed, you can install both the zip and unzip tools by running:

Ubuntu/Debian

$ sudo apt install zip unzip

CentOS/Fedora

$ sudo yup install zip unzip

Get a High RAM VPS at Entry-level Pricing

Starting with 2GB RAM at $6.99/month

Take your pick from our KVM VPS that offer a generous amount of RAM at an affordable price. We've got 5 plans for you to choose from, our smallest featuring 2GB RAM at $6.99/mo.

[Main Features]
  - SSD Drives
  - KVM Virtualization
  - 1Gbps Shared Uplink
  - Location: Chicago, IL

Pick one of our KVM plans

1 – Zip Files in Directory

This command creates a .zip archive of all the files in a directory. It does not create an archive recursively, however.

$ zip some_folder.zip some_folder/*

Example Output

adding: some_folder/a_file.conf (stored 0%)
adding: some_folder/Anaconda3-2018.12-Linux-x86_64.sh (deflated 0%)
adding: some_folder/GettyImages-845976972-5b1977813de42300372a76c8.jpg (deflated 6%)
adding: some_folder/google-chrome-stable_current_amd64.deb (deflated 0%)
adding: some_folder/mr_file (deflated 0%)
adding: some_folder/some_script.py (deflated 0%)
adding: some_folder/this_is_a_file.txt (deflated 0%)
adding: some_folder/this_is_another_file.txt (deflated 0%)

2 – Zip Files by Extension using Wildcard

Using wildcards, we can zip files that files that have a specific extension. We’ll archive only our .txt files:

$ zip some_folder_txt.zip some_folder_txt/*.txt

Example Output

adding: some_folder/this_is_a_file.txt (deflated 0%)
adding: some_folder/this_is_another_file.txt (stored 0%)

3 – Zip Files Recursively

Using the zip command with the -r flag, we can create an archive recursively, thereby compressing subdirectories as well.

$ zip -r some_folder.zip some_folder

4 – Password Protect Zip Files

If you need to protect your archives, you can password protect them using the -P flag:

$ zip -P some_folder.zip some_folder/*

5 – Adjust Zip Compression Levels

The zip command allows you to adjust the compression level from 0 to 9.

-6 is the default compression level

-0 is the lowest compression level

-9 is the highest compression level

$ zip -9 some_folder.zip some_folder/*
$ zip -0 some_folder.zip some_folder/*

How to Unzip Files in Linux

1 – List Contents of a Zip File

We can use the -l flag to list the contents of a zip file, without extracting it.

unzip -l some_folder.zip

Example Output

  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2019-01-15 14:00   some_folder/a_file.conf
112311072  2019-01-15 14:05   some_folder/Anaconda3-2018.12-Linux-x86_64.sh
     4588  2018-06-07 13:29   some_folder/GettyImages-845976972-5b1977813de42300372a76c8.jpg
 56611204  2018-12-11 20:39   some_folder/google-chrome-stable_current_amd64.deb
 20971520  2019-01-15 14:00   some_folder/mr_file
  5242880  2019-01-15 14:01   some_folder/some_script.py
 10485760  2019-01-15 14:01   some_folder/this_is_a_file.txt
---------                     -------
205627024                     7 files

2 – Extract a Zip File

To unzip a .zip archive, we can simply use the unzip command:

$ unzip some_folder.zip

3 – Test the Integrity of a Zip File

You may want to make sure your archive isn’t corrupted. For this, we can use the -t flag with the unzip command.

This option extracts each specified file in memory and compares the CRC (cyclic redundancy check, an enhanced checksum) of the expanded file with the original’s stored CRC value.

$ unzip -t some_folder.zip

Example Output

Archive:  some_folder.zip
    testing: some_folder/a_file.conf   OK
    testing: some_folder/Anaconda3-2018.12-Linux-x86_64.sh   OK
    testing: some_folder/GettyImages-845976972-5b1977813de42300372a76c8.jpg   OK
    testing: some_folder/google-chrome-stable_current_amd64.deb   OK
    testing: some_folder/mr_file      OK
    testing: some_folder/some_script.py   OK
    testing: some_folder/this_is_a_file.txt   OK
No errors detected in compressed data of some_folder.zip.

Extract Different Compression Formats

There are different compression methods and you’ll very often encounter different formats. You can use the following methods to extract the various formats.

How to Extract .gz File

.gz files are archives compressed using the Gzip utility. It’s generally considered better than Zip, especially when having to compress a huge number of file. To extract a .gz archive use the gunzip command:

$ gunzip some_folder.gz

How to Extract .tar File

.tar is referred to as the tarball but it’s name is actually derived from (t)ape (ar)chive. This is another popular compression method. To extract a .tar archive, use the following command:

$ tar -xvf some_folder.tar

How to Extract .tar.gz File

The .tar.gz is a format combining TAR and GZIP, thus providing more compression. You can extract it using the following command, using the -Z switch:

$ tar -xzf some_folder.tar.gz

How to Extract .tar.xz File

The tar.xz format does not offer archiving capabilities. It only preserves the original data into one file. To extract .tar.gz we use the -J switch:

$ tar -xJf some_folder.tar.xz

How to Extract tar.bz2 File

tar.bz2 is a combination of the TAR and bzip2 formats. To extract it, we use the -j switch:

$ tar -xjf some_folder.tar.bz2

How to Extract .7z File

7z represents the 7zip format. It’s typically not used on Linux, and to use it you’ll have to install 7-zip File Archive on your machine. With it installed, to extract a 7z archive, run the following command:

$ 7z x some_folder.7z

How to Extract .rar File

.rar files are one of the most popular archive formats for Windows, created using WinRAR. It’s not very often used on Linux, however if you need to extract it, you can use the unrar utility or 7-zip File Archiver, both of which you’ll have to install on your OS.

To extract .rar archives on Linux, use the following comman

$ unrar x some_folder.rar

OR

$ 7z x some_folder.rar

Conclusion

Well done. You’ve learned how to create ZIP archives through various methods, and to extract different archive formats. If you’ve found any issues with this tutorial, then please feel free to leave a comment or contact us, and we’ll get back to you as soon as possible.

Get a High RAM VPS at Entry-level Pricing

Starting with 2GB RAM at $6.99/month

Take your pick from our KVM VPS that offer a generous amount of RAM at an affordable price. We've got 5 plans for you to choose from, our smallest featuring 2GB RAM at $6.99/mo.

[Main Features]
  - SSD Drives
  - KVM Virtualization
  - 1Gbps Shared Uplink
  - Location: Chicago, IL

Pick one of our KVM plans


Vlad

Tech Support

LIMITED TIME SPECIAL 💀

Enter your email below to get 20% OFF on any of our Linux VPS plans and receive weekly deals on our services!