3

Under a directory, I have a bunch of directories named Debug. How do I delete all the Debug directories?

3

It's not clear --- under the directory dir_a you can have only one directory named Debug. Or are you saying that you have dir_a/Debug, dir_a/dir_b/Debug, dir_a/dir_c/Debug and so on?

You can list all directory named exactly Debug under the directory dir_a with:

cd dir_a   # or whatever you need to go there
find . -type d -name Debug

this will list all the directories named Debug under the current directory. To delete them (double check, this IS NOT UNDOABLE):

find . -depth -type d -name Debug -exec rm -r {} \; 

-depth will list Debug/Debug/ before Debug/ to avoid errors.

In detail:

  1. find . -type d -name Debug will search all directory under the current one (.) for entries which are both directory (-type d) and have the name "Debug" (-name Debug).

  2. The added -depth flag make the search depth-first (it means that "deeper" matches are found before "shallow" ones).

  3. -exec ... \; tells find to execute the command in ... for each match. (The semicolon is quoted because otherwise the shell will interpret it).

  4. In the command ... above, the special symbol {} is substituted by the current match.

So in plain English it is: find every directory under this one which name is "Debug", depth first, and for each one execute the command rm -r followed by the full name of the directory.

| improve this answer | |
  • If you use GNU find's -delete option, does that remove the need for -depth? – terdon Nov 20 '14 at 14:57
  • 1
    @terdon thought about it (the man page says that -delete implies -depth), but it protests about directories not empty... that was the reason to use the rm -r things. – Rmano Nov 20 '14 at 15:38
  • In "rm -r {} \;" command, what does "{} \;" option mean? Try to google rm linux command, but find none examples for that option. Should I find them in examples of other commands? – sontra Nov 21 '14 at 16:54
  • @sontra, You will find the description of {} and \; in the find manual, not rm. I added some detailed info. – Rmano Nov 21 '14 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.