在不存在的目录上使用`find`

gue*_*tli 8 shell find

如果/my-directory不存在,是否有一种简单的方法可以让这个默默地什么都不做?

find /my-directory -type f -mtime +14 -print0 | xargs -r0 rm
Run Code Online (Sandbox Code Playgroud)

版本:

  • 查找:GNU findutils 4.5.10
  • 重击 4.2.53

roa*_*ima 12

您可以从findwith 中丢弃错误报告2>/dev/null,或者您可以完全避免运行该命令:

test -d /my-directory && find /my-directory -type f -mtime +14 -print0 | xargs -r0 rm
Run Code Online (Sandbox Code Playgroud)

作为稍微优化和更清晰的代码,某些版本(find包括您的版本)可以rm直接为您执行:

test -d /my/directory && find /my-directory -type f -mtime +14 -delete
Run Code Online (Sandbox Code Playgroud)


Rah*_*hul 3

您可以使用,

find /my-directory -type f -mtime +14 -print0 2>/dev/null | xargs -r0 rm
Run Code Online (Sandbox Code Playgroud)

说明:

2> /dev/null意味着将 stderr 重定向到/dev/null.

  • /dev/null是空设备,它接受您想要的任何输入并将其丢弃。它可用于抑制任何输出。