How to Zip and Unzip Files and Folders using Command Line

What Do I Need?

  • A Dedicated or VPS Linux Server
  • Ubuntu

What is Zip?

Zip is a near-ubiquitous universal file archive commonly used on Windows, macOS, and Linux. You can create a zip archive, zip compressed volume, unzip files, and more with some fairly common Linux commands.

  1. Install Missing Zip Components
  1. Most common Linux operating systems, like Ubuntu 18.0+, Fedora, and even CentOS, are missing some crucial components. Let’s get them installed.
  2. Install missing components:
sudo yum install perl-IO-Compress

  1. Create a Zip File in Command Line
  1. Open Terminal and navigate to a folder that you want to zip up. In this example, we’re going to be using a folder of source code.
zip source_code *.c *.h

How to Zip and Unzip Files and Folders using Command Line

  1. Each file will be listed as it’s added to the archive. The name of the file and the amount of compression that was attained is shown beside each file.

How to Zip and Unzip Files and Folders using Command Line

  1. The extension .zip has now been added to your compressed folder name:
ls -l source_code.zip

How to Zip and Unzip Files and Folders using Command Line

  1. If you don’t want to see the output from the .zip as the zip file is created, use the -q, or quiet, flag option:
zip -q source_code *.c *.h

How to Zip and Unzip Files and Folders using Command Line

  1. Including Directories in Zip Files
  1. In order to include sub-directories in the zip archive, use the -r, or recursive, flag option and include the name of the sub-directory on the command line:
zip -r -q source_code archive/ *.c *h

How to Zip and Unzip Files and Folders using Command Line

  1. Set the Level of Compression
  1. An often missed but important step is to ensure compression is applied to the files. The compression range is 0 to 9, with 0 being no compression at all. The higher the compression, the longer it takes to create the zip file. The default compression is level 6, which is good enough for most use cases:
zip -0 -r -q source_code work/

How to Zip and Unzip Files and Folders using Command Line

  1. Maximum compression level is level 9:
zip -9 -r -q source_code work/

  1. Adding Passwords to Zip Files
  1. Adding passwords to zip files is relatively easy. Use the -e, or encrypt, flag option and you’ll be prompted to enter your password and re-enter it for verification:
zip -e -r -q source_code work/

How to Zip and Unzip Files and Folders using Command Line

  1. How to Unzip a Zip File in Command Line
  1. To extract files from your newly formed zip archive is another easy reversal:
unzip source_code.zip

How to Zip and Unzip Files and Folders using Command Line

  1. As before, the files are displayed as they’re extracted in the terminal window.

How to Zip and Unzip Files and Folders using Command Line

  1. Zip files don’t carry the details of file ownership or permissions. All of the files that are extracted have the owner set to the user who’s extracting them.
unzip -q source_code.zip

  1. Extracting Files to a Target Directory
  1. Sometimes it’s necessary to extract your files in a specific directory. Use the -d, or directory, flag option, and provide the path to the target directory:
unzip -q source_code.zip -d ./development

  1. Extracting Password Protected Zip Files
  1. If a zip file is password-protected, unzip will ask for a password to be entered. Naturally, if you don’t provide the correct password, unzip won’t extract the files.
unzip -q source_code.zip

  1. Excluding Files
  1. If you don’t want to extract a particular file or group of files, use the -x, or exclude, flag option:
unzip -q source_code.zip -x *.h

  1. Overwriting Files
  1. What happens if you’ve extracted an archive but have accidentally deleted some files? A quick fix is to extract the files once again. But if you try to extract the zip in the same directory as before, unzip will prompt you for a decision concerning overwriting the files. You can use any of the following flag options for additional command line behaviors:

y: Yes, overwrite this file

n: No, don’t overwrite this file

A: All, overwrite all of the files

N: None, overwrite none of the files

r: Rename, extract this file but give it a new name and you’ll be prompted for that new name.

How to Zip and Unzip Files and Folders using Command Line

Conclusion

There are a lot of options and flags to be explored when using zip and unzip. Be sure to check back regularly for more guides exploring this subject.

How To Set up a VSFTPD Server on a CentOS 7 VPS or Dedicated Server

Brief Description FTP is usually insecure exposing clear-text passwords, userna
2 min read
Avi Ilinsky
Avi Ilinsky
Hosting Expert

How To Set up a VSFTPD Server on an Ubuntu 16.04 VPS or Dedicated Server

Brief Description FTP data is usually insecure since information (usernames, pa
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How to use phpMyAdmin to develop a website (without MySQL experience)

Brief description A web developer who is not well versed into coding websites f
2 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Install MySQL on a Windows Web Server Running Apache

This tutorial will show you how to install the MySQL database on a Windows serve
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.