xargs是給命令傳遞參數(shù)的一個(gè)過濾器,也是組合多個(gè)命令的一個(gè)工具。它把一個(gè)數(shù)據(jù)流分割為一些足夠小的塊,以方便過濾器和命令進(jìn)行處理。通常情況下,xargs從管道或者stdin中讀取數(shù)據(jù),但是它也能夠從文件的輸出中讀取數(shù)據(jù)。xargs的默認(rèn)命令是echo,這意味著通過管道傳遞給xargs的輸入將會(huì)包含換行和空白,不過通過xargs的處理,換行和空白將被空格取代。
xargs 是一個(gè)強(qiáng)有力的命令,它能夠捕獲一個(gè)命令的輸出,然后傳遞給另外一個(gè)命令,下面是一些如何有效使用xargs 的實(shí)用例子。
1. 當(dāng)你嘗試用rm 刪除太多的文件,你可能得到一個(gè)錯(cuò)誤信息:/bin/rm Argument list too long. 用xargs 去避免這個(gè)問題
find ~ -name ‘*.log' -print0 | xargs -0 rm -f
2. 獲得/etc/ 下所有*.conf 結(jié)尾的文件列表,有幾種不同的方法能得到相同的結(jié)果,下面的例子僅僅是示范怎么實(shí)用xargs ,在這個(gè)例子中實(shí)用 xargs將find 命令的輸出傳遞給ls -l
# find /etc -name "*.conf" | xargs ls –l
3. 假如你有一個(gè)文件包含了很多你希望下載的URL, 你能夠使用xargs 下載所有鏈接
# cat url-list.txt | xargs wget –c
4. 查找所有的jpg 文件,并且壓縮它
# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
5. 拷貝所有的圖片文件到一個(gè)外部的硬盤驅(qū)動(dòng)
# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory
EXAMPLES
find /tmp -name core -type f -print | xargs /bin/rm -f
Find files named core in or below the directory /tmp and delete them. Note that this will work incorrectly if there are any filenames containing newlines or spaces.
find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
Find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names containing spaces or newlines are correctly handled.
find /tmp -depth -name core -type f -delete
Find files named core in or below the directory /tmp and delete them, but more efficiently than in the previous example (because we avoid the need to use fork(2) and exec(2) to launch rm and we don't need the extra xargs process).
cut -d: -f1 /etc/passwd | sort | xargs echo
Generates a compact listing of all the users on the system.
xargs sh -c 'emacs "$@" /dev/tty' emacs
Launches the minimum number of copies of Emacs needed, one after the other, to edit the files listed on xargs' standard input. This example achieves the same effect as BSD's -o option, but in a more flexible and portable way.
例如,下面的命令:
復(fù)制代碼 代碼如下:
rm `find /path -type f`
如果path目錄下文件過多就會(huì)因?yàn)椤皡?shù)列表過長(zhǎng)”而報(bào)錯(cuò)無法執(zhí)行。但改用xargs以后,問題即獲解決。
復(fù)制代碼 代碼如下:
find /path -type f -print0 | xargs -0 rm
本例中xargs將find產(chǎn)生的長(zhǎng)串文件列表拆散成多個(gè)子串,然后對(duì)每個(gè)子串調(diào)用rm。-print0表示輸出以null分隔(-print使用換行);-0表示輸入以null分隔。這樣要比如下使用find命令效率高的多。
復(fù)制代碼 代碼如下:
find /path -type f -exec rm '{}' \;
xargs命令應(yīng)該緊跟在管道操作符之后,它以標(biāo)準(zhǔn)輸入作為主要的源數(shù)據(jù)流,并使用stdin并通過提供命令行參數(shù)來執(zhí)行其他命令,例如:
復(fù)制代碼 代碼如下:
command | xargs
實(shí)例應(yīng)用1,將多行輸入轉(zhuǎn)換為單行輸出:
復(fù)制代碼 代碼如下:
amosli@amosli-pc:~/learn$ cat example.txt
1 2 3 4 5
6 7
8
amosli@amosli-pc:~/learn$ cat example.txt | xargs
1 2 3 4 5 6 7 8
實(shí)例應(yīng)用2,將單行輸入轉(zhuǎn)換為多行輸出:
復(fù)制代碼 代碼如下:
amosli@amosli-pc:~/learn$ cat example.txt | xargs -n 2
1 2
3 4
5 6
7 8
空格是默認(rèn)的定界符,-n 表示每行顯示幾個(gè)參數(shù)
還可以使用-d參數(shù)來分隔參數(shù),如下:
復(fù)制代碼 代碼如下:
amosli@amosli-pc:~/learn$ echo "splitXhiXamosliXsplit" | xargs -d "X" -n 1
split
hi
amosli
split
實(shí)例應(yīng)用3,讀取stdin,將格式化參數(shù)傳遞給命令
復(fù)制代碼 代碼如下:
#定義一個(gè)echo命令每次在輸出參數(shù)后都加上#
amosli@amosli-pc:~/learn$ cat cecho.sh
echo $*'#'
#需求1:輸出多個(gè)參數(shù)
amosli@amosli-pc:~/learn$ sh cecho.sh arg1
arg1#
amosli@amosli-pc:~/learn$ sh cecho.sh arg2
arg2#
amosli@amosli-pc:~/learn$ sh cecho.sh arg3
arg3#
#需求2:一次性提供所有的命令參數(shù)
amosli@amosli-pc:~/learn$ sh cecho.sh arg1 arg2 arg3
arg1 arg1 arg2 arg3#
#針對(duì)需求1、2,使用xargs代替,先用vi建一個(gè)新文件args.txt,如下:
amosli@amosli-pc:~/learn$ cat args.txt
arg1
arg2
arg3
#批量輸出參數(shù):
amosli@amosli-pc:~/learn$ cat args.txt | xargs -n 1
arg1
arg2
arg3
amosli@amosli-pc:~/learn$ cat args.txt | xargs -n 2 sh cecho.sh
arg1 arg2#
arg3#
#一次性輸出所有參數(shù):
amosli@amosli-pc:~/learn$ cat args.txt | xargs sh cecho.sh ;
arg1 arg2 arg3#
需求3,如何將參數(shù)嵌入到固定的命令行中?如下所示:
復(fù)制代碼 代碼如下:
amosli@amosli-pc:~/learn$ sh cecho.sh -p args1 -1
-p args1 -1#
amosli@amosli-pc:~/learn$ sh cecho.sh -p args2 -1
-p args2 -1#
amosli@amosli-pc:~/learn$ sh cecho.sh -p args3 -1
-p args3 -1#
使用xargs的解決方案:
復(fù)制代碼 代碼如下:
amosli@amosli-pc:~/learn$ cat args.txt | xargs -I {} sh cecho.sh -p {} -1
-p arg1 -1#
-p arg2 -1#
-p arg3 -1#
#-I {}批定了替換字符串,字符串{}會(huì)被從stdin讀取到的參數(shù)所替換,使用-I時(shí),能循環(huán)按要求替換相應(yīng)的參數(shù)
實(shí)例應(yīng)用4,結(jié)合find使用xargs
前面已經(jīng)舉過例子,這里要注意的是文件名稱定界符要以字符null來分隔輸出,如下所示,否則可能會(huì)誤刪文件
復(fù)制代碼 代碼如下:
amosli@amosli-pc:~/learn$ find . -type f -name "*test*.txt" -print0 | xargs -0 rm -f
其他:
復(fù)制代碼 代碼如下:
cat file | ( while read arg; do cat $arg; done )
cat file | xargs -I {} cat {}
您可能感興趣的文章:- Linux xargs命令的使用
- Linux基礎(chǔ)之xargs命令的入門實(shí)例
- Linux 下xargs命令詳解及xargs與管道的區(qū)別
- Linux xargs命令詳細(xì)介紹
- 在Linux上使用xargs命令的詳細(xì)教程