1. 程式人生 > >Linux Shell指令碼程式設計--xargs命令詳解

Linux Shell指令碼程式設計--xargs命令詳解

xargs是給命令傳遞引數的一個過濾器,也是組合多個命令的一個工具。它把一個數據流分割為一些足夠小的塊,以方便過濾器和命令進行處理。通常情況下,xargs從管道或者stdin中讀取資料,但是它也能夠從檔案的輸出中讀取資料。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結尾的檔案列表,有幾種不同的方法能得到相同的結果,下面的例子僅僅是示範怎麼實用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.拷貝所有的圖片檔案到一個外部的硬碟驅動

# 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 newlinesare 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) andexec(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 "[email protected]" < /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 effectas BSD's -o option, but in a more flexible and portable way.

Xargs用法詳解

1.簡介

之所以能用到這個命令,關鍵是由於很多命令不支援|管道來傳遞引數,而日常工作中有有這個必要,所以就有了xargs命令,例如:

find/sbin-perm+700|ls-l這個命令是錯誤的

find/sbin-perm+700|xargsls-l這樣才是正確的

xargs可以讀入stdin的資料,並且以空白字元或斷行字元作為分辨,將stdin的資料分隔成為arguments。因為是以空白字元作為分隔,所以,如果有一些檔名或者是其他意義的名詞內含有空白字元的時候,xargs可能就會誤判了~他的用法其實也還滿簡單的!就來看一看先!

2.選項解釋

-0sdtin含有特殊字元時候,將其當成一般字元,想/'空格等

例如:[email protected]:~/test#echo"//"|xargsecho

[email protected]:~/test#echo"//"|xargs-0echo

/

-afile從檔案中讀入作為sdtin,(看例一)

-eflag,注意有的時候可能會是-Eflag必須是一個以空格分隔的標誌,當xargs分析到含有flag這個標誌的時候就停止。(例二)

-p當每次執行一個argument的時候詢問一次使用者。(例三)

-nnum後面加次數,表示命令在執行的時候一次用的argument的個數,預設是用所有的。(例四)

-t表示先列印命令,然後再執行。(例五)

-i或者是-I,這得看linux支援了,將xargs的每項名稱,一般是一行一行賦值給{},可以用{}代替。(例六)

-rno-run-if-emptyxargs的輸入為空的時候則停止xargs,不用再去執行了。(例七)

-snum命令列的最好字元數,指的是xargs後面那個命令的最大命令列字元數。(例八)

-LnumUseatmostmax-linesnonblankinputlinespercommandline.-s是含有空格的。

-l-L

-ddelim分隔符,預設的xargs分隔符是回車,argument的分隔符是空格,這裡修改的是xargs的分隔符(例九)

-xexit的意思,主要是配合-s使用。

-P修改最大的程序數,預設是1,為0時候為asmanyasitcan,這個例子我沒有想到,應該平時都用不到的吧。

3.應用舉例

例一:

[email protected]:~/test#cattest

#!/bin/sh

echo"helloworld/n"

[email protected]:~/test#xargs-atestecho

#!/bin/shechohelloworld/n

[email protected]:~/test#

例二:

[email protected]:~/test#cattxt

/bintaoshoukun

[email protected]:~/test#cattxt|xargs-E'shou'echo

/bintao

[email protected]:~/test#

例三:

[email protected]:~/test#cattxt|xargs-pecho

echo/bintaoshoukunff?...y

/bintaoshoukunff

例四:

[email protected]:~/test#cattxt|xargs-n1echo

/bin

tao

shou

kun

[email protected]:~/test3#cattxt|xargsecho

/bintaoshoukun

例五:

[email protected]:~/test#cattxt|xargs-techo

echo/bintaoshoukun

/bintaoshoukun

例六:

$ls|xargs-t-imv{}{}.bak

例七:

[email protected]:~/test#echo""|xargs-tmv

mv

mv:missingfileoperand

Try`mv--help'formoreinformation.

[email protected]:~/test#echo""|xargs-t-rmv

[email protected]:~/test#

(直接退出)

例八:

[email protected]:~/test#cattest|xargs-i-x-s14echo"{}"

exp1

exp5

file

xargs:argumentlinetoolong

linux-2

[email protected]:~/test#

例九:

[email protected]:~/test#cattxt|xargs-i-pecho{}

echo/bintaoshoukun?...y

[email protected]:~/test#cattxt|xargs-i-p-d""echo{}

echo/bin?...y

echotao?.../bin

y

echoshou?...tao

再如:

[email protected]:~/test#cattest|xargs-i-p-d""echo{}

echoexp1

exp5

file

linux-2

ngis_post

tao

test

txt

xen-3

?...y

[email protected]:~/test#cattest|xargs-i-pecho{}

echoexp1?...y

echoexp5?...exp1

y

echofile?...exp5

y


相關推薦

Linux Shell指令碼程式設計xargs命令

xargs是給命令傳遞引數的一個過濾器,也是組合多個命令的一個工具。它把一個數據流分割為一些足夠小的塊,以方便過濾器和命令進行處理。通常情況下,xargs從管道或者stdin中讀取資料,但是它也能夠從檔案的輸出中讀取資料。xargs的預設命令是echo,這意味著通過管道傳遞給xargs的輸入將會包含換行和空

Linux Shell指令碼程式設計awk命令

簡單使用: awk :對於檔案中一行行的獨處來執行操作 。 awk -F :'{print $1,$4}'   :使用‘:’來分割這一行,把這一行的第一第四個域打印出來 。 詳細介紹: AWK命令介紹 awk語言的最基本功能是在檔案或字串中基於指定規則瀏覽和抽取資訊,awk抽取資訊後,才能進行其他

Linux Shell指令碼程式設計cut命令

cut cut命令可以從一個文字檔案或者文字流中提取文字列。 cut語法 [[email protected] ~]# cut -d'分隔字元' -f fields <==用於有特定分隔字元 [[email protected] ~]# cut

Linux Shell指令碼程式設計scp命令

不同的Linux之間copy檔案常用有3種方法: 第一種就是ftp,也就是其中一臺Linux安裝ftp Server,這樣可以另外一臺使用ftp的client程式來進行檔案的copy。 第二種方法就是採用samba服務,類似Windows檔案copy 的方式來操作,比較簡潔方便。 第三種就是利用scp命令來

Linux Shell指令碼程式設計sed命令

簡介 sed 是一種線上編輯器,它一次處理一行內容。處理時,把當前處理的行儲存在臨時緩衝區中,稱為“模式空間”(pattern space),接著用sed命令處理緩衝區中的內容,處理完成後,把緩衝區的內容送往螢幕。接著處理下一行,這樣不斷重複,直到檔案末尾。檔案內容並沒有

Linux Shell指令碼程式設計函式返回值

Shell函式返回值,常用的兩種方式:return,echo 1) return 語句shell函式的返回值,可以和其他語言的返回值一樣,通過return語句返回。示例: #!/bin/sh function test() { echo "arg1 =

Linux Shell指令碼程式設計 --awk命令

簡單使用: awk :對於檔案中一行行的獨處來執行操作 。 awk -F :'{print $1,$4}'   :使用‘:’來分割這一行,把這一行的第一第四個域打印出來 。    

Linux-Shell指令碼程式設計基礎(1)

1. 我們一般在使用Linux系統的時候,都活接觸到shell指令碼的使用,例如我們經常在linux系統中使用的ls命令、cd命令等,都是衣蛾簡單而又基本的shell命令,在 linux系統中我們一般的使用如下的格式來進行shell指令碼的編寫: (1)格式 #!bin/bash e

從新手到系統管理員(四):Linux Shell指令碼程式設計之數學(Part I)

本文由 [茶話匯] – [Qing] 編譯自 [Avishek Kumar] 轉載請註明出處 這部分主要討論數學相關的shell指令碼程式設計。 加法運算 新建一個檔案“Addition.sh”,輸入下面的內容並賦予其可執行的許可權。 [code language=”bash”] #!/bin/b

Linux Shell指令碼程式設計提高(12)

實際上Shell是一個命令直譯器,它解釋由使用者輸入的命令並且把它們送到核心,不僅如此,Shell有自己的程式語言用於對命令的編輯,它允許使用者編寫由shell命令組成的程式.Shel程式語言具有普通程式語言的很多特點,比如它也有迴圈結構和分支控制結構等,用這種程式語言編寫的Shell程式與其他應用程式具有同

Linux shell指令碼程式設計-將一行中的某個部分去重

一,背景。 今天遇到了一個使用場景,獲取檔案中每一行的某個屬性資料,然後將該屬性資料去重。 二,shell指令碼編寫。 bash 1 #!/bin/bash 2 for line in `cat test1|awk -F "|"

Linux Shell 指令碼程式設計(3)—Shell輸入與輸出

shell 輸入與輸出 提綱: echo read cat 管道 tee 檔案重定向

一文學會Linux-shell指令碼程式設計基礎

終於到shell 指令碼這章了,在以前筆者賣了好多關子說shell指令碼怎麼怎麼重要,確實shell指令碼在linux系統管理員的運維工作中非常非常重要。下面筆者就帶你正式進入shell指令碼的世界吧。 到現在為止,你明白什麼是shell指令碼嗎?如果明白最好了,不明白

Linux-Shell指令碼程式設計-學習-4-Shell程式設計-運算元字-加減乘除計算

對於任何一種程式語言都很重要的特性就是運算元字的能力,遺憾的是,對於shell指令碼來說,這個過程比較麻煩,在shell指令碼中有兩種途徑來進行數學運算操作。 1.expr 最開始的時候,shell提供了一個特別的命令來梳理數學表示式,expr允許在命令列上處理數學表示式

Linux shell指令碼程式設計之函式

在編寫功能比較複雜的shell指令碼時,完成具體任務的程式碼有時會被重複使用,bash shell指令碼提供函式特性實現程式碼複用,函式是被賦予名稱的指令碼程式碼塊。 一、建立函式 在bash shell指令碼中建立函式的格式如下: function name() {

Linux xargs命令

var config {} 表示 ges isp 復制文件 lB 9.png find命令把匹配到的文件傳遞給xargs命令,而xargs命令每次只獲取一部分文件而不是全部 xargs要處理的文件如果不是在結尾,需要加上 -i這個參數 xargs常見命令參數 args:xa

xargs命令

文件名 過程 介紹 pri .sh 例如 接收 替換字符 welcom xargs命令是把接收到的數據重新格式化,再將其作為參數提供給其他命令,下面介紹xargs命令的各種使用技巧 一、將多行輸入轉換成單行輸入: [[email protected]/* */

linux文本處理三劍客之grep命令

gawk 軟件 upper edit 進制 使用 第一個 空格 earch Linux文本處理三劍客之grep grep:文本過濾(模式:pattern)工具 grep, egrep, fgrep(不支持正則表達式搜索) sed:stream editor,

shellLinux shell 之 打印99乘法表

shell linux 腳本 打印99乘法表在任何語言中都是一個必寫的程序,特別是學習了循環之後。 打印99乘法表第一步 眾所周知,99乘法表的格式為x * y = z所以我們至少需要兩個參數,一個為x,一個為y,這裏我們使用 i 和 j 來表示。x 和 y 都不會超過 9 ,並具有一定的規律

linux中chmod與chown兩個命令

In 第一個 ID 利用 root chown 資料 後綴 沒有 在linux系統中chmod,chown命令都可以來設置權限了,但它們也是有區別的,下文小編為各位介紹chmod與chown兩個命令用法與區別介紹。 今天要分享的2個命令也是我們平時常用的,chmod與cho