2017-03-10 121 views
0

执行此命令rm:无法删除目录:`。'

find . -maxdepth 1 '!' -name .scm -exec rm -rf {} + || true 

是非常好的,如果没有文件可用。否则会产生如下消息

rm: cannot remove directory: `.' 

但是该命令按预期工作。我是否必须为这条信息烦恼,我该如何克服这一点?

问候

+4

您也可以添加'-mindepth 1',以防止包含'.'。 – Jameson

+0

@詹姆森伟大的建议! – jm666

回答

1

在bash可以简化这个使用extended globbing(匹配与.scm结尾的文件,包括.scm本身):

$ shopt -s extglob 
$ cd -- "$(mktemp --directory)" 
$ touch .scm bar.txt 
$ ls -a !(*.scm) # Change to `rm -rf` 
bar.txt 

也可以按照Jameson's suggestion和使用-mindepth

find . -mindepth 1 -maxdepth 1 '!' -name .scm -exec rm -rf {} + || true