I have problems running the bash code

mdfind -onlyin /some/folder my_string

This works on the terminal, but if I try

RunProcess[{"/usr/bin/mdfind", "-onlyin /some/folder", "my_string"}]

I get as an error

 Unknown option -onlyin /some/foder


RunProcess[{"/usr/bin/mdfind", "-onlyin", "/some/folder my_string"}]

gives as an error

/usr/bin/mdfind: no query specified

What is the correct way of handling this?

  • 1
    $\begingroup$ All the elements must be in separate items, this works for me : RunProcess[{"mdfind", "-onlyin", "/some/folder", "my_string"}] or RunProcess[{"mdfind", "-onlyin", "/some/folder", "my_string"}, "StandardOutput"] if you only need the standard output of the command, here the filenames. $\endgroup$ – SquareOne Dec 15 '14 at 14:24
  • $\begingroup$ Works for me thanks. You should expand it to an answer $\endgroup$ – mete Dec 15 '14 at 14:45

I actually found 3 ways to execute your command with RunProcess (on Unix like systems) :

Let's write your command as a string :

mycommand = "mdfind -onlyin /some/folder my_string";

but it could be any other command like :

mycommand = "ls -la";

Then these 3 inputs are equivalent :


RunProcess[{$SystemShell, "-c", mycommand}]

RunProcess[$SystemShell, All, mycommand <> "\nexit\n"]

In case you are just interested in the output not in the exit code nor error message, you just have to add/specify "StandardOutput" :

RunProcess[StringSplit@mycommand, "StandardOutput"]

RunProcess[{$SystemShell, "-c", mycommand}, "StandardOutput"]

RunProcess[$SystemShell, "StandardOutput", mycommand <> "\nexit\n"]

See also this and that posts for other useful approaches.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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