[英]Deleting a directory starting with a specific string in shell script
When I'm trying to delete all directories starting with tmp, I used this command: 当我尝试删除以tmp开头的所有目录时,我使用了以下命令:
find -type d -name tmp* -exec rmdir {} \;
And it does the trick, but this command is exiting with an error code: 它可以解决问题,但是此命令以错误代码退出:
find: `./tmp09098': No such file or directory
What causing failing my build. 是什么导致我的构建失败。
Can anyone tell me how I can delete those folders without getting the error? 谁能告诉我如何删除这些文件夹而不会出现错误?
After trying what @anubhava suggested and quoted 'temp*', 在尝试了@anubhava的建议并引用了'temp *'之后,
find -type d -name 'tmp*' -exec rmdir {} \;
I still get the same error: 我仍然收到相同的错误:
find: `./tmp0909565g': No such file or directory
find: `./tmp09095': No such file or directory
When running: 运行时:
find -type d -name 'tmp*' -exec ls -ld '{}' \;
This is the result: 结果如下:
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
You should quote the pattern otherwise it will be expanded by shell on command line: 您应该引用该模式,否则它将在命令行上被shell扩展:
find . -type d -name 'tmp*' -mindepth 1 -exec rm -rf '{}' \; -prune
-prune
causes find
to not descend into the current file/dir. -prune
导致find
不属于当前文件/目录。
This works for me: 这对我有用:
#! /bin/bash
tmpdirs=`find . -type d -name "tmp*"`
echo "$tmpdirs" |
while read dir;
do
echo "Removing directory $dir"
rm -r $dir;
done;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.