summaryrefslogtreecommitdiff
path: root/localgpgcleankeyring
blob: de4e078325d3710743e9a966ac4ed1e98928e85e (plain)
  1. #!/bin/sh
  2. # clean_keyring.sh - clean up all the excess keys
  3. # origin: https://scruss.com/blog/2013/05/12/clean-up-your-gnupg-keyring/
  4. set -e
  5. # my keys are those with a corresponding secret key
  6. mykeys=$(gpg --list-secret-keys --with-colons | grep '^sec' | cut -d: -f5)
  7. if
  8. [ -z "$mykeys" ]
  9. then
  10. # exit if no key string
  11. echo "Can't get user's key ID"
  12. exit 1
  13. fi
  14. # all of the people who have signed my key
  15. mysigners=$(gpg --list-sigs --with-colons $mykeys | grep '^sig' | cut -d: -f5 | sort -u)
  16. # keep all of the signers, plus my key (if I haven't self-signed)
  17. keepers=$(echo $mykeys $mysigners | tr ' ' '\012' | sort -u)
  18. # the keepers list in egrep syntax: ^(key|key|…)
  19. keepers_egrep=$(echo $keepers | sed 's/^/^(/; s/$/)/; s/ /|/g;')
  20. # show all the keepers as a comment so this script's output is shell-able
  21. echo '# Keepers: ' $keepers
  22. # everyone who isn't on the keepers list is deleted
  23. deleters=$(gpg --list-keys --with-colons | grep '^pub' | cut -d: -f5 | egrep -v ${keepers_egrep})
  24. # echo the command if there are any to delete
  25. # command is interactive
  26. if
  27. [ -z "$deleters" ]
  28. then
  29. echo "# Nothing to delete!"
  30. else
  31. echo 'gpg --delete-keys' $deleters
  32. fi