1

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//secondsomefile"
"/Users/shau-kote/target_dir//some file with spaces in name"
"/Users/shau-kote/target_dir//somefile"

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.

shau-kote
  • 622
  • 1
  • 5
  • 13

1 Answers1

1

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 {} +
cas
  • 78,579