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 loves dabbling in other languages like Go and Ruby. Technical topics that interest me are monitoring, security and performance. I'm also a stickler for good documentation and clear technical writing.

During the day I lead a team of developers and solve challenging technical problems at Rightpoint where I mainly work with the Magento platform. I've also spoken at a number of events.

In my spare time I blog about tech, work on open source and participate in bug bounty programs.

If you'd like to get in contact, you can find me on Twitter and LinkedIn.