亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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ò)無(wú)法執(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ù)來(lái)執(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ù)來(lái)分隔參數(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來(lái)分隔輸出,如下所示,否則可能會(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 {}

相關(guān)文章

  • Shell常用操作符總結(jié)

    Shell常用操作符總結(jié)

    這篇文章主要介紹了Shell常用操作符總結(jié),本文講解了算術(shù)操作 符、關(guān)系操作符、測(cè)試操作符等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • linux shell之pushd、popd和dirs的使用講解

    linux shell之pushd、popd和dirs的使用講解

    今天小編就為大家分享一篇關(guān)于linux shell之pushd、popd和dirs的使用講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-04-04
  • shell SNAT與DNAT的使用與區(qū)別

    shell SNAT與DNAT的使用與區(qū)別

    本文主要介紹了shell SNAT與DNAT的使用與區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 常用Shell命令集合和使用技巧(推薦)

    常用Shell命令集合和使用技巧(推薦)

    這篇文章主要介紹了最常用Shell命令集合和使用技巧,本文分場(chǎng)景通過實(shí)例講解給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Shell腳本實(shí)現(xiàn)從文件夾中遞歸復(fù)制文件

    Shell腳本實(shí)現(xiàn)從文件夾中遞歸復(fù)制文件

    這篇文章主要介紹了Shell腳本實(shí)現(xiàn)從文件夾中遞歸復(fù)制文件,本文腳本實(shí)現(xiàn)從十層左右的文件夾中復(fù)制所有文件到一目錄中,需要的朋友可以參考下
    2015-02-02
  • Linux 按時(shí)間批量刪除文件命令(刪除N天前文件)

    Linux 按時(shí)間批量刪除文件命令(刪除N天前文件)

    這篇文章主要介紹了Linux 按時(shí)間批量刪除文件的命令寫法(刪除N天前文件),需要的朋友可以參考下
    2017-05-05
  • python實(shí)現(xiàn)Linux異步epoll代碼

    python實(shí)現(xiàn)Linux異步epoll代碼

    本文提供了python實(shí)現(xiàn)Linux異步epoll的代碼,供大家參考使用,希望對(duì)你有幫助
    2013-11-11
  • Linux?signal()函數(shù)的使用學(xué)習(xí)

    Linux?signal()函數(shù)的使用學(xué)習(xí)

    這篇文章主要為大家介紹了Linux?signal()函數(shù)的使用學(xué)習(xí)及示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • Linux shell命令幫助格式詳解

    Linux shell命令幫助格式詳解

    最近看了一個(gè)教程,關(guān)于Linux命令的,本來(lái)以為當(dāng)是復(fù)習(xí)隨便看看的,結(jié)果看了不禁汗顏,這個(gè)真挺有學(xué)問的,很多東西都是我還不知道的,故此做總結(jié)。下面這篇文章主要介紹了Linux shell命令幫助格式的相關(guān)資料,需要的朋友可以參考借鑒。
    2017-01-01
  • Shell命令解釋器分類示例詳解

    Shell命令解釋器分類示例詳解

    Shell是負(fù)責(zé)User與Linux OS之間溝通的橋梁,Shell為用戶提供了一個(gè)操作界面,User在這個(gè)界面輸入指令,其實(shí)就是通過Shell向Linux Kernel傳遞過去,這也就是為什么Shell也叫解釋器的原因,這篇文章主要給大家介紹了關(guān)于Shell命令解釋器分類的相關(guān)資料,需要的朋友可以參考下
    2023-05-05

最新評(píng)論