在Linux中,如何删除在文件夹层次结构中嵌套的某个名称的文件夹?
以下路径是在一个文件夹下,我想删除所有名为a的文件夹。
1/2/3/a
1/2/3/b
10/20/30/a
10/20/30/b
100/200/300/a
100/200/300/b
我应该从父文件夹中使用什么Linux命令?
在Linux中,如何删除在文件夹层次结构中嵌套的某个名称的文件夹?
以下路径是在一个文件夹下,我想删除所有名为a的文件夹。
1/2/3/a
1/2/3/b
10/20/30/a
10/20/30/b
100/200/300/a
100/200/300/b
我应该从父文件夹中使用什么Linux命令?
当前回答
结合多个答案,下面是一个在Linux和MacOS上都可以使用的命令
rm -rf $(find . -type d -name __pycache__)
其他回答
另一个:
"-exec rm -rf {} \;" can be replaced by "-delete"
find -type d -name __pycache__ -delete # GNU find
find . -type d -name __pycache__ -delete # POSIX find (e.g. Mac OS X)
这也可以工作-它将删除所有名为“a”的文件夹及其内容:
rm -rf `find . -type d -name a`
使用find查找名称“a”,并执行rm根据您的意愿删除名称,如下所示:
find . -name a -exec rm -rf {} \;
首先使用ls来测试它:
find . -name a -exec ls {} \;
为了确保只删除目录而不是普通文件,使用"-type d"参数(正如注释中建议的那样):
find . -name a -type d -exec rm -rf {} \;
“{}”是对找到的每个文件“a”的替换——exec命令通过替换对每个文件执行。
查找路径/to/the/folders -maxdepth 1 -name "my_*" -type d -delete
这个命令对我有用。它以递归的方式工作
find . -name "node_modules" -type d -prune -exec rm -rf '{}' +
. -当前文件夹
"node_modules" -文件夹名称