With Linux, as with any other operating system, the size of your files and applications makes a difference to the overall stability of your system. After all, servers only have a finite amount of storage, and once that storage is used up, processes can quickly grind to a halt. This is where knowing how to compress or “Zip” a folder in Linux can become especially useful.
Whether you’re looking to send a file in a more email-friendly format, or simply hoping to save space and avoid purchasing increased storage capabilities, the ability to compress a file or directory in Linux is a command every Linux user should know.
How to Zip a File or Directory in Linux
Luckily, zipping a file in Linux is as easy as following these 3 steps:
- Depending on your Linux distribution, it may or may not already have the zip utility installed. After logging into Linux using your username and password, ensure you have the zip utility installed using these commands:
sudo apt install zip
sudo yum install zip
- Once the zip utility has been installed, you can begin using the zip command to compress files and folders. To compress a single folder or file, use the following syntax:
So, if you wanted to compress Folder1 into Archive1.zip, you would use the command:
zip -r archive1.zip Folder1
- You can also use the zip command to compress multiple files/folders into a single archive. This can be done by simply appending additional file names to the end of the command, like so:
So, if you wanted to compress File1, File2, and File3 into a single archive, the command would look like this:
zip -r archive1.zip File1 File2 File3
Additional Tips and Tricks
In addition to being able to compress one or multiple files using the zip command, you can also set the compression level you’d like the files compressed to. This value can range from 0 to 9 and runs at a 6 by default, with 0 meaning no compression and 9 representing optimal compression for each file. To manually set the compression level of a file to 9, use the following command:
zip -9 -r
Or, repeat this command replacing “-9” with another number to manually set the compression levels to that value instead. *Note: the higher the compression value, the longer and more CPU intensive the compression process will be.
On top of setting the compression level of the file using the zip command, you can also password protect a zipped file by encrypting it during the creation process. To encrypt a file, replace the “-r” in the command with “-e”, like so:
When entered correctly, this command will prompt the user to enter and confirm a password to be associated with the encrypted file.
And there you have it! You now have multiple variants of the zip command you can use as needed to keep your files compressed and less resource demanding.
- How to Check the Apache Version on a Linux Dedicated cPanel Server
- How to Change a Password in Linux
- How to Check if Your Linux Server is Under DDoS Attack
Don’t see what you’re looking for? Use the search bar at the top to search our entire Knowledge Base.
The Hivelocity Difference
With best-in-class customer service, affordable pricing, a wide-range of fully-customizable options, and a network like no other, Hivelocity is the hosting solution you’ve been waiting for.