Search code examples
xargs

Make xargs execute the command once for each line of input


How can I make xargs execute the command exactly once for each line of input given? It's default behavior is to chunk the lines and execute the command once, passing multiple lines to each instance.

From http://en.wikipedia.org/wiki/Xargs:

find /path -type f -print0 | xargs -0 rm

In this example, find feeds the input of xargs with a long list of file names. xargs then splits this list into sublists and calls rm once for every sublist. This is more efficient than this functionally equivalent version:

find /path -type f -exec rm '{}' \;

I know that find has the "exec" flag. I am just quoting an illustrative example from another resource.


Solution

  • The following will only work if you do not have spaces in your input:

    xargs -L 1
    xargs --max-lines=1 # synonym for the -L option
    

    from the man page:

    -L max-lines
              Use at most max-lines nonblank input lines per command line.
              Trailing blanks cause an input line to be logically continued  on
              the next input line.  Implies -x.