Automate Your PDF Decryption With This Handy Bash Script
Every month we receive a plethora of password-protected documents β payslips, invoices, reports. Each document, often with a distinct password that doesn’t come to mind years later. The risk of forgetting a crucial password is high. In this post, I’ll show you how to mitigate this issue by introducing a simple Bash script for decrypting your PDF documents en masse, using a reliable tool - qpdf.
First, why qpdf? There are many tools available for PDF manipulation, but qpdf stands out for its straightforward approach, active maintenance, and support for recent PDF versions.
Installing QPDF
Before diving into the script, let’s ensure we have qpdf installed. If you’re on Fedora or Ubuntu, use the following command:
On Fedora:
sudo dnf install qpdf
On Ubuntu:
sudo apt-get install qpdf
The Bash Script
Here is the bash script that prompts for a password and decrypts all PDF files in the directory:
#!/bin/bash
read -s -p "Enter Password: " PASSWORD
echo
for file in *.pdf
do
decrypted_filename=$(basename "$file" .pdf)np.pdf
qpdf --decrypt --password="$PASSWORD" "$file" "$decrypted_filename"
done
This script first prompts you for the password. It then cycles through each PDF in the directory, constructs a new filename for the decrypted version, and decrypts the file using the provided password.
To use this script:
- Create a new file in a text editor, paste in the script, and save it as
decrypt.sh
. - Make the script executable by running
chmod +x decrypt.sh
. - Run the script in the directory with your PDF files with
./decrypt.sh
.
The beauty of this script lies in its potential for automation. Imagine you’re tasked with removing passwords from dozens of PDFs in a folder - a simple run of this script will save you a considerable amount of time and effort.
This is just one example of how qpdf can be utilized. It’s a robust tool with many more features worth exploring. For more information, check the official documentation.
Remember to keep your documents safe and use this script responsibly. Happy decrypting!