8
find .   -name "DIR-NAME" -type d -delete

because i want to delete specific dir name recursively well it returns

find: cannot delete `./DIR-NAME': Directory not empty

which is expeted but I have no idea how to make -delete to delete non-empty dirs here as i can with rm -rf

Braiam
  • 35,380
  • 25
  • 108
  • 167
user240891
  • 81
  • 1
  • 2

2 Answers2

8

Like this :

find . -name "DIR-NAME" -type d -exec rm -r {} +
Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82
  • Maybe better as `find . -name DIR-NAME -type d -prune -exec rm -rf {} +` – Stéphane Chazelas Mar 15 '23 at 17:38
  • As the time that was written, that was not fully supported everywhere ^^ IIRC – Gilles Quénot Mar 15 '23 at 17:41
  • See [find . -print0 | xargs -0 cmd vs find . -exec cmd {} +](https://unix.stackexchange.com/q/730873) about history of `-exec {} +`. I'd say it was about as common in 2014 as now. My point was more about `-prune`. Feel free to include my comment on Andy's answer; it looks like his account is not longer active. – Stéphane Chazelas Mar 15 '23 at 17:47
  • For the love of god, quote your `{}` arguments, otherwise you have a security issue if someone make a directory with a space it can delete directories outside of the directory and even the root – Tofandel Aug 02 '23 at 20:01
1

Before you delete directories, you could delete files. ie first run:

find .   -name "DIR-NAME" -type f -delete

Might not work if you have special file types.

Andy
  • 389
  • 1
  • 5
  • 1
    Wow, sure missed that argument. I'd suggest then " find . -name "DIR-NAME" -type d -exec rm -rf {} ';'" and I'll see myself out. – Andy Dec 23 '14 at 21:17
  • ITYM something like `LC_ALL=C find . '(' -path '*/DIR-NAME/*' -o -name DIR-NAME -type d ')' -delete`. – Stéphane Chazelas Mar 15 '23 at 17:40