我需要隐藏所有被拒绝的消息:

find . > files_and_folders

当这样的信息出现时,我正在做实验。我需要收集所有的文件夹和文件,它没有出现。

是否可以将权限级别直接指向files_and_folders文件?

如何同时隐藏错误?


当前回答

Use:

find . ! -readable -prune -o -print

或者更一般地说

find <paths> ! -readable -prune -o <other conditions like -name> -print

避免“拒绝许可” 并且不要压制(其他)错误消息 AND获得退出状态0(“所有文件已成功处理”)

使用:find (GNU findutils) 4.4.2。 背景:

-readable测试匹配可读文件。!当test为false时,运算符返回true。和!-readable匹配不可读目录(&文件)。 -prune操作不会下降到目录中。 ! -readable -prune可以翻译为:如果目录不可读,不要进入该目录。 可读测试会考虑-perm测试忽略的访问控制列表和其他权限构件。

更多细节请参见(1)manpage。

其他回答

Use:

find . ! -readable -prune -o -print

或者更一般地说

find <paths> ! -readable -prune -o <other conditions like -name> -print

避免“拒绝许可” 并且不要压制(其他)错误消息 AND获得退出状态0(“所有文件已成功处理”)

使用:find (GNU findutils) 4.4.2。 背景:

-readable测试匹配可读文件。!当test为false时,运算符返回true。和!-readable匹配不可读目录(&文件)。 -prune操作不会下降到目录中。 ! -readable -prune可以翻译为:如果目录不可读,不要进入该目录。 可读测试会考虑-perm测试忽略的访问控制列表和其他权限构件。

更多细节请参见(1)manpage。

您还可以使用-perm和-prune谓词来避免下降到不可读的目录(参见如何从查找程序中删除“拒绝权限”打印输出语句?- Unix和Linux Stack Exchange):

find . -type d ! -perm -g+r,u+r,o+r -prune -o -print > files_and_folders

use

sudo find / -name file.txt

这很愚蠢(因为你提升了搜索),也不安全,但写起来要短得多。

如果你想从根目录“/”开始搜索,你可能会看到如下输出:

find: /./proc/1731/fdinfo: Permission denied
find: /./proc/2032/task/2032/fd: Permission denied

这是因为许可。要解决这个问题:

可以使用sudo命令: Sudo find /。- name“toBeSearched.file”

它要求超级用户的密码,当输入密码时,你会看到你真正想要的结果。如果您没有使用sudo命令的权限,也就是说您没有超级用户的密码,请先请求系统管理员将您添加到sudoers文件中。

You can use redirect the Standard Error Output from (Generally Display/Screen) to some file and avoid seeing the error messages on the screen! redirect to a special file /dev/null : find /. -name 'toBeSearched.file' 2>/dev/null You can use redirect the Standard Error Output from (Generally Display/Screen) to Standard output (Generally Display/Screen), then pipe with grep command with -v "invert" parameter to not to see the output lines which has 'Permission denied' word pairs: find /. -name 'toBeSearched.file' 2>&1 | grep -v 'Permission denied'

我不得不使用:

find / -name expect 2>/dev/null

指定我想要查找的名称,然后告诉它将所有错误重定向到/dev/null

expect是我正在搜索的expect程序的位置。