Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Much to my immediate chagrin, removing write protections on a file do 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

2 Answers

up vote 7 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
1  
@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

According to the man page of rm command:

-f, --force
          ignore nonexistent files, never prompt

So to prevent protect a file from accidental deletion when rm will be called with the -f flag, you can better disable this option by using alias command.

$ alias rm='rm -f'
share|improve this answer
You mean rm -i? – enzotib Apr 13 '12 at 19:12
How is aliasing supposed to disable the option? It adds it automatically to every rm, which is not a good idea. – Kevin Apr 13 '12 at 20:33
This is wrong (doesn't answer the question) and aliasing rm is not recommended anyways. – jw013 Apr 13 '12 at 22:49

Your Answer

 
discard

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.