linuxunixrm

How to remove folders with a certain name


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?


Solution

  • 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.