Delete matching directories in all subdirectories

Posted on

Problem :

How can I remove all run-*.achilles directories in all of my subdirectories under Linux?

I tried find /path -name run-*.achilles -type f -delete but it doesn’t work.

Solution :

Several issues here:

  1. You should quote * to prevent shell globbing.
  2. -type f tells find you want files.
  3. find -delete won’t delete non-empty directories. See this question. Adjusted solution from one of the answers there:

    find /path -path '*/run-*.achilles/*' -delete
    find /path -type d -name 'run-*.achilles' -empty -delete
    

It’s not perfect, the first line will match .../run-a/b.achilles/.... I think. This command should match better:

find /path -type d -name 'run-*.achilles' -exec rm -rf {} +

It uses rm -rf, so be careful with it.