代码
find / -name netcdf
Run Code Online (Sandbox Code Playgroud)
输出
find: `/root/.dbus': Permission denied
find: `/root/.gconf': Permission denied
find: `/root/.gconfd': Permission denied
find: `/root/.gnome': Permission denied
find: `/root/.gnome2': Permission denied
find: `/root/.gnome2_private': Permission denied
Run Code Online (Sandbox Code Playgroud)
Arc*_*ege 58
这些消息被发送到 stderr,并且几乎只有那些消息通常会在该输出流上看到。您可以关闭它或在命令行上重定向它。
$ find / -name netcdf 2>&-
Run Code Online (Sandbox Code Playgroud)
或者
$ find / -name netcdf 2>/dev/null
Run Code Online (Sandbox Code Playgroud)
此外,如果您要搜索根目录 (/),那么优化该过程通常会很好,这样 find 就不会消耗所有资源。
$ nice find / -name netcdf 2>&-
Run Code Online (Sandbox Code Playgroud)
这降低了进程的优先级,允许其他进程在 CPU 上有更多时间。当然,如果没有其他东西在使用 CPU,它也不会做任何事情。:) 从技术上讲,NI 值(从 中看到ps -l)增加了 PRI 值。较低的 PRI 值具有较高的优先级。比较ps -l有nice ps -l。
sda*_*aau 26
我只想指出@Gilles 在排除路径中的这个答案,这些路径使 find 抱怨权限 - Unix & Linux Stack Exchange;它基本上涉及一个构造find,使其不会下降不可读的目录,从这个意义上说,可能也快一点。
这似乎对我有用:
使用 GNU
find或任何其他find支持-readable和-executable谓词的工具:
find / -type d ! \( -readable -executable \) -prune -o -type f -name netcdf -print
Run Code Online (Sandbox Code Playgroud)
或者这个:
find / -type d ! -perm -g+r,u+r,o+r -prune -o -type f -name 'netcdf' -print
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我需要添加所有g+r,u+r,o+r(快捷方式是a+r),否则如果其中之一被遗漏,我可能仍然会收到“权限被拒绝”的点击。
这是我如何看待这一点的细分(注意-a(和)运算符 infind是隐含在两个谓词之间的):
find / # find starting from path /
-type d # match type is directory
! -perm -a+r # (and) match not permissions of `r`ead present
-prune # ignore what matched above and do not descend into it
-o # or (whatever didn't match above)
-type f # match type is file
-name 'netcdf' # (and) match name is 'netcdf'
-print # print what matched above
Run Code Online (Sandbox Code Playgroud)
请注意,如果没有最后一个-print,我会显示一些额外的项目(与 无关-name 'netcdf');的-print只有名称匹配被印刷确保(如果有的话)。
War*_*ung 12
使用locate(1)来代替:
$ locate netcdf
Run Code Online (Sandbox Code Playgroud)
它只会显示您的用户可以看到的文件。