Take the 2-minute tour ×
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 permission 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
add comment

2 Answers

up vote 9 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
add comment

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 at 7:35
    
chmod a-w . protects the current directory. –  X Tian Mar 9 at 9:39
add comment

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.