Make xargs execute the command once for each line of input

后端 未结 13 533
栀梦
栀梦 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:16

    Another alternative...

    find /path -type f | while read ln; do echo "processing $ln"; done
    
    0 讨论(0)
  • 2020-11-29 15:17

    It seems I don't have enough reputation to add a comment to Tobia's answer above, so I am adding this "answer" to help those of us wanting to experiment with xargs the same way on the Windows platforms.

    Here is a windows batch file that does the same thing as Tobia's quickly coded "show" script:

    @echo off
    REM
    REM  cool trick of using "set" to echo without new line
    REM  (from:  http://www.psteiner.com/2012/05/windows-batch-echo-without-new-line.html)
    REM
    if "%~1" == "" (
        exit /b
    )
    
    <nul set /p=Args:  "%~1"
    shift
    
    :start
    if not "%~1" == "" (
        <nul set /p=, "%~1"
        shift
        goto start
    )
    echo.
    
    0 讨论(0)
  • 2020-11-29 15:19

    execute ant task clean-all on every build.xml on current or sub-folder.

    find . -name 'build.xml' -exec ant -f {} clean-all \;
    
    0 讨论(0)
  • 2020-11-29 15:23

    In your example, the point of piping the output of find to xargs is that the standard behavior of find's -exec option is to execute the command once for each found file. If you're using find, and you want its standard behavior, then the answer is simple - don't use xargs to begin with.

    0 讨论(0)
  • 2020-11-29 15:25

    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.
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题