I am looking for a bash script which input is the old and the new password for changing password of a *.gpg file.

I encrypted a txt file with command gpg -c. System create a file.txt.gpg. How can I change the passphrase that I created before?

  • You encrypt a file if you do gpg -c, I assumed that was a typo and corrected it. – Anthon Apr 25 '16 at 5:01

You cannot change the passphrase, but you can decrypt and reencrypt:

gpg --decrypt file.txt.gpg | (gpg -c > tmp.gpg && mv tmp.gpg file.txt.gpg)

You shouldn't directly redirect to your encrypted file (it still needs to be decrypted) and the && make sure the mv is not executed unless the encryption succeeded.

The above will prompt you once for your decryption password and then twice for the encryption.

  • thanks, that help a lot. I wonder if you correct this code for me {basename \home\hieu\test.txt.gpg}::-4 I want to delete 4 character of the string which is test.txt.gpg Thanks alredy! – H.Dinh Apr 25 '16 at 5:59
  • @H.Dinh If you are using Bash, try x="\home\hieu\test.txt.gpg" and then echo ${x%.gpg}. That is much better than mucking with basename (which you would have to put in between $( )) – Anthon Apr 25 '16 at 7:29

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.