如何爲每一個給定的輸入行使xargs執行一次命令? 它的默認行爲是將行塊化並執行一次命令,將多行傳遞給每一個實例。 less
來自http://en.wikipedia.org/wiki/Xargs : spa
find / path -type f -print0 | xargs -0 rm 命令行
在此示例中,查找使用長文件名列表輸入xargs。 而後xargs將此列表拆分爲子列表,併爲每一個子列表調用rm一次。 這比這個功能相同的版本更有效: code
find / path -type f -exec rm'{}'\\; ip
我知道find有「exec」標誌。 我只是引用另外一個資源的說明性示例。 ci
另外一種選擇...... 資源
find /path -type f | while read ln; do echo "processing $ln"; done
只有在輸入中沒有空格時,如下內容纔有效: get
xargs -L 1 xargs --max-lines=1 # synonym for the -L option
從手冊頁: input
-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.
您能夠分別使用--max-lines或--max-args標誌限制行數或參數(若是每一個參數之間有空格)。 it
-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. --max-lines[=max-lines], -l[max-lines] Synonym for the -L option. Unlike -L, the max-lines argument is optional. If max-args is not specified, it defaults to one. The -l option is deprecated since the POSIX standard specifies -L instead. --max-args=max-args, -n max-args Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.
在您的示例中,將find的輸出傳遞給xargs的點是find的-exec選項的標準行爲是爲每一個找到的文件執行一次命令。 若是你正在使用find,而且你想要它的標準行爲,那麼答案很簡單 - 不要使用xargs開頭。
若是你想爲來自find
每一行(即結果)運行命令,那麼你須要xargs
用於什麼?
嘗試:
find
path -type f -exec
your-command {} \\;
文字{}
被文件名和文字\\;
取代\\;
是須要find
要知道,自定義命令到此爲止。
(在您的問題編輯以後,澄清您瞭解-exec
)
來自man xargs
:
-L max-lines
每一個命令行最多使用max-lines nonblank輸入行。 尾隨空白致使輸入行在下一個輸入行上邏輯上繼續。 意味着-x。
請注意,若是您使用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
所以,若是您不關心-exec
選項,最好使用-print0
和-0
:
$ find . -type f -print0 | xargs -0L1 wc -l 0 ./c 0 ./c 0 ./b 0 ./a