我需要隐藏所有被拒绝的消息:
find . > files_and_folders
当这样的信息出现时,我正在做实验。我需要收集所有的文件夹和文件,它没有出现。
是否可以将权限级别直接指向files_and_folders文件?
如何同时隐藏错误?
我需要隐藏所有被拒绝的消息:
find . > files_and_folders
当这样的信息出现时,我正在做实验。我需要收集所有的文件夹和文件,它没有出现。
是否可以将权限级别直接指向files_and_folders文件?
如何同时隐藏错误?
当前回答
只需使用它在系统中搜索文件。
find / name YOUR_SEARCH_TERM 2>&1 | grep YOUR_SEARCH_TERM
我们不要做不必要的工程,你只是想搜索你的文件,对吧?然后,如果文件出现在您可访问的区域,该命令将为您列出文件。
其他回答
如果你想从根目录“/”开始搜索,你可能会看到如下输出:
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'
重定向标准错误。例如,如果你在unix机器上使用bash,你可以像这样将标准错误重定向到/dev/null:
find . 2>/dev/null >files_and_folders
use
sudo find / -name file.txt
这很愚蠢(因为你提升了搜索),也不安全,但写起来要短得多。
GNU find†的优化解决方案
至少对于某些系统+文件系统组合,find不需要统计文件来获取其类型。然后,您可以在测试可读性之前检查它是否是一个目录,以加快搜索‡-我在测试中获得了约30%的改进。所以对于长时间的搜索或经常运行的搜索,可以使用其中之一:
打印所有可见的东西
$ find . -print -type d ! -readable -prune
$ find . -type d ! -readable -prune , [expression] -print
打印可见文件
$ find . -type d \( ! -readable -prune -o -true \) -o [expression] -print
打印可见目录
$ find . -type d -print ! -readable -prune
$ find . -type d \( ! -readable -prune , [expression] -print \)
只打印可读目录
$ find . -type d ! -readable -prune -o [expression] -print
笔记
†可读的和,(逗号)操作符是GNU扩展。这个表达式
$ find . [expression] , [expression]
逻辑上等价于
$ find . \( [expression] -o -true \) [expression]
‡这是因为启用此优化的查找实现将避免在所讨论的用例中声明非目录文件。
编辑:shell函数
下面是一个POSIX shell函数,我最终将这个测试前置到任何表达式。它似乎可以很好地使用隐式的-print和命令行选项:
findr () {
j=$#; done=
while [ $j -gt 0 ]; do
j=$(($j - 1))
arg="$1"; shift
test "$done" || case "$arg" in
-[A-Z]*) ;; # skip options
-*|\(|!) # find start of expression
set -- "$@" \( -type d ! -readable -prune -o -true \)
done=true
;;
esac
set -- "$@" "$arg"
done
find "$@"
}
答案中列出的其他两个替代方案导致POSIX shell中的语法错误(甚至不能源包含函数定义的文件)或ZSH中的糟糕输出…运行时间似乎是相等的。
Use:
find . 2>/dev/null > files_and_folders
当然,这不仅隐藏了Permission denied错误,还隐藏了所有错误消息。
如果你真的想保留其他可能的错误,比如符号链接上的跳数太多,而不是权限被拒绝的那些,那么你可能不得不大胆猜测你没有很多被称为“权限被拒绝”的文件,然后尝试:
find . 2>&1 | grep -v 'Permission denied' > files_and_folders
如果你只想过滤标准误差,你可以使用更精细的结构:
find . 2>&1 > files_and_folders | grep -v 'Permission denied' >&2
The I/O redirection on the find command is: 2>&1 > files_and_folders |. The pipe redirects standard output to the grep command and is applied first. The 2>&1 sends standard error to the same place as standard output (the pipe). The > files_and_folders sends standard output (but not standard error) to a file. The net result is that messages written to standard error are sent down the pipe and the regular output of find is written to the file. The grep filters the standard output (you can decide how selective you want it to be, and may have to change the spelling depending on locale and O/S) and the final >&2 means that the surviving error messages (written to standard output) go to standard error once more. The final redirection could be regarded as optional at the terminal, but would be a very good idea to use it in a script so that error messages appear on standard error.
这个主题有无穷无尽的变化,取决于你想做什么。这将在任何带有任何Bourne shell衍生物(Bash, Korn,…)的Unix变体和任何posix兼容版本的find上工作。
如果您希望适应系统上的find的特定版本,可能有可用的替代选项。特别是GNU find有许多其他版本没有的选项-请参阅当前接受的一组选项的答案。