This question already has an answer here:

I need to substitute output of find command to another command to process every found file, e.g.:

mdls `find ~/target_dir/ -iname '*some*' -depth 1`

(mdls is a command in OS X which get metadata attributes of the specified file. It doesn't support piping thus find ... | mdls fails.)

The command above works fine, but expectedly fails on files with spaces in name. I thought that adding quotes via sed helps with it:

$ find ~/target_dir/ -iname '*some*' -depth 1 | sed 's/\(.*\)/"\1"/'
"/Users/shau-kote/target_dir//some file with spaces in name"

Alack, now my mdls command fails on all files:

$ mdls `find ~/target_dir/ -iname '*some*' -depth 1 | sed 's/\(.*\)/"\1"/'`
"/Users/shau-kote/target_dir//secondsomefile": could not find "/Users/shau-kote/target_dir//secondsomefile".

How can I fix it so as mdls do correctly process all file names from find?

Thank you.

P.S. I'm not sure is it important or self-evident, but

mdls "/Users/shau-kote/target_dir//secondsomefile"

works properly.

marked as duplicate by Gilles bash Apr 7 '16 at 22:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Use find's -exec command:

find ~/target_dir/ -iname '*some*' -depth 1 -exec mdls {} \;

This will run mdls on every matching filename found by find. It will work with any filename, even those containing spaces or newlines etc.

if mdls can work with multiple filenames on the command line, you could terminate the -exec command with + instead of \;. e.g.

find ~/target_dir/ -iname '*some*' -depth 1 -exec mdls {} +

Not the answer you're looking for? Browse other questions tagged or ask your own question.