A simple batch encryption loop

Since I nailed down a way to encrypt files before transferring them over the Internet to a family member, I have been considering locking up a lot of the personal files I have on hand, in case they are lost or stolen. There are quite a few though, and the time it takes to step through the encryption process makes it inconvenient, to say the least.

gpg has a batch mode though, and splicing together the loop from this command and the encryption sequence from this command, plus a little help from gpg man page and elsewhere, I cobbled this together as a one-liner that appears to do the trick.

for nam in *.doc ; do echo MyPassword | gpg --batch -c --force-mdc --passphrase-fd 0 $nam ; done

The -c flag is the same as the --symmetric option, and the --passphrase-fd 0 flag accepts the password from whatever I type after entering the command ... except that the echo fills that in for me. The --force-mdc option is only to avoid a warning message on the receiving end when it is decrypted, so you can omit that if you like.

I suppose it's worth mentioning that I used detox to clean the file names a little bit beforehand; otherwise, spaces or oddball characters can interfere with the encryption command and cause the loop to halt. If you can improve upon this, by all means please let me know. I am well aware of my limitations as a script writer. :roll:

About these ads

2 thoughts on “A simple batch encryption loop

  1. peterix

    Try adding quotes around the filename parameter for gpg. If I’m right, it will solve the problem with spaces.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s