欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Linux-xargs命令

Linux-xargs命令

http://www.jb51.net/article/44720.htm
http://blog.csdn.net/yangshangwei/article/details/52666202

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

該命令的主要功能是從輸入中構(gòu)建和執(zhí)行shell命令。       
    在使用find命令的-exec選項處理匹配到的文件時, find命令將所有匹配到的文件一起傳遞給exec執(zhí)行。但有些系統(tǒng)對能夠傳遞給exec的命令長度有限制,這樣在find命令運行幾分鐘之后,就會出現(xiàn)溢出錯誤。錯誤信息通常是“參數(shù)列太長”或“參數(shù)列溢出”。這就是xargs命令的用處所在,特別是與find命令一起使用。  
    find命令把匹配到的文件傳遞給xargs命令,而xargs命令每次只獲取一部分文件而不是全部,不像-exec選項那樣。這樣它可以先處理最先獲取的一部分文件,然后是下一批,并如此繼續(xù)下去。  
    在有些系統(tǒng)中,使用-exec選項會為處理每一個匹配到的文件而發(fā)起一個相應(yīng)的進程,并非將匹配到的文件全部作為參數(shù)一次執(zhí)行;這樣在有些情況下就會出現(xiàn)進程過多,系統(tǒng)性能下降的問題,因而效率不高;  
    而使用xargs命令則只有一個進程。另外,在使用xargs命令時,究竟是一次獲取所有的參數(shù),還是分批取得參數(shù),以及每一次獲取參數(shù)的數(shù)目都會根據(jù)該命令的選項及系統(tǒng)內(nèi)核中相應(yīng)的可調(diào)參數(shù)來確定。

下面我們來看看xargs有哪些參數(shù)可以選擇.

舟山ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

options
  • -afile
    : 從file中讀入數(shù)據(jù)

    $cat 1.txt aaa bbb ccc ddd a b $xargs -a 1.txt aaa bbb ccc ddd a b
  • -0
    : 當(dāng)輸入有特殊字符時,將其當(dāng)作一般字符處理,比如""和空格

    $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
    : 指定結(jié)束標(biāo)志為eof-str,xargs處理到這個標(biāo)志就會停止

    $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
    : 將每行輸入輸入內(nèi)容替換為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,區(qū)別在于-l可以不指定參數(shù),默認(rèn)為1.

  • -nmax-args
    : 每行執(zhí)行max-args個輸入,默認(rèn)執(zhí)行所有

    $cat 1.txt | xargs -n 2 
    aaa bbb
    ccc ddd
    a b
  • -p
    : 交互模式,執(zhí)行前詢問是否執(zhí)行

    $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
    : 無輸入則停止執(zhí)行,默認(rèn)至少執(zhí)行1次

    $ echo ""|xargs -t mv
    mv mv: missing file operand
    Try `mv --help` for more information.
    $ echo ""|xargs -t -r mv    #直接退出
  • -smax-chars
    :xargs每次執(zhí)行命令的最大長度(含空格)

    $ 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
    : 先打印執(zhí)行的命令,然后執(zhí)行

    $cat 1.txt | xargs -t
    /bin/echo aaa bbb ccc ddd a b
    aaa bbb ccc ddd a b
  • -x
    : 當(dāng)xargs執(zhí)行的命令長度大于-s max-char時,停止執(zhí)行

  • -Pmax-procs
    : 修改線程數(shù),默認(rèn)為單線程.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
    #查找當(dāng)前目錄下的每一個普通文件,然后使用xargs命令來測試它們分別屬于哪類文件。

    /> find . -type f -print | xargs file
    ./users2:        ASCII text
    ./datafile3:      empty
    ./users:          ASCII text
    ./test.tar.bz2: bzip2 compressed data, block size = 900k
    #回收當(dāng)前目錄下所有普通文件的執(zhí)行權(quán)限。

   /> 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
    #在當(dāng)面目錄下查找所有普通文件,并用grep命令在搜索到的文件中查找hostname這個詞

    /> find . -type f -print | xargs grep "hostname"
    #在整個系統(tǒng)中查找內(nèi)存信息轉(zhuǎn)儲文件(core dump) ,然后把結(jié)果保存到/tmp/core.log 文件中。

   /> find / -name "core" -print | xargs echo "" >/tmp/core.log       /> pgrep MySQL | xargs kill -9  #直接殺掉mysql的進程
    
[1]+  Killed                  mysql

例子如下:
1. 當(dāng)你嘗試用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.

例如:
如果path目錄下文件過多就會因為“參數(shù)列表過長”而報錯無法執(zhí)行。但改用xargs以后,問題即獲解決。
find /path -type f -print0 | xargs -0 rm

xargs將find產(chǎn)生的長串文件列表拆散成多個子串,然后對每個子串調(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ù)來執(zhí)行其他命令,例如:
command | xargs

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

實例應(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 表示每行顯示幾個參數(shù)

還可以使用-d參數(shù)來分隔參數(shù),如下:
amosli@amosli-pc:~/learn$ echo "splitXhiXamosliXsplit" | xargs -d "X" -n 1
split
hi
amosli
split

實例應(yīng)用3,讀取stdin,將格式化參數(shù)傳遞給命令
#定義一個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ù)嵌入到固定的命令行中?如下所示:
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讀取到的參數(shù)所替換,使用-I時,能循環(huán)按要求替換相應(yīng)的參數(shù)

實例應(yīng)用4,結(jié)合find使用xargs
前面已經(jīng)舉過例子,這里要注意的是文件名稱定界符要以字符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 {}

文章標(biāo)題:Linux-xargs命令
本文鏈接:http://aaarwkj.com/article18/jescdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、App開發(fā)、關(guān)鍵詞優(yōu)化品牌網(wǎng)站制作、做網(wǎng)站、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)
韩国午夜理伦三级好看| 久久精品国产亚洲熟女| 最新亚洲国产高清激情| 日本在线不卡一二三区| 久草福利视频免费播放| 国产三级视频在线观看视频| 久久最新最热视频精品| 国产精品粉嫩在线播放| 国产亚洲一区二区三区在线| 久草福利资源在线观看视频| 日本黄色免费在线观看网站| av免费观看男人的天堂| 日韩av在线专区观看| 国产精品久久亚洲一区二区| 日本免费一区二区三区的电影啊| 国产视频三级在线观看| 国语少妇高潮对白在线| av免费在线观看大全| 国产在线第一页第二页| 有码不卡中文字幕在线视频| 无遮挡国产精品一级二级三级视频 | 夫妻性生活黄色录像视频| 亚洲毛片一区二区在线| 桃色av一区二区三区| 日本岛国免费一区二区| 欧美亚洲国语精品一区二区| 92午夜福利在线视频| 亚洲欧美日韩激情另类| 日韩黄色免费在线观看| 精品国产乱码久久蜜桃| 日本不卡不二三区在线看| 国产一区二区在线不卡播放| 宅男午夜一区二区三区| 性色乱码一区二区三区| 美女少妇性高潮的视频| 天堂8在线最新版av| 丁香六月婷婷激情啪啪综合| 亚洲特级黄色做啪啪啪| 一区二区三区国产不卡| 国内丰满少妇嗷嗷叫在线播放| 日韩激情中文字幕一区二区三区|