Zip files allow you to easily compress and bundle multiple files and folders into a single file that can be easily transferred or transported. It works on Linux as well as Windows, allowing users to easily move files across platforms. But sometimes you may need to add password protection to your zip files so that unauthorized users are unable to unzip it. In this article, we will look at how to create password protected zip file in Linux.
How to Create Password Protected Zip File in Linux
Here are the steps to add password to zip files in Linux. If you have not installed zip command on your system, here are the commands to do it.
$ sudo yum install zip [On CentOS/RHEL] $ sudo dnf install zip [On Fedora 22+] $ sudo apt install zip [On Debian/Ubuntu]
Here is the zip command to zip one or more files with a password.
zip -p password zip_filename files_to_be_zipped
In the above command, you need to use -p option after zip command along with a password. After that you need to specify the zip file name into which you want the utility to bundle all files and folders. Finally you need to list the file/folder you want to compress. Here is an example
$ sudo zip -p test_pass data.zip /home/ubuntu/data
However, the above method leaves the command trail in shell history. Also in this case, we are providing password in plain text that is clearly visible.
In such cases, you can use -e option with zip command which will display a prompt to enter password.
$ sudo zip -e data.zip /home/ubuntu/data Enter password:
Unzip password protected zip file
You can unzip password protected files just as you unzip normal files. The only difference is that you will be prompted to enter password before unzipping takes place. Here is an example.
$ sudo unzip data.zip Enter password:
In this article, we have learnt how to encrypt zip files with password, as well as how to unzip password protected files in Linux. You can use these commands in every Linux distribution, without any changes.
In fact, you can even include these commands to your shell scripts if you have automated zip file creation, or use them in cronjobs to regularly create password protected zip files.