How to prevent a file under user folder to be deleted?

Solution 1:

It is possible to make a file impossible to delete, even from root, on an ext2/3/4 filesystem, changing a file system attribute of the file:

$ cd
$ touch dummy
$ sudo chattr +i dummy
$ rm dummy
rm: remove write-protected regular empty file `dummy'? y
rm: cannot remove `dummy': Operation not permitted
$ sudo rm dummy
[sudo] password for enzotib: 
rm: cannot remove `dummy': Operation not permitted
$

More information on chattr and lsattr manual pages.

If later you want to delete the file you should use

sudo chattr -i dummy

before using rm.

There are two drawbacks to this:

  1. you have to be root to change attributes;
  2. you can forget about the attributes of that file or the way to change them, so that it seem you cannot delete the file anymore.