Linux: Create files and direcotires but not delete them

Solution 1:

You should be able to achieve something by setting extended attributes with chattr.

Use chattr +a on your directories. This will allow them to be added to but will stop them from having files removed.

Use chattr +i to make your files immutable - they cannot be changed once this is set.

A file with the ‘a’ attribute set can only be open in append mode for writing. Only the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability can set or clear this attribute.

A file with the ‘i’ attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file. Only the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability can set or clear this attribute.

Solution 2:

This is not possible with standard POSIX unix file permissions.

The best you can accomplish is a directory where users will be able to delete any files they own, but not those of other users.

chmod 1775 /directory

UPDATE: You can have more fine gained control with SELinux or Apparmor but from that approach you have to give permissions to processes on files.

UPDATE: You have the option of using the system append-only flag for separate files. Not on all filesystems: chattr +a filename