I use

gpg -e file

to encrypt file, which leaves me with a file.gpg file.

  • I cat the contents and its encrypted. (cat file.gpg).
  • Then to test it, I do this: echo 'this is a test' > file.gpg

This completely overwrites all encryption in the file with that string. Why? Is there a way to make the gpg file not overwritable?


I just tested the encrypted file to see if I could overwrite it with the echo cmd, and it was overwritten. So, why encrypt a file with gpg if with a simple echo cmd I can overwrite it?? I tried the chmod u-w file.gpg, it still did not protect the file from being overwritten. Is there some other parameter in gpg that I'm not plugging in? Authentication is just my user name in gpg, no passphrase. I don't want the gpg file to be overwritten.

  • 9
    What do you intend to test with your echo? The encrypted file is just a file like any other. Truncating it and writing new data into it is allowed if the file's permission allows the current user to do so. – Kusalananda Jan 17 at 7:43
  • chmod u-w file.gpg – mosvy Jan 18 at 0:19
  • What is your intention with encrypting the file? An encrypted file is still just a file. Removing the write permissions on it would make it not over-writable (unless you're root). Removing write permissions on the directory that holds the file would make the file un-deletable (unless you're root). It's the same as with any other file. What encryption does is that it protects the contents from being read by someone that does not have the password or key. Please clarify the question with your intention and expectation. – Kusalananda Jan 20 at 19:42
  • If you mean command in the title, please write command, not cmd. (You can edit it) – Volker Siegel Jan 24 at 12:27

I don't get why you would want to echo to a file with the > operator, since this completely overwrites it, but if you want to set a file to read-only, you should take away the write permissions.

In this case it's chmod -w file.gpg.

If you want to change the encrypted file, you should append to/change the original file and then reencrypt it.

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