1. 簡介
之所以能用到這個命令,關鍵是由於很多命令不支持|管道來傳遞參數,而日常工作中有有這個必要,所以就有了xargs命令,例如:
find /sbin -perm +700 |ls -l 這個命令是錯誤的
find /sbin -perm +700 |xargs ls -l 這樣才是正確的
xargs 可以讀入 stdin 的資料,並且以空白字元或斷行字元作為分辨,將 stdin 的資料分隔成為 arguments 。 因為是以空白字元作為分隔,所以,如果有一些檔名或者是其他意義的名詞內含有空白字元的時候, xargs 可能就會誤判了~他的用法其實也還滿簡單的!就來看一看先!
2. 選項解釋
-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 ,這個例子我沒有想到,應該平時都用不到的吧
例一:
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
cat example.txt
1 2 3 4 5
6 7
8
cat example.txt | xargs
1 2 3 4 5 6 7 8
cat example.txt | xargs -n 2
1 2
3 4
5 6
7 8
空格是默認的定界符,-n 表示每行顯示幾個參數
還可以使用-d參數來分隔參數,如下:
echo "splitXhiXamosliXsplit" | xargs -d "X" -n 1
split
hi
amosli
split
在使用 find命令的-exec選項處理匹配到的文件時, find命令將所有匹配到的文件一起傳遞給exec執行。但有些系統對能夠傳遞給exec的命令長度有限制,這樣在find命令運行幾分鍾之后,就會出現溢出錯誤。錯誤信息通常是“參數列太長”或“參數列溢出”。這就是xargs命令的用處所在,特別是與find命令一起使用。
find命令把匹配到的文件傳遞給xargs命令,而xargs命令每次只獲取一部分文件而不是全部,不像-exec選項那樣。這樣它可以先處理最先獲取的一部分文件,然后是下一批,並如此繼續下去。
在有些系統中,使用-exec選項會為處理每一個匹配到的文件而發起一個相應的進程,並非將匹配到的文件全部作為參數一次執行;這樣在有些情況下就會出現進程過多,系統性能下降的問題,因而效率不高; 而使用xargs命令則只有一個進程。另外,在使用xargs命令時,究竟是一次獲取所有的參數,還是分批取得參數,以及每一次獲取參數的數目都會根據該命令的選項及系統內核中相應的可調參數來確定。
使用實例:
實例1: 查找系統中的每一個普通文件,然后使用xargs命令來測試它們分別屬於哪類文件
命令:
find . -type f -print | xargs file
輸出:
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -type f -print | xargs file
./log2014.log: empty
./log2013.log: empty
./log2012.log: ASCII text
[root@localhost test]#
實例2:在整個系統中查找內存信息轉儲文件(core dump) ,然后把結果保存到/tmp/core.log 文件中
命令:
find / -name "core" -print | xargs echo "" >/tmp/core.log
輸出:
[root@localhost test]# find / -name "core" -print | xargs echo "" >/tmp/core.log
[root@localhost test]# cd /tmp
[root@localhost tmp]# ll
總計 16
-rw-r--r-- 1 root root 1524 11-12 22:29 core.log
drwx------ 2 root root 4096 11-12 22:24 ssh-TzcZDx1766
drwx------ 2 root root 4096 11-12 22:28 ssh-ykiRPk1815
drwx------ 2 root root 4096 11-03 07:11 vmware-root
實例3:在當前目錄下查找所有用戶具有讀、寫和執行權限的文件,並收回相應的寫權限
命令:
find . -perm -7 -print | xargs chmod o-w
輸出:
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -perm -7 -print | xargs chmod o-w
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 19:32 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
說明:
執行命令后,文件夾scf、test3和test4的權限都發生改變
實例4:用grep命令在所有的普通文件中搜索hostname這個詞
命令:
find . -type f -print | xargs grep "hostname"
輸出:
[root@localhost test]# find . -type f -print | xargs grep "hostname"
./log2013.log:hostnamebaidu=baidu.com
./log2013.log:hostnamesina=sina.com
./log2013.log:hostnames=true[root@localhost test]#
實例5:用grep命令在當前目錄下的所有普通文件中搜索hostnames這個詞
命令:
find . -name \* -type f -print | xargs grep "hostnames"
輸出:
[root@peida test]# find . -name \* -type f -print | xargs grep "hostnames"
./log2013.log:hostnamesina=sina.com
./log2013.log:hostnames=true[root@localhost test]#
說明:
注意,在上面的例子中, \用來取消find命令中的*在shell中的特殊含義。
實例6:使用xargs執行mv
命令:
find . -name "*.log" | xargs -i mv {} test4
輸出:
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:54 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# cd test4/
[root@localhost test4]# ll
總計 0[root@localhost test4]# cd ..
[root@localhost test]# find . -name "*.log" | xargs -i mv {} test4
[root@localhost test]# ll
總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test4/
[root@localhost test4]# ll
總計 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:54 log2014.log
[root@localhost test4]#
實例7:find后執行xargs提示xargs: argument line too long解決方法:
命令:
find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f
輸出:
[root@pd test4]# find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f
rm -f
[root@pdtest4]#
說明:
-l1是一次處理一個;-t是處理之前打印出命令
實例8:使用-i參數默認的前面輸出用{}代替,-I參數可以指定其他代替字符,如例子中的[]
命令:
輸出:
[root@localhost test]# ll
總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test4
[root@localhost test4]# find . -name "file" | xargs -I [] cp [] ..
[root@localhost test4]# ll
總計 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:54 log2014.log
[root@localhost test4]# cd ..
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]#
說明:
使用-i參數默認的前面輸出用{}代替,-I參數可以指定其他代替字符,如例子中的[]
實例9:xargs的-p參數的使用
命令:
find . -name "*.log" | xargs -p -i mv {} ..
輸出:
[root@localhost test3]# ll
總計 0
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
[root@localhost test3]# cd ..
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:06 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test3
[root@localhost test3]# find . -name "*.log" | xargs -p -i mv {} ..
mv ./log2015.log .. ?...y
[root@localhost test3]# ll
總計 0[root@localhost test3]# cd ..
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:08 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]#
說明:
-p參數會提示讓你確認是否執行后面的命令,y執行,n不執行。