2

How would I remove all of the items returned by mdfind? For example, I want to remove all files relating to "MacLaunchpad", so I did

mdfind "MacLaunchpad"

Which returned

/private/var/db/receipts/o.t.maclaunchpad.postflight.pkg.plist
/private/var/db/receipts/o.t.maclaunchpad.maclaunchpad.pkg.plist
/private/var/db/receipts/o.t.maclaunchpad.postflight.pkg.bom
/private/var/db/receipts/o.t.maclaunchpad.launchpad.pkg.plist
/private/var/db/receipts/o.t.maclaunchpad.launchpad.pkg.bom
/private/var/db/receipts/o.t.maclaunchpad.maclaunchpad.pkg.bom

I wish to remove all of these files; how would I go about doing that?

3

There are at least two ways for this

  • rm $(mdfind "MacLaunchpad")
  • mdfind "MacLaunchpad" | xargs rm

If you expect the resulting file/path names to contain spaces and other surprises use

mdfind -0 "MacLaunchpad" | xargs -0 rm --
  • Unfortunately this is not general enough to handle spaces in the file names/paths. Is there a modification that can handle that? – RoG Sep 19 '18 at 15:07
  • 1
    @RoG Yes there is, see updated answer. Thanks for pointing this out. – nohillside Sep 19 '18 at 15:19
  • Great! Is there a trick to get this to work with grep (e.g. a grep --null py$ or similar) between the two commands? – RoG Sep 19 '18 at 15:26
  • 1
    @RoG Not directly, you probably need to execute something like grep pattern {} && rm -- {} through xargs. – nohillside Sep 19 '18 at 15:43
  • Thanks. I noticed that there is a -onlyin /path/to/use parameter to mdfind which helps a lot, even if it is not as fully general as using grep. – RoG Sep 19 '18 at 15:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .