如何使用find命令清除所有文件的内容

0 scripting bash regular-expressions

是否有使用 find 命令清除所有“.log”文件内容的简单步骤 。

目前我正在使用“echo -n > filename.log”我试过 echo -n > /var/application-logs/*.log 但它不起作用..

有没有更好的方法来清除多个文件的内容?

Gor*_*son 6

要清除所有find /var/application-logs -type f -name "*.log"发现,请使用:

find /var/application-logs -type f -name "*.log" -exec tee {} \; </dev/null
Run Code Online (Sandbox Code Playgroud)

如果您的 find 版本支持它,请使用+而不是\;为所有文件使用单次运行 tee。或者,如果 shell glob 就足够了:

tee /var/application-logs/*.log </dev/null
Run Code Online (Sandbox Code Playgroud)