Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm maintaining several keys in a keyring and I regularly want to encrypt a file with every key in the keyring as a recipient. What is the best way to accomplish this if I do not want to specify each recipient individually with the --recipient or -r options?

share|improve this question

migrated from Nov 20 '12 at 11:58

This question came from our site for professional and enthusiast programmers.

How about to write a bash/batch script? What is your operating system you're working on? – arminb Nov 20 '12 at 2:40
up vote 4 down vote accepted

I'd start with something like this to auto-detect all the pubkeys in your keyring:

[rsaw ~]$ gpg --list-keys --with-colons --fast-list-mode | awk -F: '/^pub/{printf "-r %s ", $5}'
-r 8364FDE4388D0935 -r 8F5B84CD292F9E32 -r EE3656DD5B0E2954 -r F576F163ABF75D93

That should give you enough -- there are tons choices for how to proceed. One route: create a wrapper that runs gpg with that plus any args you pass it, e.g. add the following to ~/.bashrc:

g(){ gpg $(gpg --list-keys --with-colons --fast-list-mode | awk -F: '/^pub/{printf "-r %s ", $5}') "$@"; }

Example of running it:

[rsaw ~]$ g -aveo /tmp/issue.asc /etc/issue
gpg: using subkey BDE204E4 instead of primary key ABF75D93
gpg: using PGP trust model
gpg: This key belongs to us
gpg: using subkey E2E92BD7 instead of primary key 5B0E2954
gpg: This key belongs to us
gpg: using subkey 0EAE2434 instead of primary key 292F9E32
gpg: This key belongs to us
gpg: using subkey 9E0428AA instead of primary key 388D0935
gpg: This key belongs to us
gpg: reading from `/etc/issue'
gpg: writing to `/tmp/issue.asc'
gpg: RSA/AES256 encrypted for: "9E0428AA Ryan Sawhill <rsaw@devnull>"
gpg: RSA/AES256 encrypted for: "0EAE2434 Obama Mo <>"
gpg: RSA/AES256 encrypted for: "E2E92BD7 Mitt Rom <rom@devnull>"
gpg: ELG/AES256 encrypted for: "BDE204E4 Bill C <clinton@nowhere>"

This is nice if you always encrypt files and save output to files; otherwise, it might be simpler to have your script/function ONLY run the original detection-command -- this would allow more flexibility, e.g.:

gpg $(g) -aveo </tmp/myfile
share|improve this answer
Thanks, I'll give this a try. – Beau Simensen Nov 20 '12 at 3:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .