What Do I Need?
- A Dedicated or VPS Linux Server
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.
- Install Missing Zip Components
- Most common Linux operating systems, like Ubuntu 18.0+, Fedora, and even CentOS, are missing some crucial components. Let’s get them installed.
- Install missing components:
sudo yum install perl-IO-Compress
- Create a Zip File in Command Line
- 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
- 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.
- The extension .zip has now been added to your compressed folder name:
ls -l source_code.zip
- 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
- Including Directories in Zip Files
- 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
- Set the Level of Compression
- 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/
- Maximum compression level is level 9:
zip -9 -r -q source_code work/
- Adding Passwords to Zip Files
- 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 Unzip a Zip File in Command Line
- To extract files from your newly formed zip archive is another easy reversal:
- As before, the files are displayed as they’re extracted in the terminal window.
- 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
- Extracting Files to a Target Directory
- 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
- Extracting Password Protected Zip Files
- 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
- Excluding Files
- 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
- Overwriting Files
- 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.
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.
- Get answer to all of your queries about best VPS hosting by clicking here.