![为什么遇到 $HOME 时 'rmdir -p --ignore-fail-on-non-empty' 会失败?](https://linux22.com/image/116413/%E4%B8%BA%E4%BB%80%E4%B9%88%E9%81%87%E5%88%B0%20%24HOME%20%E6%97%B6%20'rmdir%20-p%20--ignore-fail-on-non-empty'%20%E4%BC%9A%E5%A4%B1%E8%B4%A5%EF%BC%9F.png)
请考虑以下命令:
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
为什么rmdir
到了最后一步就失败了?为什么它会尝试删除非空 $HOME
而不是停下来?
答案1
内容如下man rmdir
:
--忽略非空失败
忽略每一个失败独自因为目录非空
即由于某些原因无法删除目录其他错误,除了它是空的之外,仍然会报告。
由于普通用户通常没有足够的权限进行修改/home
(包括删除自己的主目录),因此rmdir
在这样调用时只会遇到“权限被拒绝”错误。
您可以通过尝试以下方式轻松确认这一点:
rmdir ~
这将导致:
rmdir: failed to remove '/home/youruser': Permission denied