How To Zip And Unzip Files In Linux From The Terminal

How To Zip And Unzip Files In Linux From The Terminal

In this tutorial we will explain how to zip and unzip files in Linux systems through the command line using the zip and unzip command.

Above all, ZIP is the most widely used archive file format that supports lossless data compression. A ZIP file is a data container containing one or more compressed files or directories.

Compressing several files and folders into a single distributable file is the most common way done by many computer users around the world. In Windows, you can simply use Winzip, WinRAR, 7Zip, and so on to zipping and unzipping file. If you’ve installed full Linux distros with GUI, you can also find and use similar app to do similar tasks. But what about a Linux server which you can only access it via SSH client?

Installing zip and unzip

On RedHat/CentOS based system:

yum install zip unzip

On Ubuntu/Debian based system:

apt install zip unzip

How to create a ZIP file with the zip command

To create a ZIP file, you need to tell zip the name of the archive file and which files to include in it. To test this, we created two files: file-01.txt and file-02.txt. We will compress them into with the following command:

zip file-01.txt file-02.txt
  adding: file-01.txt (stored 0%)
  adding: file-02.txt (stored 0%)

As a result, each file is listed as it’s added. The name of the file and the amount of compression that was achieved on that file is shown also.

In addition, if you do not want to see the output from zip as the ZIP file is created, use the -q (quiet) option.

zip -q file-01.txt file-02.txt

To include sub-directories and everything contained in them in the ZIP file, use the -r (recursive) option and include the name of the sub-directory on the command line.

zip -r directory-01/ directory-02/ file-01.txt file-02.txt

Adding passwords to ZIP files is easy. For example, you can use the -e (encrypt) option and you’ll be prompted to enter your password and to re-enter it for verification.

zip -e file-01.txt file-02.txt
Enter password: 
Verify password: 
  adding: file-01.txt (stored 0%)
  adding: file-02.txt (stored 0%)

You can add or update one or more files of the compressed archive. Suppose we have compressed an archive then, modified a file. There is a possibility to add the update file to the compressed archive with the -u (update) option.

zip -u file-01.txt file-03.txt
updating: file-01.txt (stored 0%)
  adding: file-03.txt (stored 0%)

How to unzip a ZIP file with the zip command

Using unzip command in Linux is absolutely simple. For example, in the directory, where you have the zip file, use this command:

 extracting: file-01.txt             
 extracting: file-02.txt

Just like zip, unzip has a -q (quiet) option, so that you do not need to see the file listing as the files are extracted.

unzip -q

In addition, a good practice is to unzip to directory in Linux command line. This way, all the extracted files are stored in the directory you specified. If the directory doesn’t exist, it will create one.

unzip -d my_files/
 extracting: my_files/file-01.txt    
 extracting: my_files/file-02.txt

You can check the content of the zip file without even extracting it with the -l option.

unzip -l
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2020-08-25 23:37   file-01.txt
        0  2020-08-25 23:37   file-02.txt
---------                     -------
        0                     2 files

In conclusion, in this article, we have explained how to compress/zip and extract/unzip .zip archive files in Linux terminal. Of course, you can add your thoughts to this article via the feedback form below.

Leave a Reply

Your email address will not be published.