This is a simple Python3 script that allows you to encrypt multiple files in a path without them being zipped into a single encrypted archive. The directory structure of the specified root path to start with is preserved so /path/to/file.txt will result in /path/to/file.txt.gpg
When using GPG Tools for Mac I discovered that encrypting more than one file at a time zips them together.
I wanted to encrypt multiple files at once, but still keep them as separate files for later individual decryption.
Originally this was done in PHP but that's not exactly a normal way to run a CLI script so I rewrote it in Python3.
This script was developed on macOS so there are a few things assumed when using this script:
- You have a GPG agent running in the background with a functioning PIN Entry application.
- You are encrypting to a single GPG key that you have the secret key for in your keyring.
- You have all required file permissions needed to operate on the folder you point this script to.
- Hidden files (files that begin with a
.are ignored in both encryption and decryption operations)
This script only requires a few libraries that are found in the requirements.txt file.
gnupgprovided by thepython-gnupgpackageargparseprovided by theargparsepackageosprovided by Python3 core librariessysprovided by Python3 core librariesdatetimeprivided by Python3 core libraries
There are a few parameters used by this script:
-h(Optional) - Help/Usage- Encrypt or Decrypt via:
-e/--encrypt- Select this to encrypt files-d/--decrypt- Select this to decrypt files
-p/--path- The path to the folder and it's sub-folders you want to encrypt all files in-k/--keyEmail- The email address of the GPG key that should be able to decrypt the files. You can specify this more than once. (Required when encrypting)--delete- Optional argument to delete the original files after the operation completes.
Here are a few examples of the command options and what they would do:
python gpg_files_bulk_manage.py -p /path/to/taxes -e --delete -k me@gmail.com -k spouse@gmail.com- Encrypt all files in the folder/path/to/taxeswith the keys forme@gmail.comandspouse@gmail.comas recipients. The original files will be deleted.python gpg_files_bulk_manage.py -p /path/to/taxes -d- Decrypt all files in the folder/path/to/taxesbut preserve the encrypted versions.python gpg_files_bulk_manage.py -p /path/to/taxes -e --delete -k me@gmail.com- Encrypt all files in the folder/path/to/taxeswith only the key forme@gmail.comas a recipient. The original files will not be deleted.
A log file named bulk_gpg_{TIMESTAMP}.log in the same directory of the script will contain the results of operation