乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      find命令中的print0和xargs

       BIOINFO_J 2017-08-15

      注:轉(zhuǎn)載自http://blog.csdn.net/gsyzhu/article/details/38115529

      看到命令find . -name '*.h' -print0 | xargs -0 checkout-cache -f --

      不明白其中-print0和 xargs -0的用法。查了一下,轉(zhuǎn)載一篇備忘。

      xargs命令的作用是將參數(shù)列表轉(zhuǎn)換成小塊分段傳遞給其他命令,以避免參數(shù)列表過長的問題

      以下內(nèi)容轉(zhuǎn)自http://blog.163.com/laser_meng@126/blog/static/16972784420117102638257/

      默認(rèn)情況下, find 每輸出一個文件名, 后面都會接著輸出一個換行符 ('\n'), 因此我們看到的 find 的輸出都是一行一行的:

      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; ls -l
      total 0
      -rw-r--r-- 1 root root 0 2010-08-02 18:09 file1.log
      -rw-r--r-- 1 root root 0 2010-08-02 18:09 file2.log
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; find -name '*.log'
      ./file2.log
      ./file1.log
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; bye

      比如我想把所有的 .log 文件刪掉, 可以這樣配合 xargs 一起用:
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; find -name '*.log'
      ./file2.log
      ./file1.log
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; find -name '*.log' | xargs rm
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; find -name '*.log'
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; bye

      嗯, 不錯, find+xargs 真的很強大. 然而:
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; ls -l
      total 0
      -rw-r--r-- 1 root root 0 2010-08-02 18:12 file 1.log
      -rw-r--r-- 1 root root 0 2010-08-02 18:12 file 2.log
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; find -name '*.log'
      ./file 1.log
      ./file 2.log
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; find -name '*.log' | xargs rm
      rm: cannot remove `./file': No such file or directory
      rm: cannot remove `1.log': No such file or directory
      rm: cannot remove `./file': No such file or directory
      rm: cannot remove `2.log': No such file or directory
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; bye

      原因其實很簡單, xargs 默認(rèn)是以空白字符 (空格, TAB, 換行符) 來分割記錄的, 因此文件名 ./file 1.log 被解釋成了兩個記錄 ./file  1.log, 不幸的是 rm 找不到這兩個文件.

      為了解決此類問題, 聰明的人想出了一個辦法, 讓 find 在打印出一個文件名之后接著輸出一個 NULL 字符 ('\0') 而不是換行符, 然后再告訴 xargs 也用 NULL 字符來作為記錄的分隔符. 這就是 find 的 -print0 和 xargs 的 -0 的來歷吧.
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; ls -l
      total 0
      -rw-r--r-- 1 root root 0 2010-08-02 18:12 file 1.log
      -rw-r--r-- 1 root root 0 2010-08-02 18:12 file 2.log
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; find -name '*.log' -print0 | hd
                 0  1  2  3   4  5  6  7   8  9  A  B   C  D  E  F  |0123456789ABCDEF|
      --------+--+--+--+--+---+--+--+--+---+--+--+--+---+--+--+--+--+----------------|
      00000000: 2e 2f 66 69  6c 65 20 31  2e 6c 6f 67  00 2e 2f 66  |./file 1.log../f|
      00000010: 69 6c 65 20  32 2e 6c 6f  67 00                     |ile 2.log.      |
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; find -name '*.log' -print0 | xargs -0 rm
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; find -name '*.log'
      -(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
      [bash-4.1.5]
       ; bye

      你可能要問了, 為什么要選 '\0' 而不是其他字符做分隔符呢? 這個也容易理解: 一般的編程語言中都用 '\0' 來作為字符串的結(jié)束標(biāo)志, 文件的路徑名中不可能包含 '\0' 字符.



        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多