4

Hi I've got lots of folders with the name "@eaDir" all across one of my disks and I'd like to search through, find all of them and delete them and their contents.

I know this is probably a combination of the find and rm command but I can't quite figure them out. Can anyone help?

Adam
  • 532
  • 3
  • 11
  • 22

3 Answers3

4

Try this:

find . -type d -name '@eaDir' -print0 | xargs -rt0 rm -rv

Here's the same thing but using explicit long options for xargs:

find . -type d -name '@eaDir' -print0 | xargs --no-run-if-empty --verbose --null rm -rv

(using long options is always a good idea if you're writing scripts that will need to be maintained/reviewed by other people)

But before anything else:

man find
man xargs
dschulz
  • 4,666
  • 1
  • 31
  • 31
  • Why using a non-standard argument to find, `find . -type d -name '@eaDir' -exec rm -rv {} \;` is fine, no ? – jfg956 Dec 26 '12 at 10:49
  • Yes, that's fine too. The difference is that with `-exec` option `find` will spawn a `rm` process for each directory found. With `xargs` the argument list for `rm -rv` is built, and then `rm -rv [argument list here]` is invoked once. – dschulz Dec 27 '12 at 06:25
1
find /path/to/the/disk -type d -name "@eaDir" -delete

Notice that the order here is fundamental: quoting the manpage,

Warnings: Don't forget that the find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified.

So, as always, first try your find command with -print, then, when you checked that everything works fine, replace it with -delete. Notice that -delete implies -depth, so, to do meaningful testing with -print, you should explicitly specify it in the expression:

When testing a find command line that you later intend to use with -delete, you should explicitly specify -depth in order to avoid later surprises.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • Right, but i'd be wary to use and recommend `-delete`. It has the potential to cause a disaster putting it in the wrong place/order. – dschulz Dec 25 '12 at 23:56
  • 1
    @dschulz: yes, the order thing here is fundamental, if you put it before the `-name` it will happily erase everything under the specified root. On the other hand, the usual rule about mass deletion and shell scripting applies here: *first* test with `-print` (`echo`), *then* put `-delete` (`rm`) in its place. – Matteo Italia Dec 25 '12 at 23:59
0

Goto root folder or directory and execute the following command:

find . -path '*/@eaDir/*' -delete -print && find . -path '*/@eaDir' -delete -print

This should work for you.