Using shell script to delete all matching certificates.

I found a list of certificates using:

security find-certificate -c "certificatename" -a -Z|grep SHA-1|awk '{ print $NF }'

Output is like this:

$ bash delete-cert.sh 

Now, I want to run the delete command on each of those SHA-1 hashes, how can I do that?

sudo security delete-certificate -Z hash

I was doing like this,

hashes=$(security find-certificate -c "certificatename" -a -Z|grep SHA-1|awk '{ print $NF }')
for hash in $hashes; do
    echo deleting $hash
    sudo security delete-certificate -Z $hash

But I like the one line solution by #chaos

1 Answer 1


Use this:

security find-certificate -c "certificatename" -a -Z | \
  sudo awk '/SHA-1/{system("security delete-certificate -Z "$NF)}'

awk is called with sudo. awk then searches for the string SHA-1 and calls the security delete-certificate command with the hash as argument. You have to provide the sudo password only once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.