Zipping files is the standard method for compressing data to save space or grouping multiple files together for easy transfer. However, standard Zip archives offer zero security. If you are emailing sensitive financial documents, backing up personal data to a public cloud server, or sharing private keys, anyone who intercepts or accesses the .zip file can simply double-click it to read your data.
To secure your data, you must encrypt the Zip file with a password. While some graphical archive managers support this, doing it directly in the Ubuntu terminal is often faster, more reliable, and essential if you are managing a headless server via SSH.
This guide explains how to install the necessary tools and create a securely encrypted, password-protected Zip file using the Ubuntu command line.
Step 1: Install the Zip Utility
Ubuntu comes pre-installed with tools for managing .tar.gz files, but the standard zip utility is not always installed by default, especially on minimal server editions.
Before you begin, ensure the zip package is installed by running:
sudo apt update
sudo apt install zip
Step 2: Create a Password-Protected Zip File
The standard command to create a Zip file is zip archive_name.zip file_to_compress.txt. To add password protection, you simply need to append the encryption flag (-e).
To encrypt a single file, run the following command (replacing the file names with your actual data):
zip -e secure_data.zip confidential_report.pdf
The terminal will instantly pause and prompt you to enter a password:
Enter password:
Verify password:
Note: When typing your password in the Ubuntu terminal, no characters or asterisks will appear on the screen. This is a standard Linux security feature. Type your password carefully and press Enter, then type it again to verify.
Once verified, the terminal will confirm the file was added, and your encrypted secure_data.zip file will be created in your current directory.
Step 3: Encrypting an Entire Directory
If you want to compress and password-protect an entire folder (and all the files inside it), you must combine the encryption flag (-e) with the recursive flag (-r).
The recursive flag tells the zip utility to look inside the target folder and grab every sub-folder and file it contains. You can combine these flags into a single command:
zip -er backup_archive.zip /path/to/my/private_folder/
Again, you will be prompted to enter and verify your password. The utility will then output a list of every file it compresses and adds to the secure archive.
How to Unzip a Password-Protected File
When you (or the person you send the file to) want to extract the data, the process is incredibly straightforward. You use the standard unzip command.
unzip secure_data.zip
Because the utility detects the encryption, it will automatically pause and prompt for the password before extracting the contents:
Archive: secure_data.zip
[secure_data.zip] confidential_report.pdf password:
Type the correct password and press Enter. The file will be extracted to your current directory.
By using the -e flag, you ensure that even if your archive falls into the wrong hands, the data inside remains completely inaccessible without the correct passphrase.