Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

bash - 如何从“查找”中排除所有“拒绝权限”消息?(How can I exclude all “permission denied” messages from “find”?)

I need to hide all permission denied messages from:

(我需要隐藏以下所有拒绝权限的消息:)

find . > files_and_folders

I am experimenting when such message arises.

(我正在尝试何时出现此类消息。)

I need to gather all folders and files, to which it does not arise.

(我需要收集所有不会出现的文件夹和文件。)

Is it possible to direct the permission levels to the files_and_folders file?

(是否可以将权限级别files_and_foldersfiles_and_folders文件?)

How can I hide the errors at the same time?

(如何同时隐藏错误?)

  ask by Léo Léopold Hertz ?? translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use:

(采用:)

find . 2>/dev/null > files_and_folders

This hides not just the Permission denied errors, of course, but all error messages.

(当然,这不仅隐藏了Permission denied错误,而且还隐藏了所有错误消息。)

If you really want to keep other possible errors, such as too many hops on a symlink, but not the permission denied ones, then you'd probably have to take a flying guess that you don't have many files called 'permission denied' and try:

(如果您真的想保留其他可能的错误,例如符号链接上的跳数过多,但没有被权限拒绝的错误,那么您可能不得不大胆猜测您没有很多称为“权限被拒绝”的文件并尝试:)

find . 2>&1 | grep -v 'Permission denied' > files_and_folders

If you strictly want to filter just standard error, you can use the more elaborate construction:

(如果严格只过滤标准错误,则可以使用更复杂的构造:)

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 |

(find命令的I / O重定向是: 2>&1 > files_and_folders |)

.

(。)

The pipe redirects standard output to the grep command and is applied first.

(管道将标准输出重定向到grep命令,并首先应用。)

The 2>&1 sends standard error to the same place as standard output (the pipe).

(2>&1将标准错误发送到与标准输出(管道)相同的位置。)

The > files_and_folders sends standard output (but not standard error) to a file.

(> files_and_folders将标准输出(但不是标准错误)发送到文件。)

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.

(最终结果是,写入标准错误的消息将通过管道发送,并且将find的常规输出写入文件。)

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.

(grep过滤标准输出(您可以决定想要的输出有多高,可能取决于语言环境和O / S来更改拼写),最后的>&2表示尚存错误消息(写入标准输出)再次进入标准错误。)

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.

(最终重定向在终端上可以看作是可选的,但是在脚本中使用它是一个很好的主意,以便错误消息出现在标准错误上。)

There are endless variations on this theme, depending on what you want to do.

(根据您要执行的操作,此主题有很多变体。)

This will work on any variant of Unix with any Bourne shell derivative (Bash, Korn, …) and any POSIX-compliant version of find .

(它可以在带有Bourne Shell派生类(Bash,Korn等)和POSIX兼容版本的find任何Unix变体上运行。)

If you wish to adapt to the specific version of find you have on your system, there may be alternative options available.

(如果您希望适应系统上使用的find的特定版本,则可能有其他选择。)

GNU find in particular has a myriad options not available in other versions — see the currently accepted answer for one such set of options.

(特别是GNU find有许多其他版本中没有的选项-请参阅当前接受的答案中的一组这样的选项。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...