簡體   English   中英

從bash shell中的find中排除文件擴展名列表

[英]Exclude list of file extensions from find in bash shell

我想為我的make文件編寫一個清理例程,除了我文件夾中的必要源文件之外,它會刪除所有內容。 例如,我的文件夾包含具有以下擴展名的文件:.f .f90 .F90 .F03 .o .h .out .dat .txt .hdf .gif。

我知道我可以用以下方法完成此任務

find . -name \( '*.o' '*.out' '*.dat' '*.txt' '*.hdf' '*.gif' \) -delete

使用否定,我可以這樣做:

find . -not -name '*.f*' -not -name '*.F*' -not -name '*.h' -delete

但是,當我嘗試這樣做時:

find . -not -name \( '*.f*' '*.F*' '*.h' \)

我收到一個錯誤:

find: paths must exceed expression: [first expression in the above list]

(在這種情況下,我會得到: find: paths must exceed expression: *.f*

你能解釋為什么會這樣,以及如何做我想做的事情? 每次我想在列表中添加文件擴展名時,我都討厭編寫-not -name。 另外,我想找出為什么這會給我一個錯誤,這樣我就可以更好地學習Linux。

謝謝!

find . -not -name \( '*.f' '*.F' '*.h' \)

被解釋為

find
    .                      # path to search
    -not                   # negate next expression
    -name \(               # expression for files named "("
    '*.f' '*.F' .'*.h' \)  # more paths to search?

導致錯誤。

由於這些是單字母擴展,您可以將它們折疊為單個glob:

find . -not -name '*.[fFh]'

但如果它們更長,你必須寫出全球

find . -not -name '*.f' -not -name '*.F' -not -name '*.h'

要么

find . -not \( -name '*.f' -o -name '*.F' -o -name '*.h' \)

或切換到使用正則表達式。

find . -not -regex '.*\.(f|F|h)$'

請注意, find中的正則表達式不是POSIX標准的一部分,可能並非在所有實現中都可用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM