Much to my immediate chagrin, removing write permission from a file does not seem to protect it from rm -f:

touch foo
chmod a-w foo
rm -f foo

How can I protect a file from accidental deletion when rm will be called with the -f flag? It looks like chattr +i foo would work, but it requires root on my system (is that intended?), so I'm looking for a non-root solution.


To prevent files from being added or deleted to a directory, you can to remove the write permission for the directory.

  • Good idea, didn't think about protecting the directory. That's 1/2 of the way there, is it possible to protect a single file in a directory, but leave the others mutable?
    – Hooked
    Apr 13 '12 at 18:56
  • 2
    @Hooked No, this is how Unix works. If you want to keep the directory's write bit, but also preserve some files inside it, you can hardlink them from another directory without write permission.
    – angus
    Apr 13 '12 at 19:03
  • @angus: this should not prevent the deletion of the file. At the end it should be no different than having a backup.
    – enzotib
    Apr 13 '12 at 19:10
  • 2
    @enzotib It won't prevent unlinking the link from the mixed directory, but it does ensure the file doesn't disappear altogether if it's accidentally deleted from there..
    – Kevin
    Apr 13 '12 at 20:30

Write permission on a file allows for changing the contents of a file, write permission on a folder allows for changing the contents of a folder (ie which files are in the folder).

Most filesystems allow for certain other attributetes. You might be interrested in the 'immutable' attribute. sudo chattr +i file disables editing, moving, deleting and linking to the file.

  • On a tangent, if you add the attribute +a on a log, it will never be truncated. Use this and histappend -a to keep your shell log forever.
    – blippe
    Mar 9 '14 at 7:35
  • chmod a-w . protects the current directory.
    – X Tian
    Mar 9 '14 at 9:39

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.