查找返回“发现:.:权限被拒绝",但我未在其中搜索 [英] Find is returning "find: .: Permission denied", but I am not searching in
问题描述
我有一个正在解决的庞大的Shell脚本.我经常使用sudo
从主目录运行脚本.每当执行find
时,都会看到此错误:
I have an enormous shell script that I am troubleshooting. I often run the script from my home directory with a sudo
. Whenever a find
is executed, I see this error:
find: .: Permission denied
的确,root用户无权访问我的主目录(这是当前工作目录或上述错误中的.
),但我没有要求find
在我的主目录中执行任何操作,并且宁愿将它完全留下来.
It is true that root does not have access to my home directory (which is the current working directory or .
in the error above), but I'm not asking find
to do anything in my home directory and would rather it leave it alone entirely.
要真正把这点讲清楚,我跑了这个:
To really drive the point home I ran this:
sudo find /dev -maxdepth 1 -type f
,仍然出现相同的错误.如果-type -f
被删除,则错误将附加到预期结果的末尾.当然,如果我cd /dev
则没有错误..可能是因为root可以访问/dev
.即使我不认为这会引起问题,也使脚本看起来有问题.如何防止脚本显示这些错误?
and still get the same error. If the -type -f
is removed the error is appended to the end of the expected results. Of course, if I cd /dev
there is no error..probably since root has access to /dev
. Even though I don't think it's causing problems, it makes the script look buggy. How can I prevent the script from showing these errors?
推荐答案
我跑了:
strace find /dev -maxdepth 1
在GNU/Linux(Ubuntu)上,事实证明find
使用fchdir
syscall遍历目录树,最后执行fchdir
返回原始工作目录.这是一个片段:
on GNU/Linux (Ubuntu) and it turns out that find
uses fchdir
syscall to traverse the directory tree and finally executes fchdir
to go back to the original working directory. Here's a snippet:
open(".", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_NOFOLLOW) = 4
fchdir(4) = 0
... irrelevant ...
write(1, "/dev\n", 5) = 5
open("/dev", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 5
fcntl64(5, F_GETFD) = 0x1 (flags FD_CLOEXEC)
fchdir(5) = 0
... potentially more fchdirs ...
fchdir(4) = 0
close(4) = 0
我的提示? cd /tmp
(或其他完全可访问的目录),然后再运行find
.
My hint? cd /tmp
(or some other fully accessible directory) before running find
.
这篇关于查找返回“发现:.:权限被拒绝",但我未在其中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!