Spotlight – How to Remove All mdfind Results with Spaces in Pathnames

spotlight

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)'

returns

/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!

Best Answer

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.