Zipping and Unzipping using the Command Line in the Linux Terminal

Compressing and Extracting Zip Archives on your Web Server

Posted by Anthony on 29th Nov 2017

If you are using a Virtual Machine and a Linux distribution as your web development environment, you have hopefully become familiar with the command line terminal by now.

It may be possible to remember a lot of basic commands, but when it comes to creation of folders and files it is best to always have a reference handy to avoid mistakes. While and FTP tool such as Filezilla is great to transfer files, it does not allow you to zip or unzip content before or after transfer. When working with large file sizes, or thousands of files in deep folder structures, compressing the content to a single archive makes for much faster transfer time and speeds.

Knowing the correct sequence of command, target, and destination along with any other flags such as compression is important to get right.

Let's start with a single file in a zipdemo folder

It contains a single text file, content.txt. This will be the file we compress and zip to a destination container.

zipping a file

Here you can see the command, the zip archive filename to be used, and subsequent files to include. zip file1 file2 file3

You can list individual files one at a time to be zipped or you can use a more convenient method to zip an entire folder...

Compressing an entire folder to a zip file:

The zipdemo folder contains a single text file, content.txt. This will be the file we compress and zip to a destination archive.

zipping folders

the -r9 syntax translates to (-r=recursive -9=max compression) including all subfolders and files and performing the maximum compression possible.

zipping folders max compression

Should you wish to include all subfolders and files but omit the parent folder...

zipping folders max compression

Zipping done, lets cover Unzipping

From your folder where you transferred your zip file you will now use the unzip command From the screenshot below follow through the process of zipping the content, the zipdemo folder is removed, and the zip archive is extracted to the original location. Additionally you can specify a destination folder with the -d flag and this is illustrated with the zipdemo2 folder.

In the case of zipdemo2, the zip could be done omitting the parent folder to create a zipdemo2 set up similarly to zipdemo.

zipping folders

And that is about all there is to know about zip, and unzip.

Don't log in using your root account - etcetera. Thanks for reading!