2

Please consider the following commands:

cd /
mkdir -p ~/a/b
touch ~/a/content
# Removes dir 'b' and stops at 'a' because it's non-empty.
# (This is the expected behavior.)
rmdir -p --ignore-fail-on-non-empty ~/a/b

rm ~/a/content
mkdir -p ~/a/b
# Fails with error: "rmdir: failed to remove directory '/home/myhome'".
rmdir -p --ignore-fail-on-non-empty ~/a/b

Why does rmdir fail at the last step? Why does it try to delete the non-empty $HOME instead of stopping?

1
  • The -p command tells it to remove the directory and it's ancestors doesn't it? This includes parent directories. It shouldn't give you the error though because you are telling it to ignore the errors. – jesse_b Aug 18 '17 at 20:15
5

Here is what man rmdir says:

--ignore-fail-on-non-empty

ignore each failure that is solely because a directory is non-empty

i.e. a failure to remove a directory due to some other error, aside from it's being empty, will still be reported.

And as regular users normally do not have enough permissions to modify /home (including removal of their own home directory), the rmdir just hits a "Permission denied" error, when invoked like that.

You can easily confirm this by attempting:

rmdir ~

which will result in:

rmdir: failed to remove '/home/youruser': Permission denied

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.