Monday, April 15, 2024
rated 0 times [  8] [ 0]  / answers: 1 / hits: 2958  / 2 Years ago, fri, october 21, 2022, 2:47:57

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?

More From » command-line


Create a hidden file inside the folder and do (.foo as an example) as "root":

touch ~/foo/.foo
chattr +i ~/foo/.foo

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...

sudo chmod +t ~/foo

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.

[#1523] Friday, October 21, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 321
Total Questions: 112
Total Answers: 106

Location: Sweden
Member since Mon, Dec 7, 2020
3 Years ago
iriousutur questions
Sun, Apr 9, 23, 02:48, 1 Year ago
Tue, May 23, 23, 00:41, 12 Months ago
Mon, May 9, 22, 00:01, 2 Years ago
Mon, Nov 1, 21, 20:59, 3 Years ago