Password Protect Files and Folders from the Command Line with zip

Published: May 4, 2018

Tags:

There are quite a few blog posts and StackOverflow threads covering this, but somehow, I had difficulty finding the answer I was looking for on Google. As such, I’m publishing this for my (and now your :smile:) reference…

Files

To password protect a file simply run the zip command and supply the -e flag (which is short for --encrypt). First pass it the “destination” (e.g. file.zip) and next pass it the source (e.g. file).

$ zip -e file.zip file

Per the zip man pages you’ll be prompted to input (and verify) the encryption password.

Unzipping the file will require the user to supply the encryption password you used to encrypt the zip file.

A screenshot showing the unzipping an encrypted zip file on a Mac

It’s also worth noting that if you enter the same file name for both the source and the destination, zip will automatically append .zip to the destination file name.

$ zip -e file file

Folders

Password protecting folders is more or less the same, but requires supplying one additional flag to the zip command…the -r flag (short for --recurse-paths).

The command looks something like this…

$ zip -er folder.zip folder

Some guides you’ll find suggest doing zip -e folder.zip folder/*, but this technique will not work if there are subfolders inside of folder/ (e.g. folder/subfolder).

Max Chadwick Hi, I'm Max!

I'm a software developer who mainly works in PHP, but also dabbles in Ruby and Go. Technical topics that interest me are monitoring, security and performance.

During the day I solve challenging technical problems at Something Digital where I mainly work with the Magento platform. I also blog about tech, work on open source and hunt for bugs.

If you'd like to get in touch with me the best way is on Twitter.