2

Trying to answer the question Are true dynamic folders (NOT a “Smart Folder” SavedSearch) possible? I stumbled about the problem to loop the results of an mdfind search as quoted or escaped paths.

Code snippet:

for File in $(mdfind -onlyin $MusicSamples 'kMDItemAudioBitRate >= "44000"  && _kMDItemUserTags = "Sample"')
do
    ln -s $File $DrumFoldr
done

The for loop should create soft links of all matching files in the folder $MusicSamples or its sub folders in the folder $DrumFoldr. The loop works for files with paths/file names without spaces.

  • The answer to a similar question simply suggests to quote $File (... "$File" ...). This doesn't work - it simply creates broken soft links with names of the contiguous strings in the original file name: a file named "1. Artist - Song - Mix.mp3" will create four or five soft links: "1.", "Artist", "Song", "Mix.mp3" and "-".

  • Escaping the spaces by piping the mdfind results to a sed command replacing a space with an escaped space will result in something like "\ 1.", "\ Artist" etc.

  • Creating an array of the mdfind results with:

    result=()
    mdfind ... | while IFS= read -r filename; do
      result+=("$filename")
    done
    

    and using it in for File in "${result[@]}"; do ln -s "$File" $DrumFoldr; done doesn't work either.

How do I properly escape those spaces in the paths?

  • mdfind has a null option. You could then try piping the results through xargs. – fd0 Apr 17 '17 at 19:47
  • @fd0 I tried this also (haven't mentioned it in my question though) - without success. But I may have made a mistake in the command. – klanomath Apr 17 '17 at 20:00

You must log in to answer this question.

Browse other questions tagged .