Xargs用法詳解

來源:互聯網
上載者:User

標籤:

 

1. 簡介

  之所以能用到這個命令,關鍵是由於很多命令不支援|管道來傳遞參數,而日常工作中有有這個必要,所以就有了xargs命令,例如:

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

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

xargs 可以讀入 stdin 的資料,並且以空白字元或斷行字元作為分辨,將 stdin 的資料分隔成為 arguments 。 因為是以空白字元作為分隔,所以,如果有一些檔名或者是其他意義的名詞內含有空白字元的時候, xargs 可能就會誤判了~他的用法其實也還滿簡單的!就來看一看先!

2. 選項解釋

-0 當sdtin含有特殊字元時候,將其當成一般字元,想/‘空格等

例如:[email protected]:~/test#echo "//"|xargs  echo 

      [email protected]:~/test#echo "//"|xargs -0 echo 

       /

-a file 從檔案中讀入作為sdtin,(看例一)

-e flag ,注意有的時候可能會是-E,flag必須是一個以空格分隔的標誌,當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使用。

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

3. 應用舉例

例一:

[email protected]:~/test#cat test #!/bin/shecho "hello world/n"[email protected]:~/test#xargs -a test echo#!/bin/sh echo hello world/n[email protected]:~/test#

 

 

例二:

[email protected]:~/test#cat txt/bin tao shou kun[email protected]:~/test#cat txt|xargs -E ‘shou‘ echo/bin tao[email protected]:~/test#

 

 

例三:

[email protected]:~/test#cat txt|xargs -p echoecho /bin tao shou kun ff ?...y/bin tao shou kun ff

 

 

例四:

[email protected]:~/test#cat txt|xargs -n1 echo/bintaoshoukun[email protected]:~/test3#cat txt|xargs  echo/bin tao shou kun

 

 

例五:

[email protected]:~/test#cat txt|xargs -t echoecho /bin tao shou kun /bin tao shou kun

 

 

例六:

 

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

 

 

 

例七:

[email protected]:~/test#echo ""|xargs -t mvmv mv: missing file operandTry `mv --help‘ for more information.[email protected]:~/test#echo ""|xargs -t -r  mv[email protected]:~/test#(直接退出)

 

例八:

[email protected]:~/test#cat test |xargs -i -x  -s 14 echo "{}"exp1exp5filexargs: argument line too longlinux-2[email protected]:~/test#

 

 

例九:

[email protected]:~/test#cat txt |xargs -i -p echo {}echo /bin tao shou kun ?...y[email protected]:~/test#cat txt |xargs -i -p -d " " echo {}echo /bin ?...yecho tao ?.../binyecho shou ?...tao再如:[email protected]:~/test#cat test |xargs -i -p -d " " echo {}echo exp1exp5filelinux-2ngis_posttaotesttxtxen-3 ?...y[email protected]:~/test#cat test |xargs -i -p echo {}echo exp1 ?...yecho exp5 ?...exp1yecho file ?...exp5y

 

Xargs用法詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.