You just need to make sure that your regular user doesn't have write access to the parent directory of the ones you don't want to be able to delete and that your user does have write access to the directories themselves. Like this:
$ tree -pu
.
└── [drwxr-xr-x root ] testDir
└── [drwxrwxrwx user ] subDir
As you can see above, the testDir
directory belongs to root
and only root
has write access to it. The testDir/subDir
directory belongs to my user, and everyone has full access to it. This means that I can freely create and delete files in there:
$ touch testDir/subDir/file
$ echo "hello World" > testDir/subDir/file2
$ ls -l testDir/subDir/
total 4
-rw-r--r-- 1 terdon terdon 0 May 1 17:56 file
-rw-r--r-- 1 terdon terdon 12 May 1 17:57 file2
$ rm testDir/subDir/file
$ ls -l testDir/subDir/
total 4
-rw-r--r-- 1 terdon terdon 12 May 1 17:57 file2
I cannot, however, delete the directory itself:
$ rm -r testDir/subDir
rm: cannot remove 'testDir/subDir': Permission denied
Not even if it is empty:
$ rm testDir/subDir/*
$ rmdir testDir/subDir/
rmdir: failed to remove 'testDir/subDir/': Permission denied
man
page yet? Or look anywhere like here – bu5hman May 01 '20 at 17:50