在 shell 脚本中删除以特定字符串开头的目录
Deleting a directory starting with a specific string in shell script
当我试图删除所有以 tmp 开头的目录时,
我使用了这个命令:
find -type d -name tmp* -exec rmdir {} \;
它确实有效,但此命令正在退出并显示错误代码:
find: `./tmp09098': No such file or directory
是什么导致我的构建失败。
谁能告诉我如何删除这些文件夹而不出现错误?
尝试@anubhava 建议和引用的内容后 'temp*',
find -type d -name 'tmp*' -exec rmdir {} \;
我仍然得到同样的错误:
find: `./tmp0909565g': No such file or directory
find: `./tmp09095': No such file or directory
当运行时:
find -type d -name 'tmp*' -exec ls -ld '{}' \;
这是结果:
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
您应该引用该模式,否则它将在命令行中被扩展 shell:
find . -type d -name 'tmp*' -mindepth 1 -exec rm -rf '{}' \; -prune
-prune
导致 find
不下降到当前 file/dir。
这对我有用:
#! /bin/bash
tmpdirs=`find . -type d -name "tmp*"`
echo "$tmpdirs" |
while read dir;
do
echo "Removing directory $dir"
rm -r $dir;
done;
当我试图删除所有以 tmp 开头的目录时, 我使用了这个命令:
find -type d -name tmp* -exec rmdir {} \;
它确实有效,但此命令正在退出并显示错误代码:
find: `./tmp09098': No such file or directory
是什么导致我的构建失败。
谁能告诉我如何删除这些文件夹而不出现错误?
尝试@anubhava 建议和引用的内容后 'temp*',
find -type d -name 'tmp*' -exec rmdir {} \;
我仍然得到同样的错误:
find: `./tmp0909565g': No such file or directory
find: `./tmp09095': No such file or directory
当运行时:
find -type d -name 'tmp*' -exec ls -ld '{}' \;
这是结果:
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
您应该引用该模式,否则它将在命令行中被扩展 shell:
find . -type d -name 'tmp*' -mindepth 1 -exec rm -rf '{}' \; -prune
-prune
导致 find
不下降到当前 file/dir。
这对我有用:
#! /bin/bash
tmpdirs=`find . -type d -name "tmp*"`
echo "$tmpdirs" |
while read dir;
do
echo "Removing directory $dir"
rm -r $dir;
done;