I have some folder ~/foo
which I'd like to protect from accidental deletion, that is I'd like to protect it from deleting with rm -r ~/foo
or rmdir ~/foo
.
I realized you can do that using chmod a-w ~/foo
or chattr -i ~/foo
but this has the drawback that you cannot change anything inside that folder. I however would still like to be able to create/move/delete files and folders inside ~/foo
.
Is it somehow possible to do so?
Create a hidden file inside the folder and do (
.foo
as an example) as "root":You can now delete all files except
.foo
in this directory, and the directory can not be deleted by another user. You can still move the directory though and you can stop that with the sticky bit...And that should cover your problem. BUT I do agree: making a backup is always the better option. A somewhat more difficult option: put a directory watcher on
~/foo
and create a timestamped backup of every file touched and before it is altered in a directory users can not reach would be a safer method.I would suggest adding the following "safetynets" to your .bashrc (or config file for another shell) to prevent the most obvious mistakes.
Ask for confirmation before removing 3 or more files, or any directory recursively. Also prevents removal of
/
recursively: (the-I
parameter is less intrusive than-i
which prompts on every file, but still prevents most mistakes by a single confirmation prompt)Prevent changing permissions and ownership on
/
recursively: (not your question, but still a nice safety feature to include)