Xargs用法詳解
1. 簡介
之所以能用到這個命令,關鍵是由於很多命令不支持|管道來傳遞參數,而日常工作中有有這個必要,所以就有了xargs命令,例如:
find /sbin -perm +700 |ls -l 這個命令是錯誤的
find /sbin -perm +700 |xargs ls -l 這樣才是正確的
xargs 可以讀入 stdin 的資料,並且以空白字元或斷行字元作為分辨,將 stdin 的資料分隔成為 arguments 。 因為是以空白字元作為分隔,所以,如果有一些檔名或者是其他意義的名詞內含有空白字元的時候, xargs 可能就會誤判了~他的用法其實也還滿簡單的!就來看一看先!
2. 選項解釋
淘寶、天貓、聚划算,商家短期打造爆款,秒殺、咚咚搶、優惠券,應有盡有,只有您想不到,沒有做不到,開心購呀購(網址:www.kxgba.com,微信小程序搜索:開心購呀購),您的購物好助手!
-0 當sdtin含有特殊字元時候,將其當成一般字符,想/'空格等
例如:root@localhost:~/test#echo "//"|xargs echo
root@localhost:~/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. 應用舉例
例一:
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
args的-i參數
Replace occurre nces of replace-str in the initial-arguments with names read from standard input. A lso, unquoted blanks do not terminate
input items; instead the separator is the newline character. Implies -x and -L 1.
--replace[=replace-str], -i[replace-str]
This option is a synonym for -Ireplace-str if replace-str is specified, and for -I{} otherwise. This option is depre cated; use -I instead.
1kk. zip 3kk.zip 5kk.zip b.rar d.rar f.rar h.rar j.rar mini.txt ni.txt
2kk.zip 4kk.zip a.rar c.rar e.rar g.rar i.rar k.rar nii.txt
[root@test05 ab]# find . -type f -name "*.txt" | xargs -i cp {} /tmp/k/
[root@test05 ab]# ls ../k/
mini.txt nii.txt ni.txt
[root@test05 ab]#
[root@test05 ab]# find . -type f -name "*.txt" | xargs -I {} cp {} /tmp/n/
[root@test05 ab]# ls ../n/
mini.txt nii.txt ni.txt
加 -I 參數 需要事先指定替換字符