linux shell腳本學(xué)習(xí)xargs命令使用詳解
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.
例如,下面的命令:
rm `find /path -type f`
如果path目錄下文件過多就會(huì)因?yàn)椤皡?shù)列表過長(zhǎng)”而報(bào)錯(cuò)無(wú)法執(zhí)行。但改用xargs以后,問題即獲解決。
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命令效率高的多。
find /path -type f -exec rm '{}' \;
xargs命令應(yīng)該緊跟在管道操作符之后,它以標(biāo)準(zhǔn)輸入作為主要的源數(shù)據(jù)流,并使用stdin并通過提供命令行參數(shù)來(lái)執(zhí)行其他命令,例如:
command | xargs
實(shí)例應(yīng)用1,將多行輸入轉(zhuǎn)換為單行輸出:
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)換為多行輸出:
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ù)來(lái)分隔參數(shù),如下:
amosli@amosli-pc:~/learn$ echo "splitXhiXamosliXsplit" | xargs -d "X" -n 1
split
hi
amosli
split
實(shí)例應(yīng)用3,讀取stdin,將格式化參數(shù)傳遞給命令
#定義一個(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ù)嵌入到固定的命令行中?如下所示:
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的解決方案:
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來(lái)分隔輸出,如下所示,否則可能會(huì)誤刪文件
amosli@amosli-pc:~/learn$ find . -type f -name "*test*.txt" -print0 | xargs -0 rm -f
其他:
cat file | ( while read arg; do cat $arg; done )
cat file | xargs -I {} cat {}
相關(guān)文章
shell腳本5種執(zhí)行方式及腳本不同的執(zhí)行方法和區(qū)別詳解
我們都知道bash?shell?腳本的方法有多種,下面這篇文章主要給大家介紹了關(guān)于shell腳本5種執(zhí)行方式及腳本不同的執(zhí)行方法和區(qū)別的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
shell腳本實(shí)現(xiàn)批量測(cè)試局域網(wǎng)主機(jī)是否在線
這篇文章主要介紹了shell腳本實(shí)現(xiàn)批量測(cè)試局域網(wǎng)主機(jī)是否在線,效果是:主機(jī)在線顯示綠色,主機(jī)離線顯示紅色結(jié)果,需要的朋友可以參考下2014-12-12
Linux環(huán)境下tcpdump網(wǎng)絡(luò)協(xié)議抓包與解析
這篇文章主要為大家介紹了Linux環(huán)境下tcpdump網(wǎng)絡(luò)協(xié)議抓包與解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Linux中根據(jù)一個(gè)單詞快速鎖定到日志的操作命令
這篇文章給大家介紹了Linux中如何根據(jù)一個(gè)單詞快速鎖定到日志,文中通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01
linux數(shù)據(jù)庫(kù)備份并通過ftp上傳腳本分享
這篇文章主要介紹了linux數(shù)據(jù)庫(kù)備份并通過ftp上傳腳本,腳本還可以備份網(wǎng)站,需要的朋友可以參考下2014-03-03
Linux忘記root密碼進(jìn)入單用戶模式切換運(yùn)行級(jí)別切換用戶
這篇文章主要介紹了Linux忘記root密碼進(jìn)入單用戶模式切換運(yùn)行級(jí)別切換用戶,需要的朋友可以參考下2019-08-08
Linux?ps命令詳解及Linux查看進(jìn)程的操作方法
這篇文章主要介紹了Linux?ps命令詳解,Linux查看進(jìn)程的操作方法,ps命令常用的方式有三種,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
linux設(shè)置定時(shí)任務(wù)的方法步驟
這篇文章主要介紹了linux設(shè)置定時(shí)任務(wù)的方法步驟,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05

