删除少于X个文件的所有子文件夹

问题描述 投票:1回答:1

我想删除文件夹中少于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)

我哪里做错了?非常感谢!

Source of the code

bash terminal find
1个回答
0
投票

使用-depthfind处理目录本身之前的每个目录的内容。删除项目时,您将要执行此操作。

$ find . -depth -type d -exec sh -c 'set -- "$0"/*; [ $# -le 10 ]' {} \; -exec rm -r {} \;
© www.soinside.com 2019 - 2024. All rights reserved.