Linux-xargs命令
http://www.jb51.net/article/44720.htm
http://blog.csdn.net/yangshangwei/article/details/52666202
xargs是給命令傳遞參數的一個過濾器,也是組合多個命令的一個工具。它把一個數據流分割為一些足夠小的塊,以方便過濾器和命令進行處理。通常情況下,xargs從管道或者stdin中讀取數據,但是它也能夠從文件的輸出中讀取數據。xargs的默認命令是echo,這意味著通過管道傳遞給xargs的輸入將會包含換行和空白,不過通過xargs的處理,換行和空白將被空格取代。
該命令的主要功能是從輸入中構建和執行shell命令。
在使用find命令的-exec選項處理匹配到的文件時,
find命令將所有匹配到的文件一起傳遞給exec執行。但有些系統對能夠傳遞給exec的命令長度有限制,這樣在find命令運行幾分鐘之后,就會出現溢出錯誤。錯誤信息通常是“參數列太長”或“參數列溢出”。這就是xargs命令的用處所在,特別是與find命令一起使用。
find命令把匹配到的文件傳遞給xargs命令,而xargs命令每次只獲取一部分文件而不是全部,不像-exec選項那樣。這樣它可以先處理最先獲取的一部分文件,然后是下一批,并如此繼續下去。
在有些系統中,使用-exec選項會為處理每一個匹配到的文件而發起一個相應的進程,并非將匹配到的文件全部作為參數一次執行;這樣在有些情況下就會出現進程過多,系統性能下降的問題,因而效率不高;
而使用xargs命令則只有一個進程。另外,在使用xargs命令時,究竟是一次獲取所有的參數,還是分批取得參數,以及每一次獲取參數的數目都會根據該命令的選項及系統內核中相應的可調參數來確定。
下面我們來看看xargs有哪些參數可以選擇.

舟山ssl適用于網站、小程序/APP、API接口等需要進行數據傳輸應用場景,ssl證書未來市場廣闊!成為創新互聯的ssl證書銷售渠道,可以享受市場價格4-6折優惠!如果有意向歡迎電話聯系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
options
-
-afile
: 從file中讀入數據
$cat 1.txt aaa bbb ccc ddd a b $xargs -a 1.txt aaa bbb ccc ddd a b
-
-0
: 當輸入有特殊字符時,將其當作一般字符處理,比如""和空格
$echo "http:// " | xargs // $echo "http:// " | xargs -0 //
-
-d
: 指定分隔符
$cat 1.txt
aaa bbb ccc ddd
a b $cat 1.txt | xargs -d 'c' aaa bbb ddd
a b
-
-Eeof-str
: 指定結束標志為eof-str,xargs處理到這個標志就會停止
$xargs -E 'ddd' -a 1.txt
aaa bbb ccc $xargs -E 'dd' -a 1.txt
aaa bbb ccc ddd a b $cat 1.txt | xargs -E 'ddd' aaa bbb ccc
-
-Ireplace-str
: 將每行輸入輸入內容替換為replace-str
$cat 1.txt
aaa bbb ccc ddd
a b $cat 1.txt | xargs -t -I {} echo {} >> 1.txt echo aaa bbb ccc ddd echo a b $cat 1.txt
aaa bbb ccc ddd
a b
aaa bbb ccc ddd
a b
-
-i
: 等同于-I{}
$cat 1.txt
aaa bbb ccc ddd
a b $cat 1.txt | xargs -t -i echo {} >> 1.txt echo aaa bbb ccc ddd echo a b $cat 1.txt
aaa bbb ccc ddd
a b
aaa bbb ccc ddd
a b
-
-Lmax-lines
: 每次讀取max-line行輸入交由xargs處理
$cat 1.txt
aaa bbb ccc ddd
a b $cat 1.txt |xargs -L 2
aaa bbb ccc ddd a b $cat 1.txt |xargs -L 1
aaa bbb ccc ddd
a b
-
-l
: 類似于-L,區別在于-l可以不指定參數,默認為1.
-
-nmax-args
: 每行執行max-args個輸入,默認執行所有
$cat 1.txt | xargs -n 2
aaa bbb
ccc ddd
a b
-
-p
: 交互模式,執行前詢問是否執行
$cat 1.txt | xargs -p
/bin/echo aaa bbb ccc ddd a b ?...y
aaa bbb ccc ddd a b $cat 1.txt | xargs -p
/bin/echo aaa bbb ccc ddd a b ?...n
-
-r
: 無輸入則停止執行,默認至少執行1次
$ echo ""|xargs -t mv
mv mv: missing file operand
Try `mv --help` for more information.
$ echo ""|xargs -t -r mv #直接退出
-
-smax-chars
:xargs每次執行命令的最大長度(含空格)
$ cat 1.txt
aaa bbb ccc ddd a b
$ cat 1.txt |xargs -t -s 30 /bin/echo aaa bbb ccc ddd a b
aaa bbb ccc ddd a b #length(/bin/echo aaa bbb ccc ddd a b )=30 $cat 1.txt |xargs -t -s 14 /bin/echo aaa
aaa
/bin/echo bbb
bbb
/bin/echo ccc
ccc
/bin/echo ddd
ddd
/bin/echo a b
a b #length(/bin/echo aaa )=14
-
-t
: 先打印執行的命令,然后執行
$cat 1.txt | xargs -t
/bin/echo aaa bbb ccc ddd a b
aaa bbb ccc ddd a b
-
-x
: 當xargs執行的命令長度大于-s max-char時,停止執行
-
-Pmax-procs
: 修改線程數,默認為單線程.max-procs為0時,as many processes as possible
/> ls -l -rw-r--r--. 1 root root 0 Nov 12 10:02 datafile3
-rw-r--r--. 1 root root 10530 Nov 11 23:08 test.tar.bz2
-rwxr--r--. 1 root root 183 Nov 11 08:02 users
-rw-r--r--. 1 root root 279 Nov 11 08:45 users2
#查找當前目錄下的每一個普通文件,然后使用xargs命令來測試它們分別屬于哪類文件。
/> find . -type f -print | xargs file ./users2: ASCII text
./datafile3: empty
./users: ASCII text
./test.tar.bz2: bzip2 compressed data, block size = 900k
#回收當前目錄下所有普通文件的執行權限。
/> find . -type f -print | xargs chmod a-x /> ls -l -rw-r--r--. 1 root root 0 Nov 12 10:02 datafile3
-rw-r--r--. 1 root root 10530 Nov 11 23:08 test.tar.bz2
-rw-r--r--. 1 root root 183 Nov 11 08:02 users
-rw-r--r--. 1 root root 279 Nov 11 08:45 users2
#在當面目錄下查找所有普通文件,并用grep命令在搜索到的文件中查找hostname這個詞
/> find . -type f -print | xargs grep "hostname" #在整個系統中查找內存信息轉儲文件(core dump) ,然后把結果保存到/tmp/core.log 文件中。
/> find / -name "core" -print | xargs echo "" >/tmp/core.log /> pgrep MySQL | xargs kill -9 #直接殺掉mysql的進程
[1]+ Killed mysql
例子如下:
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 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.
例如:
如果path目錄下文件過多就會因為“參數列表過長”而報錯無法執行。但改用xargs以后,問題即獲解決。
find /path -type f -print0 | xargs -0 rm
xargs將find產生的長串文件列表拆散成多個子串,然后對每個子串調用rm。-print0表示輸出以null分隔(-print使用換行);-0表示輸入以null分隔。這樣要比如下使用find命令效率高的多。
find /path -type f -exec rm '{}' \;
xargs命令應該緊跟在管道操作符之后,它以標準輸入作為主要的源數據流,并使用stdin并通過提供命令行參數來執行其他命令,例如:
command | xargs
實例應用1,將多行輸入轉換為單行輸出:
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
實例應用2,將單行輸入轉換為多行輸出:
amosli@amosli-pc:~/learn$ cat example.txt | xargs -n 2
1 2
3 4
5 6
7 8
空格是默認的定界符,-n 表示每行顯示幾個參數
還可以使用-d參數來分隔參數,如下:
amosli@amosli-pc:~/learn$ echo "splitXhiXamosliXsplit" | xargs -d "X" -n 1
split
hi
amosli
split
實例應用3,讀取stdin,將格式化參數傳遞給命令
#定義一個echo命令每次在輸出參數后都加上#
amosli@amosli-pc:~/learn$ cat cecho.sh
echo $*'#'
#需求1:輸出多個參數
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:一次性提供所有的命令參數
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
#批量輸出參數:
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#
#一次性輸出所有參數:
amosli@amosli-pc:~/learn$ cat args.txt | xargs sh cecho.sh ;
arg1 arg2 arg3#
需求3,如何將參數嵌入到固定的命令行中?如下所示:
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 {}批定了替換字符串,字符串{}會被從stdin讀取到的參數所替換,使用-I時,能循環按要求替換相應的參數
實例應用4,結合find使用xargs
前面已經舉過例子,這里要注意的是文件名稱定界符要以字符null來分隔輸出,如下所示,否則可能會誤刪文件
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 {}
文章標題:Linux-xargs命令
本文鏈接:http://m.newbst.com/article18/jescdp.html
成都網站建設公司_創新互聯,為您提供企業網站制作、App開發、關鍵詞優化、品牌網站制作、做網站、靜態網站
廣告
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源:
創新互聯