Xargs用法詳解

1. 簡介linux

  之因此能用到這個命令,關鍵是因爲不少命令不支持|管道來傳遞參數,而平常工做中有有這個必要,因此就有了xargs命令,例如:post

find /sbin -perm +700 |ls -l       這個命令是錯誤的spa

find /sbin -perm +700 |xargs ls -l   這樣纔是正確的.net

xargs 能夠讀入 stdin 的資料,而且以空白字元或斷行字元做爲分辨,將 stdin 的資料分隔成爲 arguments 。 由於是以空白字元做爲分隔,因此,若是有一些檔名或者是其餘意義的名詞內含有空白字元的時候, xargs 可能就會誤判了~他的用法其實也還滿簡單的!就來看一看先!命令行

2. 選項解釋orm

-0 sdtin含有特殊字元時候,將其當成通常字符,想/'空格等進程

例如:root@localhost :~/test#echo "//"|xargs  echo get

      root@localhost :~/test#echo "//"|xargs -0 echo input

       /it

-a file 從文件中讀入做爲sdtin,(看例一)

-e flag ,注意有的時候可能會是-Eflag必須是一個以空格分隔的標誌,當xargs分析到含有flag這個標誌的時候就中止。(例二)

-p 當每次執行一個argument的時候詢問一次用戶。(例三)

-n num 後面加次數,表示命令在執行的時候一次用的argument的個數,默認是用全部的。(例四)

-t 表示先打印命令,而後再執行。(例五)

-i 或者是-I,這得看linux支持了,將xargs的每項名稱,通常是一行一行賦值給{},能夠用{}代替。(例六)

-r no-run-if-empty xargs的輸入爲空的時候則中止xargs,不用再去執行了。(例七)

-s num 命令行的最好字符數,指的是xargs後面那個命令的最大命令行字符數。(例八)

 

-L  num Use at most max-lines nonblank input lines per command line.-s是含有空格的。

-l  同-L

-d delim 分隔符,默認的xargs分隔符是回車,argument的分隔符是空格,這裏修改的是xargs的分隔符(例九)

-x exit的意思,主要是配合-s使用。

-修改最大的進程數,默認是1,爲0時候爲as many as it can ,這個例子我沒有想到,應該平時都用不到的吧。

3. 應用舉例

例一:

root@localhost :~/test#cat test 

#!/bin/sh

echo "hello world/n"

root@localhost :~/test#xargs -a test echo

#!/bin/sh echo hello world/n

root@localhost :~/test#

例二:

root@localhost:~/test#cat txt

/bin tao shou kun

root@localhost:~/test#cat txt|xargs -E 'shou' echo

/bin tao

root@localhost:~/test#

例三:

root@localhost:~/test#cat txt|xargs -p echo

echo /bin tao shou kun ff ?...y

/bin tao shou kun ff

例四:

root@localhost:~/test#cat txt|xargs -n1 echo

/bin

tao

shou

kun

root@localhost:~/test3#cat txt|xargs  echo

/bin tao shou kun

例五:

root@localhost:~/test#cat txt|xargs -t echo

echo /bin tao shou kun 

/bin tao shou kun

例六:

$ ls | xargs -t -i mv {} {}.bak

例七:

root@localhost:~/test#echo ""|xargs -t mv

mv 

mv: missing file operand

Try `mv --help' for more information.

root@localhost:~/test#echo ""|xargs -t -r  mv

root@localhost:~/test#

(直接退出)

例八:

root@localhost:~/test#cat test |xargs -i -x  -s 14 echo "{}"

exp1

exp5

file

xargs: argument line too long

linux-2

root@localhost:~/test#

例九:

root@localhost:~/test#cat txt |xargs -i -p echo {}

echo /bin tao shou kun ?...y

root@localhost:~/test#cat txt |xargs -i -p -d " " echo {}

echo /bin ?...y

echo tao ?.../bin

y

echo shou ?...tao

再如:

root@localhost:~/test#cat test |xargs -i -p -d " " echo {}

echo exp1

exp5

file

linux-2

ngis_post

tao

test

txt

xen-3

 ?...y

root@localhost:~/test#cat test |xargs -i -p echo {}

echo exp1 ?...y

echo exp5 ?...exp1

y

echo file ?...exp5

y

相關文章
相關標籤/搜索