删除shell脚本中以特定字符串开头的目录

Sha*_*uan 1 unix linux bash ubuntu redhat

当我尝试删除以 tmp 开头的所有目录时,我使用了以下命令:

 find -type d -name tmp* -exec rmdir {} \;
Run Code Online (Sandbox Code Playgroud)

它确实成功了,但该命令正在退出并显示错误代码:

find: `./tmp09098': No such file or directory
Run Code Online (Sandbox Code Playgroud)

是什么导致我的构建失败。

谁能告诉我如何删除这些文件夹而不出现错误?

在尝试了@anubhava的建议并引用了“temp*”之后,

find -type d -name 'tmp*' -exec rmdir {} \;
Run Code Online (Sandbox Code Playgroud)

我仍然遇到同样的错误:

find: `./tmp0909565g': No such file or directory

find: `./tmp09095': No such file or directory
Run Code Online (Sandbox Code Playgroud)

运行时:

find -type d -name 'tmp*' -exec ls -ld '{}' \;
Run Code Online (Sandbox Code Playgroud)

这是结果:

drwxr-xr-x 2 root root 4096 Jun 16 10:08 ./tmp0909565g
drwxr-xr-x 2 root root 4096 Jun 16 10:07 ./tmp09095
drwxr-xr-x 2 root root 4096 Jun 16 10:08 ./tmp09094544656
Run Code Online (Sandbox Code Playgroud)

anu*_*ava 5

您应该引用该模式,否则它将被命令行上的 shell 扩展:

find . -type d -name 'tmp*' -mindepth 1 -exec rm -rf '{}' \; -prune
Run Code Online (Sandbox Code Playgroud)

-prune导致find不下降到当前文件/目录。