最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

linux shell腳本學(xué)習(xí)xargs命令使用詳解

 更新時間:2013年12月23日 09:10:21   投稿:zxhpj  
xargs是一條Unix和類Unix操作系統(tǒng)的常用命令。它的作用是將參數(shù)列表轉(zhuǎn)換成小塊分段傳遞給其他命令,以避免參數(shù)列表過長的問題

xargs是給命令傳遞參數(shù)的一個過濾器,也是組合多個命令的一個工具。它把一個數(shù)據(jù)流分割為一些足夠小的塊,以方便過濾器和命令進行處理。通常情況下,xargs從管道或者stdin中讀取數(shù)據(jù),但是它也能夠從文件的輸出中讀取數(shù)據(jù)。xargs的默認命令是echo,這意味著通過管道傳遞給xargs的輸入將會包含換行和空白,不過通過xargs的處理,換行和空白將被空格取代。

xargs 是一個強有力的命令,它能夠捕獲一個命令的輸出,然后傳遞給另外一個命令,下面是一些如何有效使用xargs 的實用例子。
1. 當你嘗試用rm 刪除太多的文件,你可能得到一個錯誤信息:/bin/rm Argument list too long. 用xargs 去避免這個問題

find ~ -name ‘*.log' -print0 | xargs -0 rm -f

2. 獲得/etc/ 下所有*.conf 結(jié)尾的文件列表,有幾種不同的方法能得到相同的結(jié)果,下面的例子僅僅是示范怎么實用xargs ,在這個例子中實用 xargs將find 命令的輸出傳遞給ls -l

# find /etc -name "*.conf" | xargs ls –l

3. 假如你有一個文件包含了很多你希望下載的URL, 你能夠使用xargs 下載所有鏈接

# cat url-list.txt | xargs wget –c

4. 查找所有的jpg 文件,并且壓縮它

# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

5. 拷貝所有的圖片文件到一個外部的硬盤驅(qū)動

# 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目錄下文件過多就會因為“參數(shù)列表過長”而報錯無法執(zhí)行。但改用xargs以后,問題即獲解決。

復(fù)制代碼 代碼如下:

find /path -type f -print0 | xargs -0 rm

本例中xargs將find產(chǎn)生的長串文件列表拆散成多個子串,然后對每個子串調(diào)用rm。-print0表示輸出以null分隔(-print使用換行);-0表示輸入以null分隔。這樣要比如下使用find命令效率高的多。

復(fù)制代碼 代碼如下:

find /path -type f -exec rm '{}' \;

xargs命令應(yīng)該緊跟在管道操作符之后,它以標準輸入作為主要的源數(shù)據(jù)流,并使用stdin并通過提供命令行參數(shù)來執(zhí)行其他命令,例如:

復(fù)制代碼 代碼如下:

command | xargs

實例應(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

實例應(yīng)用2,將單行輸入轉(zhuǎn)換為多行輸出:

復(fù)制代碼 代碼如下:

amosli@amosli-pc:~/learn$ cat example.txt | xargs -n 2
1 2
3 4
5 6
7 8

空格是默認的定界符,-n 表示每行顯示幾個參數(shù)

還可以使用-d參數(shù)來分隔參數(shù),如下:

復(fù)制代碼 代碼如下:

amosli@amosli-pc:~/learn$ echo "splitXhiXamosliXsplit" | xargs -d "X" -n 1
split
hi
amosli
split

實例應(yīng)用3,讀取stdin,將格式化參數(shù)傳遞給命令

復(fù)制代碼 代碼如下:

#定義一個echo命令每次在輸出參數(shù)后都加上#
amosli@amosli-pc:~/learn$ cat cecho.sh
echo $*'#'

#需求1:輸出多個參數(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#

#針對需求1、2,使用xargs代替,先用vi建一個新文件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 {}批定了替換字符串,字符串{}會被從stdin讀取到的參數(shù)所替換,使用-I時,能循環(huán)按要求替換相應(yīng)的參數(shù)

實例應(yīng)用4,結(jié)合find使用xargs

前面已經(jīng)舉過例子,這里要注意的是文件名稱定界符要以字符null來分隔輸出,如下所示,否則可能會誤刪文件

復(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 {}

您可能感興趣的文章:

相關(guān)文章

最新評論

丹阳市| 丰顺县| 平昌县| 灵璧县| 张家川| 宜宾市| 清涧县| 新晃| 无为县| 边坝县| 连州市| 方山县| 来宾市| 岐山县| 东丽区| 河津市| 枣强县| 行唐县| 利津县| 广德县| 宣城市| 澜沧| 图木舒克市| 涿州市| 彭阳县| 和政县| 靖西县| 大新县| 石景山区| 赞皇县| 年辖:市辖区| 安陆市| 宿松县| 长子县| 玉林市| 麻城市| 金平| 汶上县| 左云县| 广水市| 高清|