Linux 如何删除具有特定名称的文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13032701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:35:01  来源:igfitidea点击:

How to remove folders with a certain name

linuxunixrm

提问by Joe

In Linux, how do I remove folders with a certain name which are nested deep in a folder hierarchy?

在 Linux 中,如何删除嵌套在文件夹层次结构深处的具有特定名称的文件夹?

The following paths are under a folder and I would like to remove all folders named a.

以下路径位于一个文件夹下,我想删除所有名为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

What Linux command should I use from the parent folder?

我应该从父文件夹使用什么 Linux 命令?

采纳答案by pistache

If the target directory is empty, use find, filter with only directories, filter by name, execute rmdir:

如果目标目录为空,则使用find,只过滤目录,按名称过滤,执行rmdir:

find . -type d -name a -exec rmdir {} \;

If you want to recursively delete its contents, replace -exec rmdir {} \;by -deleteor -prune -exec rm -rf {} \;. Other answers include details about these versions, credit them too.

如果要递归删除其内容,请替换-exec rmdir {} \;-delete-prune -exec rm -rf {} \;。其他答案包括有关这些版本的详细信息,也请相信他们。

回答by wmorrison365

Use find for name "a" and execute rm to remove those named according to your wishes, as follows:

使用 find for name "a" 并执行 rm 根据您的意愿删除那些命名的,如下所示:

find . -name a -exec rm -rf {} \;

Test it first using ls to list:

首先使用 ls 来测试它:

find . -name a -exec ls {} \;

To ensure this only removes directories and not plain files, use the "-type d" arg (as suggested in the comments):

为确保这仅删除目录而不是普通文件,请使用“-type d”参数(如评论中所建议):

find . -name a -type d -exec rm -rf {} \;

The "{}" is a substitution for each file "a" found - the exec command is executed against each by substitution.

“{}”是对找到的每个文件“a”的替换 - exec 命令通过替换对每个文件执行。

回答by Slevin

find ./ -name "FOLDERNAME" | xargs rm -Rf

Should do the trick. WARNING, if you accidentally pump a .or /into xargs rm -Rfyour entire computer will be deleted without an option to get it back, requiring an OS reinstall.

应该做的伎俩。警告,如果您不小心将一个./注入xargs rm -Rf您的整个计算机将被删除,而无法选择将其取回,需要重新安装操作系统。

回答by Arash Fotouhi

This also works - it will remove all the folders called "a" and their contents:

这也有效 - 它将删除所有名为“a”的文件夹及其内容:

rm -rf `find -type d -name a`

回答by Thamaraiselvam

I had more than 100 files like

我有超过 100 个文件,例如

log-12
log-123
log-34
....

above answers did not work for me

以上答案对我不起作用

but the following command helped me.

但以下命令帮助了我。

find . -name "log-*" -exec rm  -rf {} \;

i gave -typeas .so it deletes both files and folders which starts with log-

我给-type.,因此删除与启动文件和文件夹log-

and rm -rfdeletes folders recursively even it has files.

rm -rf递归删除文件夹,即使它有文件。

if you want folders alone

如果你只想要文件夹

find -type d -name "log-*" -exec rm  -rf {} \;

files alone

单独的文件

find -type f -name "log-*" -exec rm  -rf {} \;

回答by David Grayson

To delete all directories with the name foo, run:

要删除名称为 的所有目录foo,请运行:

find -type d -name foo -a -prune -exec rm -rf {} \;

The other answers are missing an important thing: the -pruneoption. Without -prune, GNU find will delete the directory with the matching name and then try to recurse into it to find more directories that match. The -pruneoption tells it to not recurse into a directory that matched the conditions.

其他答案缺少一个重要的东西:-prune选项。如果没有-prune,GNU find 将删除具有匹配名称的目录,然后尝试递归到其中以查找更多匹配的目录。该-prune选项告诉它不要递归到与条件匹配的目录。

回答by dgrigonis

Another one:

另一个:

"-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)

回答by PickleRick

find path/to/the/folders -maxdepth 1 -name "my_*" -type d -delete

find path/to/the/folders -maxdepth 1 -name "my_*" -type d -delete

回答by mpalumbo7

I ended up here looking to delete my node_modulesfolders before doing a backup of my work in progress using rsync. A key requirements is that the node_modulesfolder can be nested, so you need the -pruneoption.

我最终在这里希望删除我的node_modules文件夹,然后再使用rsync. 一个关键要求是node_modules文件夹可以嵌套,因此您需要该-prune选项。

First I ran this to visually verify the folders to be deleted:

首先,我运行它以直观地验证要删除的文件夹:

find -type d -name node_modules -prune

Then I ran this to delete them all:

然后我运行它以将它们全部删除:

find -type d -name node_modules -prune -exec rm -rf {} \;

Thanks to pistache

感谢开心果

回答by marcosfede

Combining multiple answers, here's a command that works on both Linux and MacOS

结合多个答案,这是一个适用于 Linux 和 MacOS 的命令

rm -rf $(find . -type d -name __pycache__)