249

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Joe
  • 14,513
  • 28
  • 82
  • 144
  • Perhaps missing the point but maybe it does assist someone: I have 410 folders in a main folder - each of the 410 with two sub-folders. All first sub-folders are named 'final' and contains edited pdfs and a Word content list. All the second sub-folders named tif_pdf_various_names contains originally scanned saved-to tiff files (some up to 200 pages), their un-edited pdfs and a draft .docx content list - thus a home work collection. I needed to make a collection of the 410 folders containing only the finally edited material - with the tiffs close to 4TB. Easiest method found was to use (in Micr – Jurie Jan 05 '19 at 11:26

12 Answers12

264

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.

Jamie
  • 3,105
  • 1
  • 25
  • 35
pistache
  • 5,782
  • 1
  • 29
  • 50
  • 17
    Could you please explain what `{} \;` does? – winklerrr Jan 21 '19 at 16:40
  • 16
    `{}` can be read as "for each matching file/ folder" - i.e. it substitutes for each "found" file/ folder. `\;` is a terminator for the `-exec` clause. – wmorrison365 Mar 28 '19 at 14:18
  • 7
    This gives me a "cannot delete" error when the directory is not empty – blindeyes Jul 19 '19 at 10:06
  • 5
    Consider adding `-prune` flag to the answer as per @David Grayson suggestion. Without this, `find` will still try to visit the now missing folder and will eventually exit with an error code, which can e.g. fail a Docker build. – Czyzby Mar 18 '20 at 13:19
243

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.

wmorrison365
  • 5,995
  • 2
  • 27
  • 40
  • 5
    You probably want to add a "-type d" in there just incase a file matches the same name. – Mark Loeser Oct 23 '12 at 14:29
  • 3
    Also note that this will catch something like `1/2/a/3/4`, which may not be intended, so +1 for the recommendation to test first... – twalberg Oct 23 '12 at 14:46
  • 16
    reco `echo {}` to test it first, instead of `ls {}` – Charney Kaye Nov 18 '16 at 06:25
  • 18
    Here go my `node_modules/`. – saruftw Aug 09 '18 at 10:09
  • 14
    I used `find . -name a -type d -exec rm -rf {} \;` and it worked, although it printed out `find: ‘./subdir/subdir/a’: No such file or directory` for each directory deleted. – pacoverflow Mar 19 '19 at 19:21
  • 2
    The 'No such file or directory' error comes from find because it tries to continue finding in that same folder you just removed. Use -prune to prevent that and shut it up. – floydheld Nov 24 '21 at 17:09
110

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

rm -rf `find . -type d -name a`
M. Reza Nasirloo
  • 16,434
  • 2
  • 29
  • 41
Arash Fotouhi
  • 1,933
  • 2
  • 22
  • 43
  • @Buffalo, how come? This defo works but so do the others. Would be surprised to find an env where this works but the others don't given all answers are essentially `find/rm`. – wmorrison365 Jul 15 '16 at 14:19
  • 9
    @wmorrison365 because if you do `find . -name a -exec rm -rf {} \;`, find will crash when it tries to enter that directory to look for more items, giving you an error like `find: '1/2/3/a': No such file or directory`. – Alex Grönholm Jan 09 '17 at 19:57
  • @Alex Grönholm sure, but all folders named 'a' are nevertheless deleted. As you state, find can't subsequently look inside a deleted 'a' folder but that doesn't matter. Wanting to keep content of 'a' for subsequent processing before delete is a different use-case to what the OP asked. – wmorrison365 Jan 10 '17 at 10:50
  • 10
    @wmorrison365 Yes, the directories are deleted but I don't think it's good to recommend a solution where the command fails (returns a nonzero exit code). – Alex Grönholm Jan 11 '17 at 11:58
  • 4
    This was giving me an error `illegal option --t`. I had to also pass the directory to search in (https://stackoverflow.com/questions/25840713/illegal-option-error-when-using-find-on-macos): `rm -rf \`find . -type d -name a\``. – Steve Harrison Jun 17 '19 at 01:51
  • I need to add a `.` --> `rm -rf \`find . -type d -name a\`` – I.G. Pascual Aug 26 '20 at 15:49
63

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

Tudor
  • 4,137
  • 5
  • 38
  • 54
mpalumbo7
  • 1,180
  • 10
  • 11
  • Ahh that worked nicely, to make this something you could use with ease you could save this as a little shellscript: ```sh #!/bin/bash echo "Found node_modules: "; find -type d -name node_modules -prune; read -r -p "Do you really want to delete all directories? [yes/no] " input; if [ ! $input != "yes" ]; then find -type d -name node_modules -prune -exec rm -rf {} \;; fi ``` – Feirell Jun 10 '19 at 19:37
  • 1
    On my Macbook Pro, when I ran your command, I got "illegal option -- t". So I added a dot. My command: `find . -type d -name node_modules -prune -exec rm -rf {} \;` – Christiaan Westerbeek Oct 31 '20 at 18:40
  • And this is why I'm switching to `pnpm` just cleared over 150gb on my computer. Thanks for this one. – timhc22 Jul 19 '23 at 03:12
37

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.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
23

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

1nstinct
  • 1,745
  • 1
  • 26
  • 30
16
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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Slevin
  • 4,268
  • 12
  • 40
  • 90
15

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

rm -rf $(find . -type d -name __pycache__)
marcosfede
  • 355
  • 2
  • 6
13

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 {} \;
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
7

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)
dgrigonis
  • 387
  • 5
  • 10
4

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 {} \;
Fritz N
  • 41
  • 2
-2

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

PickleRick
  • 419
  • 1
  • 5
  • 13