I don't think it's possible. File creation and deletion in Unix are controlled by the ability to write to the directory --- basically the same flag.
Now what you can do is create two directories --- one with write permission and the other one restricted to root.
[romano:~/tmp/test] % ls -l
total 8
drwxrwxr-x 2 romano romano 4096 Oct 8 18:06 normal
drwxrwxr-x 2 root root 4096 Oct 8 18:06 onlyroot
You normally work on normal
. Suppose you have in it:
[romano:~/tmp/test/normal] % ls
one.txt three.txt two.txt
You can clearly delete files and create new ones; suppose you want to protect "one.txt
" from deleting. What you can do is creating a hard link to it in onlyroot
:
[romano:~/tmp/test/normal] 1 % sudo ln one.txt ../onlyroot
This will create another name for one.txt
in onlyroot
(using a negligible amount of space; the file is not copied). Now as a normal user you can delete one.txt
in the normal
folder, but you will have an untouchable version under the onlyroot
one.
[romano:~/tmp/test/normal] % ls
one.txt three.txt two.txt
[romano:~/tmp/test/normal] % rm one.txt
rm: remove regular empty file ‘one.txt’? y
[romano:~/tmp/test/normal] % cd ..
[romano:~/tmp/test] % cd onlyroot
[romano:~/tmp/test/onlyroot] % ls
one.txt
[romano:~/tmp/test/onlyroot] % rm one.txt
rm: remove regular empty file ‘one.txt’? y
rm: cannot remove ‘one.txt’: Permission denied