6

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.

terdon
  • 242,166
Hooked
  • 1,373
  • 3
  • 17
  • 24

2 Answers2

11

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

Shawn J. Goff
  • 46,081
  • 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
0

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.

blippe
  • 1