<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Linux入門(mén)之玩轉(zhuǎn)xargs命令

          共 5220字,需瀏覽 11分鐘

           ·

          2021-03-10 03:30

          ??本文將介紹Linux中的xargs命令。
          ??在Linux系統(tǒng)中,有些命令只能以命令行參數(shù)的形式接受數(shù)據(jù),而無(wú)法通過(guò)stdin接受數(shù)據(jù)流。xargs正好擅長(zhǎng)此道!xargs擅長(zhǎng)將標(biāo)準(zhǔn)輸入數(shù)據(jù)轉(zhuǎn)換成命令行參數(shù),使其適用于多種問(wèn)題場(chǎng)景!

          工作原理

          ??xargs命令可以用自己的定界符來(lái)分隔參數(shù),默認(rèn)定界符為" "(空格)。用-d選項(xiàng)為輸入指定一個(gè)定制的定界符,如下:

          echo "splitXsplitXsplitXsplitXsplit" | xargs -d X
          split split split split split

          在上述命令中,因?yàn)橹付╔為定界符,因此xargs命令會(huì)將字符串splitXsplitXsplitXsplitXsplit變成split split split split split。
          ??結(jié)合-n選項(xiàng),可以將輸入劃分成多行,每行n個(gè)參數(shù),比如我們將-n設(shè)置成2,如下:

          echo "splitXsplitXsplitXsplitXsplit" | xargs -d X -n 2
          split split
          split split
          split

          ??借助上述操作,我們可以將多行輸入轉(zhuǎn)換成單行輸入,如下:

          $ cat example.txt 
          1 2 3 4 5 6
          7 8 9 
          10
          $ cat example.txt | xargs
          1 2 3 4 5 6 7 8 9 10

          實(shí)戰(zhàn)演練

          例1. 統(tǒng)計(jì)Python腳本的代碼行數(shù)

          ??比如我們想統(tǒng)計(jì)某個(gè)文件夾下的Python腳本的代碼行數(shù),我們可以使用ls -1 *.py命令找到所有Python腳本,借助xargs轉(zhuǎn)換成命令行參數(shù),再使用wc -l統(tǒng)計(jì)文件的行數(shù)即可。演示如下:

          $ ls -1 *.py
          common.py
          main.py
          server.py
          util.py
          $ ls -1 *.py | xargs wc -l
             6 common.py
            17 main.py
            82 server.py
           170 util.py
           275 總用量

          這樣我們使用一行命令就能統(tǒng)計(jì)該文件夾下所有Python腳本的代碼行數(shù)。
          ??借助xargs中的-p參數(shù)讓 xargs 提示我們是否要進(jìn)行下一步的操作,同時(shí)還能看到下一步的操作是怎樣的。

          $ ls -1 *.py | xargs -p wc -l
          wc -l common.py main.py server.py util.py ?...y
             6 common.py
            17 main.py
            82 server.py
           170 util.py
           275 總用量

          在上述例子中,使用了-p后輸入命令會(huì)提示是否執(zhí)行wc -l common.py main.py server.py util.py ?命令,我們輸入y運(yùn)行該命令。

          例2. 創(chuàng)造多個(gè)文件夾

          ??本例將演示如何使用xargs創(chuàng)建多個(gè)文件夾,比如創(chuàng)建example1至example10共10個(gè)空文件夾。
          ??首先創(chuàng)建example.txt,每行含有example{n},其中n為1-10,再使用xargs轉(zhuǎn)換成命令行參數(shù),最后使用mkdir命令創(chuàng)建文件夾。如下:

          $ cat example.txt 
          example1
          example2
          example3
          example4
          example5
          example6
          example7
          example8
          example9
          example10
          $ cat example.txt | xargs -p mkdir
          mkdir example1 example2 example3 example4 example5 example6 example7 example8 example9 example10 ?...y

          ??xargs中的-I參數(shù)指定替換字符串,這個(gè)字符串在xargs擴(kuò)展時(shí)會(huì)被替換掉。同時(shí)使用sh -c命令可以啟動(dòng)一個(gè)新的子shell,它會(huì)讓我們的命令更具有可讀性,如下:

          $ cat example.txt | xargs -I % sh -c "echo make directory: %; mkdir %"
          make directory: example1
          make directory: example2
          make directory: example3
          make directory: example4
          make directory: example5
          make directory: example6
          make directory: example7
          make directory: example8
          make directory: example9
          make directory: example10

          例3. 刪除多個(gè)文件夾

          ??本例演示如何使用xargs命令刪除多個(gè)文件夾,比如刪除example1至example10共10個(gè)空文件夾。
          ??命令如下:

          cat example.txt | xargs -p rm -rf
          rm -rf example1 example2 example3 example4 example5 example6 example7 example8 example9 example10 ?...y

          或者使用命令如下:

          $ cat example.txt | xargs -I {} sh -c "echo delete directory: {}; rm -rf {}"
          delete directory: example1
          delete directory: example2
          delete directory: example3
          delete directory: example4
          delete directory: example5
          delete directory: example6
          delete directory: example7
          delete directory: example8
          delete directory: example9
          delete directory: example10

          例4. print0參數(shù)

          ??比如我們的文件名中含有空格,這時(shí)候使用xargs可能會(huì)出錯(cuò)。
          ??我們創(chuàng)建兩個(gè)文件:good morning.txt和good afternoon.txt,使用xrags將其刪除,如下:

          $ touch "good morning.txt"
          $ touch "good afternoon.txt"
          $ ls -1 *.txt | xargs rm -rf
          $ ls -1
          總用量 0
          good afternoon.txt
          good morning.txt

          ??一種方式是使用-I參數(shù),如下:

          $ ls -1 *.txt | xargs -I {} rm -rf "{}"
          $ ls -1

          ??另一種方式為將find指令加上-print0參數(shù),另外將 xargs 指令加上-0參數(shù),如下:

          $ find -name "*.txt" -print0 | xargs -0 rm -rf
          $ ll
          總用量 0

          例5. 與find命令結(jié)合

          ??xargs命令和find命令算是一對(duì)死黨,兩者結(jié)合使用能讓很多任務(wù)變得更輕松高效。本例將演示如何刪除某個(gè)文件夾下的pycache文件夾。
          ??演示命令如下:

          $ tree
          .
          ├── app
          │   ├── app.py
          │   └── __pycache__
          │       └── __init__.py
          ├── config
          │   └── __pycache__
          └── __pycache__

          $ find . -name "__pycache__" -type d -print0 | xargs -p -0 rm -rf 
          rm -rf ./__pycache__ ./config/__pycache__ ./app/__pycache__ ?...y
          $ tree
          .
          ├── app
          │   └── app.py
          └── config

          參考文獻(xiàn)

          1. Linux Shell Scripting Cookbook(Second Edition): Shantanu Tushar, Sarath Lakshman

          2. 如何在Linux上使用xargs命令: https://mp.weixin.qq.com/s/eP6QX1Gwr_MorYHqW_qbrQ

          3. Linux 系統(tǒng) xargs 指令範(fàn)例與教學(xué): https://blog.gtwang.org/linux/xargs-command-examples-in-linux-unix/

              

               注意,活動(dòng)截止時(shí)間為2021年3月10日晚10點(diǎn)。屆時(shí)請(qǐng)大家關(guān)注中獎(jiǎng)消息!

              

          瀏覽 36
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美一区色情视频 | 亚洲国产精品久久久久久 | 久久熟女网| 免费观看国产黄色视频 | 日韩黄色在线电影 |