Script - Decrypt all pdf under a folder

Today I needed to decrypted a number of pdfs contained in different folders. So I wrote a bash script to do this. I think it’s worth putting it up here so someone who are in the same situation can just grab and use it.

Softwares needed

qpdf is used to decrypt pdfs.

On fedora:

sudo yum install qpdf

On ubuntu:

sudo apt-get install qpdf

The script

#!/bin/bash
pw=thepassword
dirs=$(ls -D1)

for dir in $dirs ; do
        cd $dir
        for file in *.pdf; do
                newfile=${file%.pdf}
                newfile=$newfile.decrypted.pdf
                qpdf --decrypt --password=$pw "$file" "$newfile"
                rm "$file"
                done
        cd ..
done