2010-04-15 75 views
9

我想從shell(bash)通配字符串*.log中排除特定文件名(比如fubar.log)。沒有什麼我嘗試似乎工作,因爲globbing不使用標準RE集。從shell通配中排除特定文件名

測試情況:該目錄包含

fubar.log 
fubaz.log 
barbaz.log 
text.txt 

只有fubaz.log barbaz.log必須由水珠擴大。

+0

可能重複時,模式匹配一個unix/linux的shell?](http://stackoverflow.com/questions/216995/how-can-i-use-inverse-or-negative-wildcards-when-pattern-matching-in-a-unix-linu)這是另一個非常接近的子集。 – 2014-10-19 13:33:12

回答

15

如果你使用bash

#!/bin/bash 
shopt -s extglob 
ls !(fubar).log 

或不extglob

shopt -u extglob 
for file in !(fubar).log 
do 
    echo "$file" 
done 

for file in *log 
do 
    case "$file" in 
    fubar*) continue;; 
    *) echo "do your stuff with $file";; 
    esac 
done 
+0

謝謝!你有沒有extglob的解決方案? – Alsciende 2010-04-15 09:08:48

+2

被拒絕的編輯表明,在沒有extglob的情況下需要這樣做:'shopt -u extglob'我沒有知識來判斷它。 – 2013-10-25 10:09:37

3

你爲什麼不使用grep?例如:

ls | grep -v fubar | while read line;做回聲「閱讀$行」;完成;

這裏是輸出:

閱讀barbaz.log 閱讀fubaz.log 閱讀[如何我可以使用逆或負通配符的text.txt

+1

因爲glob字符串被賦予一個程序(logrotate)。所以我需要一個glob字符串。 – Alsciende 2010-04-15 09:38:44

相關問題