Software & AppsOperating SystemLinux

How To Unzip Password-Protected Files with a Shell Script

Ubuntu 7

In this article, we will explore how to unzip password-protected files using a shell script. This can be particularly useful when dealing with a large number of files or automating tasks. We’ll cover the basic unzip command, its parameters, and how to handle different scenarios, such as AES encrypted ZIP files.

Quick Answer

To unzip password-protected files with a shell script, you can use the unzip command with the -P parameter followed by the password. If the ZIP file is AES encrypted, you will need to use the 7za command from the p7zip-full package instead.

Understanding the Unzip Command

The unzip command is a utility that helps you list, test, and extract compressed files in a ZIP archive. The -P parameter allows you to provide the password for the encrypted ZIP file. Here’s an example of how to use it:

unzip -P password file.zip

In this command, password is the password for the ZIP file, and file.zip is the name of the ZIP file. This command will extract the contents of the file.zip archive using the provided password.

Extracting Files to a Specific Directory

If you want to extract the files to a specific directory, maintaining the same structure as the source .zip file, you can use the -d option followed by the directory path:

unzip -P password file.zip -d /path/to/directory

This command will create a directory at the specified path and extract the contents of the file.zip archive into it.

Extracting Files Without Directory Structure

To extract the files without preserving the directory structure, you can use the -j option. This will extract all files into the current directory:

unzip -j -P password file.zip

The -j option is known as the “junk paths” option, which means it will only save the names of the files, not the directories.

Handling AES Encrypted ZIP Files

The standard unzip command does not support AES encryption. If you encounter an unsupported compression method 99 error, it means that the ZIP file is AES encrypted. In such cases, you can use the 7za command from the p7zip-full package.

First, install the package using the following command:

sudo apt install p7zip-full

After the package is installed, you can use the following command to extract the AES encrypted ZIP file with a password:

7za x -p<PASSWORD> file.zip

Replace <PASSWORD> with the actual password provided by the vendor. The x option specifies that we want to extract the files with their full paths.

Conclusion

Unzipping password-protected files using a shell script can be a handy tool in your system administration toolkit. It allows for automation and efficient handling of multiple files. Remember to replace file.zip and password with the actual file name and password in all the above commands. Also, ensure you have the necessary permissions to execute the unzip or 7za command and access the files.

For further reading on the unzip and 7za commands, you can check their man pages by typing man unzip or man 7za in your terminal, or visit their official documentation here and here respectively.

Can I unzip password-protected files using a shell script?

Yes, you can unzip password-protected files using a shell script. The unzip command with the -P parameter allows you to provide the password for the encrypted ZIP file.

How do I extract files to a specific directory while unzipping?

To extract files to a specific directory while unzipping, use the -d option followed by the directory path. For example, unzip -P password file.zip -d /path/to/directory will create the specified directory and extract the contents of the ZIP file into it.

Is it possible to extract files without preserving the directory structure?

Yes, you can extract files without preserving the directory structure by using the -j option with the unzip command. For instance, unzip -j -P password file.zip will extract all files into the current directory without their respective directories.

What should I do if I encounter an “unsupported compression method 99” error?

If you encounter an "unsupported compression method 99" error, it means that the ZIP file is AES encrypted. In such cases, you can use the 7za command from the p7zip-full package to extract the AES encrypted ZIP file with a password.

How do I install the `p7zip-full` package?

You can install the p7zip-full package by running the command sudo apt install p7zip-full in the terminal. This will install the necessary package to handle AES encrypted ZIP files using the 7za command.

Where can I find more information about the `unzip` and `7za` commands?

You can find more information about the unzip and 7za commands by checking their man pages. Simply type man unzip or man 7za in your terminal to access the manual pages. You can also visit their official documentation here for unzip and here for 7za.

Leave a Comment

Your email address will not be published. Required fields are marked *