Make xargs execute the command once for each line of input

后端 未结 13 625
栀梦
栀梦 2020-11-29 15:09

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 multip

13条回答
  •  情深已故
    2020-11-29 15:25

    If you want to run the command for every line (i.e. result) coming from find, then what do you need the xargs for?

    Try:

    find path -type f -exec your-command {} \;

    where the literal {} gets substituted by the filename and the literal \; is needed for find to know that the custom command ends there.

    EDIT:

    (after the edit of your question clarifying that you know about -exec)

    From man xargs:

    -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.

    Note that filenames ending in blanks would cause you trouble if you use xargs:

    $ mkdir /tmp/bax; cd /tmp/bax
    $ touch a\  b c\  c
    $ find . -type f -print | xargs -L1 wc -l
    0 ./c
    0 ./c
    0 total
    0 ./b
    wc: ./a: No such file or directory
    

    So if you don't care about the -exec option, you better use -print0 and -0:

    $ find . -type f -print0 | xargs -0L1 wc -l
    0 ./c
    0 ./c
    0 ./b
    0 ./a
    

提交回复
热议问题