Zip is one of the most used archiving utility, used on Android, Windows, Linux, macOS even on servers. So, here some useful linux zip command examples to create and password protect your zip files.
Well, command line is not the most efficient way to create zip archives, a graphical archive manager is much easier to use. However purpose of this tutorial is how to use command line to do that, and it’s clearly for beginners.
Install linux zip and unzip command line tools
Many Linux distros don’t have zip and unzip command installed by default, so you need to install them first.
If you’re on Ubuntu or Debian, use apt to install them.
sudo apt-get install zip unzip
If you’re on ArchLinux or Arch based based distro, install it with
sudo pacman -S zip unzip
Zip and unzip are part of standard Fedora install, so you don’t need to install it on Fedora.
The 3 most useful option to use along with the zip command are
-roption is for recursively adding files and folders to the ZIP archive.
- When the
-9option is used, zip tries maximum compression on everything.
-eoption is to create basic password protected ZIP files.
Create linux zip file with command line
Typical syntax to create a zip file is like this,
zip output_file.zip input_file
input_file is the file we want to compress, and
output_file.zip is our zip archive output.
Now we’ll learn how to zip and single and multiple files with the linux zip command.
A practical example for a single file could be like below, assume you want to compress the file db_backup_wp2.sql with zip.
zip database_backup.zip db_backup_wp2.sql
If you want to compress multiple files with zip command at once, it’s like the example command below.
zip backup.zip border.png db_backup_wp2.sql my_resume.pdf pp.jpg
Well, the files used in the above example are pretty random, and backup.zip is our archive.
If you need to create the ZIP archive somewhere else, I mean not in the current director(pwd), just append the proper path before the archive name.
zip -9r /root/log_backup/logs_25082017.zip /var/log/
Linux zip folder with command line
Here we’ll learn how to add single or multiple folders to a zip archive, not much different from files anyway.
As example, in linux you can zip folder with command line like below.
zip -r folder1.zip folder1/
Of course you’ve to use the
-r flag with zip command to recursively compress files.
Now, if you want to add multiple directories to that zip archive, this should be like below.
zip -r folder_backup.zip folder1/ folder2/ folder3/
Ignore the trailing slashes after the folder1 or folder2 and so on, they’re added automatically due to bash shell tab auto complete.
Linux zip command to recursively compress files and folders
What if you want to zip several files and folders at the same time in a single archive at the same time?
It’s same as before, you just need to add the name of files and folder one by one. A practical example could be like below.
zip -9r my_archive.zip folder1/ folder2/ border.png pp.jpg my_resume.pdf
Linux zip with password
If you want to encrypt your zip files with password, it could be done by using
-e option with linux zip command.
zip -e -9r my_encrypted_archive.zip file1 file2 folder1 folder2
The above command will prompt you to enter a password and verify it.
However it’s not the best way to encrypt files, you should use 7zip to password protect your archives.
Linux zip exclude files and folders
You can use the
-x flag with zip command to exclude specific files or folders.
zip -9r my_archive.zip * -x *.zip
As example, the above command will compress everything to my_archive.zip in that directory while excluding all .zip files present there.
You can also exclude specific files or sub-folders inside folders, example below.
zip -9r my_archive2.zip -x folder2/widget_bg.png
Here we want to exclude the widget_bg.png file under the folder2 directory.
So. that’s all about how to create and password protect ZIP archives in Linux. You may also want to extract, create or edit 7zip archives, here’s linux 7zip command examples.
If you’ve any further question or suggestion, please ask it through the comments.