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

find . > files_and_folders

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

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

如何同时隐藏错误?


当前回答

只需使用它在系统中搜索文件。

find / name YOUR_SEARCH_TERM 2>&1 | grep YOUR_SEARCH_TERM

我们不要做不必要的工程,你只是想搜索你的文件,对吧?然后,如果文件出现在您可访问的区域,该命令将为您列出文件。

其他回答

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。

上面的答案对我都没用。我在网上找到的东西都集中在:隐藏错误。没有正确处理进程返回码/退出码。我在bash脚本中使用命令find来定位一些目录,然后检查它们的内容。我计算命令查找成功使用退出代码:值为零工作,否则失败。

Michael Brux提供的答案有时是有效的。但我有一个失败的场景!我发现了问题,自己解决了。我需要修剪文件时:

it is a directory AND has no read access AND/OR has no execute access

这里的关键问题是:AND/OR。我读到的一个很好的建议条件序列是:

-type d ! -readable ! -executable -prune

这并不总是有效的。这意味着当匹配为:

it is directory AND no read access AND no execute access

当授予读访问权限而不授予执行访问权限时,此表达式序列将失败。

经过一些测试后,我意识到这一点,并将我的shell脚本解决方案更改为:

find /home*/ -maxdepth 5 -follow \ \(type d -a !\(-readable -a -executable \) \) -删除\ - o \ \(-type d -a -readable -a -executable -name "${m_find_name}" \) -打印

这里的关键是在组合表达式中放置“not true”:

has read access AND has execute access

否则它没有完全访问权限,这意味着:修剪它。事实证明,在一个之前建议的解决方案失败的场景中,这对我来说是有效的。

我在评论部分提供了以下技术细节。如果细节过多,我很抱歉。

¿Why using command nice? I got the idea here. Initially I thought it would be nice to reduce process priority when looking an entire filesystem. I realized it makes no sense to me, as my script is limited to few directories. I reduced -maxdepth to 3. ¿Why search within /home*/? This it not relevant for this thread. I install all applications by hand via source code compile with non privileged users (not root). They are installed within "/home". I can have multiple binaries and versions living together. I need to locate all directories, inspect and backup in a master-slave fashion. I can have more than one "/home" (several disks running within a dedicated server). ¿Why using -follow? Users might create symbolic links to directories. It's usefulness depends, I need to keep record of the absolute paths found.

为了在整个文件系统中搜索某些文件,例如主机,除了会导致各种错误的/proc树,我使用以下方法:

# find / -path /proc ! -prune -o -name hosts -type f
/etc/hosts

注意:因为-prune始终为真,所以必须对其求反,以避免在输出中看到行/proc。我试过了!-readable方法,并发现它返回/proc下当前用户可以读取的所有内容。所以"OR"条件并不是你所期望的。

我从find手册页给出的示例开始,参见-prune选项。

为了避免出现“权限被拒绝”警告,请告诉find通过从搜索中删除不可读的文件来忽略它们。将表达式作为OR添加到查找中,例如

find / \! -readable -prune -o -name '*.jbd' -ls

这主要是说(匹配一个不可读的文件并将其从列表中删除)或(匹配一个像*这样的名称。JBD和显示它[与ls])。(请记住,默认情况下表达式是AND'd,除非使用-or。)您需要在第二个表达式中使用-ls,否则find可能会添加一个默认操作来显示任何一个匹配,这也将显示所有不可读的文件。

但是如果你在你的系统中寻找真实的文件,通常没有理由去找/dev,因为那里有很多很多的文件,所以你应该添加一个表达式来排除这个目录,比如:

find / -mount \! -readable -prune  -o  -path /dev -prune  -o  -name '*.jbd' -ls

所以(匹配不可读的文件并从列表中修剪)或(匹配路径/dev并从列表中修剪)或(匹配像*。JBD并显示它)。

-=适用于苹果操作系统=-

使用别名创建一个新命令:只需添加~/。bash_profile线:

alias search='find / -name $file 2>/dev/null'

在新的终端窗口中,你可以调用它:

$ file=<filename or mask>; search

例如: $ file =等;搜索