I want to make a file locked against deletion but still be writeable. How do I do this?
The file in question is a Truecrypt volume as a file on a NAS SMB Network share, so I don't want to accidentally delete it.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
For Unix (including OS X):
Note that this will only prevent the file from being removed (deleted), but won't do anything against accidental truncation or overwriting with garbage. If a file is writable, you can write anything to it, period.
Also, file permissions are next to impossible to translate between operating systems. If the NAS runs Linux, and you try to set permissions from within Windows, the end result may be different from what you expect.
In Linux you could create a hard link to it. Then you can write to it and "delete" it, but you'll be only removing the reference in your directory. The other hard-link will still point to the file's contents, so it won't have been deleted anyway.
In Unix world, you don't "delete" files. You just decrease the number of hard links to it. When nothing else is pointing to it, the space is considered free and can be used…
On a cow file system like btrfs you can achieve this by using subvolumes + snapshots or cp with --reflink=always this will effectively result in as many files as you want which would consume the same amount of space as one + some overhead (but without an insane number of copies or snapshots especially combined with tiny file sizes this should not be noticeable) until they are modified in which case only the parts that have been changed are stored separately and the rest is still shared. Then set the permissions on each separately (to achieve what you want just regularly make a snapshot or copy with read only permissions (and optionally mount it ro or not a all if it's a snapshot and if it's a file use chattr +i (users can't write or modify the file even if the have write permissions) on one copy if you are paranoid).
In "standard" UNIX, it seems to be impossible to protect a single file from deletion if the directory is writeable. Intuitively, one might expect that clearing the w protection from the mode bits with 'chmod' should protect against deletion, but THIS IS NOT THE CASE. Similarly, in AFS, you cannot protect single files from being deleted, because ACL entries (lacking or denying the relevant 'd' permmission) only apply to the directory as a whole.