76

I need to create a shell script wherein I will unzip a password protected zip file. I know the password, and need to automate the unzip process.

How can I achieve this using Unix shell scripting?

davnicwil
  • 28,487
  • 16
  • 107
  • 123
Murugesh Anand
  • 779
  • 1
  • 6
  • 5
  • 2
    If you are going to hard-code a password in a script, I assume this means that (1) you are in control of the creation of the .zip file and (2) this file will be created (on one computer) and decompressed (on another computer) many times since you want to script it. If so, have you considered not using file-level encryption, and instead using secure transmission (e.g. with SSH) over the network using private/public keys in a way that avoids having to type passwords? – Fred Feb 12 '17 at 12:33

7 Answers7

104
unzip -P your-password zipfile.zip

man unzip

-P password

use password to decrypt encrypted zipfile entries (if any). THIS IS INSECURE! Many multi-user operating systems provide ways for any user to see the current command line of any other user; even on stand-alone systems there is always the threat of over-the-shoulder peeking. Storing the plaintext password as part of a command line in an automated script is even worse. Whenever possible, use the non-echoing, interactive prompt to enter passwords. (And where security is truly important, use strong encryption such as Pretty Good Privacy instead of the relatively weak encryption provided by standard zipfile utilities.)

Community
  • 1
  • 1
Yaron
  • 10,166
  • 9
  • 45
  • 65
  • 3
    I also had the same problem, but your command didn't work. `7z x archive.zip -ppassword` command did work. – Kapil Jul 25 '20 at 15:33
  • 2
    `need PK compat. v5.1 (can do v4.6)` – ACV Jul 30 '21 at 20:33
  • I just found out that it depends on the encryption wether this works. As this does not work for AES-256 encryption. But it works for ZipCrypto (which seems to be default) – Monocito Jan 25 '22 at 09:14
  • thank you - i am surprised there is not a corresponding double-dash command such as: "--password" – edwardsmarkf Sep 08 '22 at 18:10
  • If you need to extract hundreds of password-protected files at once, [here's an answer I just added on how to parallelize and speed up the operation with `find`, `xargs`, and `unar -p`](https://stackoverflow.com/a/76910116/4561887). – Gabriel Staples Aug 16 '23 at 02:24
37

In Ubuntu, I've to install 7zip (7z) archive tool using following command:

sudo apt-get install p7zip-full

Then, you can use Ubuntu's default Archive Manager to unzip the password protected zip files

Radix
  • 2,527
  • 1
  • 19
  • 43
22

I ran into problem using unzip saying need PK compat. v5.1 (can do v4.6). Installed p7zip instead and unzipped using following command:

7z x archive.zip -ppassword
Kapil
  • 362
  • 6
  • 7
kub1x
  • 3,272
  • 37
  • 38
12

To unzip multiple password protected files, this command WILL NOT WORK

unzip -P PASSWORD *.zip

To make it work, you need to include the *.zip in quotes because whenever you use a wildcard (*), the shell itself will expand that and pass the results to the program. This is because, unlike most programs in UNIX, unzip cannot take more than one file at a time.

Read more here https://chrisjean.com/unzip-multiple-files-from-linux-command-line/.

Therefore, to unzip multiple protected files, use this

unzip -P PASSWORD  '*.zip'    
Striped
  • 2,544
  • 3
  • 25
  • 31
R. Mogire
  • 157
  • 1
  • 7
3

Please follow bellow instruction, i have solved same problem by following these steps.

  1. sudo apt install p7zip-full
  2. 7z x example.zip
  3. it will ask password for your zip file, provide password and let the process complete

Visit the following URL: For more details

2

After banging my head on the wall many times, here's my two-cent.

If the password you received contains special characters such as $ ; \ etc, your shell terminal might interpret it as part of the shell special symbol and not exactly the password string that you intended it to be.

Solution is simple, enclosed the password in a single quote!

7z x [yourzipfile] -P'your$pass;word!'

I hope this help save your time when suddenly it seems like your zip/unzip encrypted password did not work!

Bhoom Suktitipat
  • 2,147
  • 2
  • 17
  • 11
0

Unzip or extract one password-protected file

unar (un-archive) works well. So, you can use unar or unzip, like this. Note that it is a lowercase -p with unar, but it is an uppercase -P with unzip:

# (my preference) Unzip myfile.zip into dir myfile/
unar -p my_password path/to/myfile.zip

# Unzip myfile.zip into the current directory
unzip -P my_password path/to/myfile.zip
# Unzip myfile.zip into dir myfile/
unzip -P my_password -d myfile path/to/myfile.zip

I learned about the -d some/directory option with unzip here: How to extract a zip file to a specific folder?.

I prefer unar over unzip simply because unar automatically unzips the contents into a folder by the same name as the file, which is convenient and prevents me from misplacing the extracted files.

Unzip many password-protected files in parallel via find, xargs, and unar

If you're unzipping hundreds or even thousands of files at once, you can parallelize the operation with xargs to spawn one process per CPU core you have (as shown by nproc) until all files are done. This makes it much much faster! Here is how:

# Unzip many password-protected files at once, assuming they all have the same
# password

# Generally what I use:
# - Note: `-maxdepth 1` causes this to only unzip *top-level* .zip files within
#   your current directory
time find . -maxdepth 1 -type f -iname "*.zip" -print0 | xargs -0 -I{} -n 1 -P $(nproc) unar -p my_password -f {}

# Or, remove `-maxdepth 1` to unzip *all* .zip files, recursively, which are
# found within your current directory or lower.
time find . -type f -iname "*.zip" -print0 | xargs -0 -I{} -n 1 -P $(nproc) unar -p my_password -f {}

The first command above just unzipped 93 similar ~2 MB files for me in 1.084 seconds. That's 1.084 sec/93 files = 0.012 sec per file. nproc shows that I have 20 cores.

If I run it single-threaded/single-process, all in serial instead of parallel, by changing -P $(nproc) to -P 1 instead, I end up with this command (time find . -type f -iname "*.zip" -print0 | xargs -0 -I{} -n 1 -P $(nproc) unar -p my_password -f {}) which takes 9.246 sec instead. That's 9.246 sec/93 files = 0.099 sec per file, which is 0.099/0.012 = 8.25x slower.

Just for fun, I also ran a serial unzip command, time unzip -o -P my_password '*.zip' (note that -o is dangerous and means "overwrite" existing files), as a speed comparison. It took 8.196 sec on my 93 files, also much slower than the multi-process unar command above which took 1.084 seconds, and even worse, it created a massive, unusable mess of extracted files piled up on top of each other everywhere in the same directory.

So, use the parallel forms above where I pipe find to xargs and unar.

References

  1. I learned unzip -P my_password path/to/myfile.zip in the main answer here.
  2. My answer on How to unzip multiple files at once, where I first wrote about piping find to xargs and unar for parallel extracting operations.
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265