xargs on OSX: illegal option --

23,198

Solution 1

xargs on Mac OS X doesn't support the --replace option; you can use -I instead:

find docs/ -name "*png" | xargs -I F python myscript.py "F"

The strange error message is produced because this version of xargs interprets characters after a single - as options, so with --replace it's looking for an option named -, which doesn't exist.

Solution 2

Mac OSX xargs does not support long options like GNU xargs. For using --replace like GNU xargs, use -I:

find docs/ -name "*png" | xargs -I F python myscript.py "F"

Note that this approach breaks with file name contain newline, you want to use find -print0 with xargs -0:

find docs/ -name "*png" -print0 | xargs -0 -I F python myscript.py "F"

or standard one:

find docs/ -name "*png" -exec python myscript.py {} +
Share:
23,198

Related videos on Youtube

Richard
Author by

Richard

Updated on September 18, 2022

Comments

  • Richard
    Richard over 1 year

    I'm on OSX. I want to run a python script against all pngs in a particular directory. This is what I've tried:

    find docs/ -name "*png" | xargs --replace=F python myscript.py "F"
    

    But I see this:

    xargs: illegal option -- -
    usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
                 [-L number] [-n number [-x]] [-P maxprocs] [-s size]
                 [utility [argument ...]]
    

    What am I doing wrong?

    • Admin
      Admin almost 8 years
      Your problem is that you memorized non-portable GNUisms instead of using the portable set of options.
  • schily
    schily almost 8 years
    In general, it is a good idea to only learn and use the portable standard options instead of using GNUisms. This is why I encourage people to change their mind when someone again send an answer with a non-portable GNUism.
  • cuonglm
    cuonglm almost 8 years
    @schily: Sticking with the standard is good, but we should also offer all the tools available for user.
  • schily
    schily almost 8 years
    More than 90% of the Answers in this forum that use GNUisms could be written in a portable way without giving up any functionality.