日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux 命令 find -exec 操作的问题

發布時間:2023/12/2 linux 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux 命令 find -exec 操作的问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近有這樣一個需求,刪掉某目錄下的一些文件夾。其實就是名為“CVS”的文件夾,用過CVS的人都知道,CVS會在目錄的每一級建立一個名為CVS的文件夾,里面放著CVS相關信息,我需要將某目錄下所有的名為“CVS”的文件夾刪掉。在Linux下其實很簡單,使用find命令:

[plain]?view plaincopy
  • find?.?-name?CVS?-exec?rm?-rf?{}?\;??

  • 看似沒問題,但卻報錯了: [plain]?view plaincopy
  • find:?`./CVS':?No?such?file?or?directory??

  • 檢查了下,發現其實./CVS這個文件夾確實存在而且此時已經被刪掉了,算是功德圓滿但是為什么還報錯?

    沒辦法記得find完成這種功能還有一種寫法:

    [plain]?view plaincopy
  • find?.?-name?CVS?-exec?rm?-rf?{}?\+??

  • 抱著試試的態度,令人意外的是,這種方式成功執行,并未報任何錯誤,這就叫人疑惑了,沒辦法只好求助于男人(man)

    -exec command ;
    ?? ?????? Execute? command;?? ? true? if 0 status is returned.?? ? All following
    ?? ?????? arguments to find are taken to be arguments to the command until
    ?? ?????? an? argument? consisting of ';' is encountered.? The string '{}'
    ?? ?????? is replaced by the current file name being processed? everywhere
    ?? ?????? it occurs in the arguments to the command, not just in arguments
    ?? ?????? where it is alone, as in some versions of find.? Both? of?? ? these
    ?? ?????? constructions might need to be escaped (with a '\') or quoted to
    ?? ?????? protect them from expansion by the shell.?? ? See the EXAMPLES sec-
    ?? ?????? tion? for examples of the use of the '-exec' option.?? The speci-
    ?? ?????? fied command is run once for each matched file.
    ? The command? is
    ?? ?????? executed?? ?in? the?? ? starting? directory.?? ? There are unavoidable
    ?? ?????? security problems surrounding? use? of? the? -exec? option;? you

    ?? ?????? should use the -execdir option instead.

    另外一種:

    -exec command {} +
    ?? ?????? This? variant? of the -exec option runs the specified command on
    ?? ?????? the selected files, but?the command line is built?? ? by? appending
    ?? ?????? each? selected file name at the end; the total number of invoca-
    ?? ?????? tions of the command will?? ? be? much? less?? ? than? the? number? of
    ?? ?????? matched? files.?
    ? ?The command line is built in much the same way
    ?? ?????? that xargs builds its command lines.? Only one instance of? '{}'
    ?? ?????? is? allowed? within the command.?? ?The command is executed in the
    ?? ?????? starting directory.

    看了半天我注意到他們的區別,紅字體標注。

    也就是說 "-exec" + ";" 為每一個匹配的文件都執行了一次命令,具體到此處就是 rm -rf 命令,而 “-exec” + "+" 只是把匹配的文件名作為命令的參數append到命令后面,即是這樣: rm -rf file1 file2 file3

    可是這種差別為什么會導致如此明顯的差異呢? 想了想, 悟到了:

    #1的執行過程應該是這樣:

    1.? 記錄并遍歷當面層的所有目錄和文件,并對比是否匹配。

    2.? 匹配了名為CVS的文件夾,然后執行了命令 rm -rf ./CVS,?

    3. 根據之前的記錄多所有目錄進行遞歸遍歷,此時問題出現,當程序試圖進入該層名為“CVS”的目錄進行遍歷是,發現此目錄不存在,所以報錯。為什么這個目錄沒了,哈,在第二步已經被刪掉了!


    是這樣嗎: 可以驗證:

    [plain]?view plaincopy
  • find?.?-maxdepth?1?-name?CVS?-exec?rm?-rf?{}?\;??

  • -maxdepth 參數指明匹配只發生在當前目錄,并不深入子目錄, 結果是這個命令沒有報錯,也驗證了之前的猜想。

    在此記錄,以備后查。

    總結

    以上是生活随笔為你收集整理的linux 命令 find -exec 操作的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。