将非空输出定向到文件中

Jos*_*muk 1 command-line redirect tee

我只想将 find 命令的非空输出保存到文件中。如果没有输出文件则不应创建。

$ ls folder
aa
$ find . -name 'bb' | tee file
$ cat file
$ ls folder
aa file
Run Code Online (Sandbox Code Playgroud)

怎么做?

use*_*274 5

ifne包中的实用程序moreutils应该可以完成您想要的任务:

$ find . -name 'bb' | ifne tee file
Run Code Online (Sandbox Code Playgroud)

从手册页:

描述

当且仅当标准输入不为空时, ifne才会运行以下命令。

  • 很好 - 每天都学到一些东西;) (2认同)