How to remove folders with a certain name

LinuxUnixRm

Linux Problem Overview


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

The following paths are under a folder and I would like to remove all folders named 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 Solutions


Solution 1 - Linux

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

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

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

Solution 2 - Linux

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

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

Test it first using ls to list:

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

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.

Solution 3 - Linux

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

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

Solution 4 - Linux

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

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

Solution 5 - Linux

To delete all directories with the name foo, run:

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

The other answers are missing an important thing: the -prune option. 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 -prune option tells it to not recurse into a directory that matched the conditions.

Solution 6 - Linux

This command works for me. It does its work recursively

find . -name "node_modules" -type d -prune -exec rm -rf '{}' +

. - current folder

"node_modules" - folder name

Solution 7 - Linux

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

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

Solution 8 - Linux

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

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

Solution 9 - Linux

I had more than 100 files like

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 -type as . so it deletes both files and folders which starts with log-

and rm -rf deletes folders recursively even it has files.

if you want folders alone

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

files alone

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

Solution 10 - Linux

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)

Solution 11 - Linux

Earlier comments didn't work for me since I was looking for an expression within the folder name in some folder within the structure

The following works for a folder in a structure like:

b/d/ab/cd/file or c/d/e/f/a/f/file

To check before using rm-rf

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

Removing folders including content recursively

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

Solution 12 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoeView Question on Stackoverflow
Solution 1 - LinuxpistacheView Answer on Stackoverflow
Solution 2 - Linuxwmorrison365View Answer on Stackoverflow
Solution 3 - LinuxArash FotouhiView Answer on Stackoverflow
Solution 4 - Linuxmpalumbo7View Answer on Stackoverflow
Solution 5 - LinuxDavid GraysonView Answer on Stackoverflow
Solution 6 - Linux1nstinctView Answer on Stackoverflow
Solution 7 - LinuxSlevinView Answer on Stackoverflow
Solution 8 - LinuxmarcosfedeView Answer on Stackoverflow
Solution 9 - LinuxThamaraiselvamView Answer on Stackoverflow
Solution 10 - LinuxdgrigonisView Answer on Stackoverflow
Solution 11 - LinuxFritz NView Answer on Stackoverflow
Solution 12 - LinuxPickleRickView Answer on Stackoverflow