欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

linux shell腳本學習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.

例如,下面的命令:

復制代碼 代碼如下:

rm `find /path -type f`

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

復制代碼 代碼如下:

command | xargs

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

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

復制代碼 代碼如下:

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

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

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

復制代碼 代碼如下:

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

實例應用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)按要求替換相應的參數(shù)

實例應用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 {}

您可能感興趣的文章:

相關文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    Linux?signal()函數(shù)的使用學習

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

    Linux shell命令幫助格式詳解

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

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

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

最新評論