作为部署脚本的一部分,我想从我的临时目录中转储一些缓存的内容。我使用如下命令:
rm /tmp/our_cache/*
Run Code Online (Sandbox Code Playgroud)
但是,如果/tmp/our_cache
为空(在快速连续向我们的测试服务器推送许多更改时很常见),则会打印以下错误消息:
rm: cannot remove `/tmp/our_cache/*': No such file or directory
Run Code Online (Sandbox Code Playgroud)
这没什么大不了的,但它有点难看,我想降低这个脚本输出中的噪声信号比。
在 unix 中删除目录内容而不会收到抱怨目录已经为空的消息的简洁方法是什么?
dep*_*uid 62
既然您想在没有提示的情况下删除所有文件,为什么不直接使用-f
开关rm
来忽略不存在的文件呢?
rm -f /tmp/our_cache/*
Run Code Online (Sandbox Code Playgroud)
从手册页:
-f, --force
ignore nonexistent files, never prompt
Run Code Online (Sandbox Code Playgroud)
此外,如果其中可能有任何子目录,/tmp/our_cache/
并且您还希望删除这些子目录及其内容,请不要忘记-r
切换。
find /tmp/our_cache/ -mindepth 1 -delete
Run Code Online (Sandbox Code Playgroud)
编辑 1
删除“-type f
编辑 2
添加了非标准选项-mindepth 1
,以防止删除搜索根目录(取消-type f
限制后)。