Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 10 down vote accepted

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

share|improve this answer
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
@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
@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.

share|improve this answer
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 posting your answer, you agree to the privacy policy and terms of service.

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