I already solved this with sed, but there must be a better way to handle spaces. I have run into this with some my scripts as well.

mdfind '(kMDItemFSName=*.ipsw)'


/Volumes/disk/november 2014/Users/someuser/Library/iTunes/iPhone Software Updates/iPhone4,2_6.1_12B411_Restore.ipsw

when trying to pipe this, it breaks because of the spaces. i tried several methods including

rm $(mdfind '(kMDItemFSName=*.ipsw)')

mdfind '(kMDItemFSName=*.ipsw)' |xargs rm -vf

the only solution i found was

mdfind '(kMDItemFSName=*.ipsw)' |sed -e 's/ /\\ /g' |xargs rm -vf

but there must be a better way.

Thanks for your help!

1 Answer 1


You can use mdfind -0 to print a null character after each path. Then, xargs -0 to parse the list on each null character instead of using the default whitespace.

  • it works, what more can i say
    – jakethedog
    Commented Oct 15, 2015 at 16:43
  • I'm having this same issue, could someone please elaborate on this answer? I don't understand what exactly to do with the information.
    – JVC
    Commented Apr 17, 2017 at 17:40
  • @JonathanvanClute, what is the trouble you're having? If you need help with mdfind, I'd suggest opening a new question. This one is mainly concerned with running a command on each of the results of mdfind (in this case, the command is "rm -vf"), when the mdfind list has pathnames which contain whitespace. By telling mdfind to put a null character at the end of each item it finds, and telling xargs to look for the null character when deciding which results to run the command on, the original question was solved.
    – Kent
    Commented Apr 18, 2017 at 0:41
  • I'm having an issue with spaces in a path and mdfind, but seem to have fixed it by quoting the variable that stored the path that contained a space. My problem was that I have little idea what the answer is talking about. I'm not a programmer so this answer is essentially a fragment, which while appearing to address my exact issue, was not fleshed out enough for me to have any idea how to use it. I think I'm already set though so, thanks anyway!
    – JVC
    Commented Apr 18, 2017 at 0:44
  • The answer be improved with some explained examples? like mdfind -0 -name IMG_1910 | xargs -0 ls -li (with explanation - would that help, @JonathanvanClute?) Commented Feb 19, 2018 at 0:37

You must log in to answer this question.

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