How to ZIP file with password on Linux

If you have one or more files that you need to send to a friend or store for a long time, compressing the files into a .zip archive is a good way to save on space and combine all files into a single object. If you need to keep the file contents private, for fear that they could be intercepted by the wrong party, or that someone could try to access them on your own system, then it is possible to add a password to your zip file. In this tutorial, you will learn how to zip files and add a password to your zip archive on a Linux system.

In this tutorial you will learn:

  • How to install the zip and unzip commands
  • How to zip file with password using -p or -e option
  • How to unzip a password protected zip file
How to ZIP file with password on Linux
How to ZIP file with password on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software zip
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install zip on Linux




Using zip on the command line will involve two different utilities, zip for compressing or “zipping” files and unzip for decompressing or “unzipping” files. Your Linux distro may already come with these packages installed, but not all do. You can run the respective commands on your system to install and/or update the two programs:

To install zip and unzip on Ubuntu, Debian, and Linux Mint:

$ sudo apt install zip unzip

To install zip and unzip on Fedora, CentOS, AlmaLinux, and Red Hat:

$ sudo dnf install zip unzip

To install zip and unzip on Arch Linux and Manjaro:

$ sudo pacman -S zip unzip

Zip files with password




Let’s use the zip command to create a password protected and compressed archive by using the examples below.

  1. The -p (password) option allows us to specify a password in our zip command. The following command will create a password protected zip archive of file1.txt and file2.txt, using a password of “mypassword” and the zip archive will be called my_archive.zip.
    $ zip -p mypassword my_archive.zip file1.txt file2.txt
    
    WARNING
    The problem with the -p option is that our password can easily be seen by anyone looking at the terminal over our shoulder or perusing the terminal history e.g. with the history command. This is generally not a good method to use, but could be employed safely in some scenarios, such as inside of a Bash script, assuming that the user sets the password and the value is stored as a variable.
  2. A much better method to use is -e (encrypt). This will prompt you for a password to use, and it will be hidden in the terminal so that onlookers and those browsing the terminal history will have no idea what you typed for a password. The syntax is as follows:
    $ zip -e my_archive.zip file1.txt file2.txt
    Enter password: 
    Verify password: 
      adding: file1.txt (stored 0%)
      adding: file2.txt (stored 0%)
    

    As you can see above, we are prompted in the terminal to enter and confirm our password selection, but the input keystrokes are invisible.

  3. To unzip the files, you just use the ordinary command syntax for the unzip command, but will be prompted to enter the archive’s password before the contents will be extracted.
    $ unzip my_archive.zip 
    Archive:  my_archive.zip
    [my_archive.zip] file1.txt password: 
     extracting: file1.txt               
     extracting: file2.txt
    

Closing Thoughts




In this tutorial, we saw how to compress files into a zip archive while password protecting the file on a Linux system. Using compressed and password protected archives allows us to send or store sensitive files without needing to worry about an unauthorized user getting into them. Of course, the zip archive is only as protected as your password is strong. Beware that weak passwords can always lead to your zip files being cracked.