Linux – Why Can’t You Move Another User’s Directory When You Can Move Their File?

directorylinuxmvpermissions

Following on from another user's question I've bumped into a quirk of Linux filesystem permissions that I can't easily rationalize:

sudo mkdir ~/foo ~/foo/bar
sudo touch ~/baz
mkdir ~/my_dir
chown 700 ~/my_dir
# this is fine
mv ~/baz ~/my_dir
# renaming is fine
mv ~/foo ~/bob
# Moving caused: Permission denied
mv ~/bob ~/my_dir/

For clarity foo foo/bar baz are owned by root. my_dir is owned by my own user and of course ~ is owned by my own user. I can rename and move a file owned by another user. I can rename a directory owned by another user, but I can't move a directory owned by another user.

This seems a very specific restriction and I don't understand what danger is being protected against or what underlying mechanism means that it can only work this way.

Why can other users' directories not be moved?

Best Answer

This is one of the situations documented to lead to EACCES:

oldpath is a directory and does not allow write permission (needed to update the .. entry).

You can’t write inside bob, which means you can’t update bob/.. to point to its new value, my_dir.

Moving files doesn’t involve writing to them, but moving directories does.

Related Question