我想删除文件夹中少于X文件的所有子文件夹
以下代码搜索少于X文件的子文件夹:
$ find . -type d -exec sh -c 'set -- "$0"/*; [ $# -le 10 ]' {} \; -print
./digna_1919
./digna_2040
./digna_1682
(more output omitted)
所以我可以找到它们!但如果我这样做,我会得到“目录不空”:
$ find . -type d -exec sh -c 'set -- "$0"/*; [ $# -le 10 ]' {} \; -delete
find: cannot delete ‘./digna_1919’: Directory not empty
find: cannot delete ‘./digna_2040’: Directory not empty
find: cannot delete ‘./digna_1682’: Directory not empty
(more output omitted)
如果我这样做,我会得到“没有这样的文件或目录”:
$ find . -type d -exec sh -c 'set -- "$0"/*; [ $# -le 10 ]' {} \; -exec rm -r "{}" \;
find: ‘./digna_1919’: No such file or directory
find: ‘./digna_2040’: No such file or directory
find: ‘./digna_1682’: No such file or directory
(more output omitted)
我哪里做错了?非常感谢!
使用-depth
让find
处理目录本身之前的每个目录的内容。删除项目时,您将要执行此操作。
$ find . -depth -type d -exec sh -c 'set -- "$0"/*; [ $# -le 10 ]' {} \; -exec rm -r {} \;