I know that deleting a file depends on the permissions I have on the enclosing directory, and not on the file itself, see e.g. here
But what about a non-empty directory? Using rm -rf NON_EMPTY_DIR
fails because the files within the directory cannot be deleted. Is keeping the directory non-empty and non-writable for others a safe way of preventing other users to delete the folder?
Example:
# Create write-all outer dir
mkdir outer
chmod 777 outer
# Create two subdirectories, one of which is empty, one not,
# and none are writable by anyone
mkdir outer/{non-,}empty
touch outer/non-empty/file
chmod 555 outer/*
ls -la outer
## drwxrwxrwx 4 user user 80 May 31 15:12 .
## [ .. does not matter here ]
## dr-xr-xr-x 2 user user 40 May 31 15:12 empty
## dr-xr-xr-x 2 user user 60 May 31 15:12 non-empty
rm -rf outer/empty
# Succeeds
rm -rf outer/non-empty
# rm: cannot remove outer/non-empty/file: Permission denied
..
entry must change. – Sparkette Dec 13 '20 at 18:32mkdir dir; chmod 0 dir; mv dir foo
gives Permission denied, butrmdir dir
still works. – ilkkachu Dec 13 '20 at 19:11