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

find . > files_and_folders

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

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

如何同时隐藏错误?


当前回答

简单的回答是:

找到。> files_and_folders 2>&-

2>&-关闭(-)标准错误文件描述符(2),因此所有错误消息都被静音。

如果将打印任何“权限拒绝”错误,退出代码仍将为1

健壮的GNU答案找到:

找到。type d \!\(-readable -executable \) -prune -print -o -print >文件和文件夹

传递额外的选项来查找-prune(防止降为),但仍然-print任何(-typed)不(\!)具有-可读和-可执行权限的目录,或(-o) -print任何其他文件。

-readable和-executable选项是GNU扩展,不是POSIX标准的一部分 可能仍然会对异常/损坏的文件返回'Permission denied'(例如,参见使用lxcfs < v2.0.5影响容器挂载文件系统的错误报告)

健壮的答案,适用于任何posix兼容的查找(GNU, OSX/BSD等)

{LC_ALL=C查找。3>&2 2>&1 1>&3 > files_and_folders | grep -v '权限被拒绝';[$ ?= 1];} 3>&2 2>&1

使用管道将标准错误流传递给grep,删除包含“Permission denied”字符串的所有行。

LC_ALL=C使用一个环境变量设置POSIX区域设置,3>&2 2>&1 1>&3和3>&2 2>&1重复文件描述符将标准错误流管道到grep,并且[$?= 1]使用[]来反转grep返回的错误代码,以近似find的原始行为。

也会过滤任何由于输出重定向导致的“权限拒绝”错误(例如,如果files_and_folders文件本身是不可写的)

其他回答

通过使用2>/dev/null管道stderr到/dev/null

找到。- name”……2 > / dev / null

重定向标准错误。例如,如果你在unix机器上使用bash,你可以像这样将标准错误重定向到/dev/null:

find . 2>/dev/null >files_and_folders

您可以使用grep -v逆匹配

-v, --invert-match        select non-matching lines

是这样的:

find . > files_and_folders
cat files_and_folders | grep -v "permission denied" > files_and_folders

应该有魔力

简单的回答是:

找到。> files_and_folders 2>&-

2>&-关闭(-)标准错误文件描述符(2),因此所有错误消息都被静音。

如果将打印任何“权限拒绝”错误,退出代码仍将为1

健壮的GNU答案找到:

找到。type d \!\(-readable -executable \) -prune -print -o -print >文件和文件夹

传递额外的选项来查找-prune(防止降为),但仍然-print任何(-typed)不(\!)具有-可读和-可执行权限的目录,或(-o) -print任何其他文件。

-readable和-executable选项是GNU扩展,不是POSIX标准的一部分 可能仍然会对异常/损坏的文件返回'Permission denied'(例如,参见使用lxcfs < v2.0.5影响容器挂载文件系统的错误报告)

健壮的答案,适用于任何posix兼容的查找(GNU, OSX/BSD等)

{LC_ALL=C查找。3>&2 2>&1 1>&3 > files_and_folders | grep -v '权限被拒绝';[$ ?= 1];} 3>&2 2>&1

使用管道将标准错误流传递给grep,删除包含“Permission denied”字符串的所有行。

LC_ALL=C使用一个环境变量设置POSIX区域设置,3>&2 2>&1 1>&3和3>&2 2>&1重复文件描述符将标准错误流管道到grep,并且[$?= 1]使用[]来反转grep返回的错误代码,以近似find的原始行为。

也会过滤任何由于输出重定向导致的“权限拒绝”错误(例如,如果files_and_folders文件本身是不可写的)

上面的答案对我都没用。我在网上找到的东西都集中在:隐藏错误。没有正确处理进程返回码/退出码。我在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.