bash Find 正在返回“find: .: Permission denied”,但我没有在搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5791651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:51:18  来源:igfitidea点击:

Find is returning "find: .: Permission denied", but I am not searching in

linuxbashsudo

提问by User1

I have an enormous shell script that I am troubleshooting. I often run the script from my home directory with a sudo. Whenever a findis executed, I see this error:

我有一个巨大的 shell 脚本,我正在对其进行故障排除。我经常从我的主目录运行脚本sudo。每当find执行 a 时,我都会看到此错误:

find: .: Permission denied

find: .: Permission denied

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 findto do anything in my home directory and would rather it leave it alone entirely.

确实,root 无权访问我的主目录(即当前工作目录或.上述错误中的目录),但我并没有要求find在我的主目录中执行任何操作,而是希望它完全不理会它。

To really drive the point home I ran this:

为了真正把重点带回家,我运行了这个:

sudo find /dev -maxdepth 1 -type f

sudo find /dev -maxdepth 1 -type f

and still get the same error. If the -type -fis removed the error is appended to the end of the expected results. Of course, if I cd /devthere 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?

并且仍然得到同样的错误。如果-type -f被删除,则错误会附加到预期结果的末尾。当然,如果我cd /dev没有错误..可能是因为 root 可以访问/dev. 尽管我不认为它会导致问题,但它使脚本看起来有问题。如何防止脚本显示这些错误?

回答by Bolo

I ran:

我跑了:

strace find /dev -maxdepth 1

on GNU/Linux (Ubuntu) and it turns out that finduses fchdirsyscall to traverse the directory tree and finally executes fchdirto go back to the original working directory. Here's a snippet:

在 GNU/Linux (Ubuntu) 上,事实证明它find使用fchdirsyscall 遍历目录树,最后执行fchdir以返回原始工作目录。这是一个片段:

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

My hint? cd /tmp(or some other fully accessible directory) before running find.

我的提示? cd /tmp(或其他一些完全可访问的目录)在运行之前find

回答by l0b0

Add a cd /to the start of the script. Unless you sourceit, the script is run in a sub-shell, so your own $PWDwill not be changed. If you dosource it, either store $PWDat the start and cd -- "$PWD"at the end, or simply cd -if you don't do any other cds in the script.

将 a 添加cd /到脚本的开头。除非你source它,脚本是在一个子 shell 中运行的,所以你自己的$PWD不会被改变。如果您确实获取了它,请$PWD在开始和cd -- "$PWD"结束时存储,或者只是在脚本中cd -不执行任何其他操作cd

回答by Andrew

Try redirecting stderr. For example, you could throw it away:

尝试重定向标准错误。例如,你可以把它扔掉:

find /dev 2>/dev/null