2013-10-12 48 views
0

我有這樣的文件在某個目錄:如何使用grep獲取某些文件名?

[email protected]:~/kys$ ls 
address_modified_20130312.txt customer_rows_full_20131202.txt 
customer_full_20131201.txt  customer_rows_modified_20131202.txt 
customer_modified_20131201.txt 
[email protected]:~/kys$ 

我想用grep獲取與一個字「客戶」開頭的文件名一定。我試過這個

[email protected]:~/kys$ ls | grep customer.* 
customer_full_20131201.txt 
customer_modified_20131201.txt 
customer_rows_full_20131202.txt 
customer_rows_modified_20131202.txt 
[email protected]:~/kys$ 

但是,這給了我這些customer_rows。*文件,我不想。正確的結果集是

customer_full_20131201.txt 
customer_modified_20131201.txt 

如何實現此目的?

回答

1

使用grep

ls -1 | grep "^customer_[^r].*$" 

使用find命令

find . \! -iname "customer_rows*" 
1

你可以試試:

ls customer_[fm]* 

ls customer_[^r]* 
1

使用grep -v過濾掉你不想要的東西。

ls customer* | grep -v '^customer_rows' 
0

使用bash擴展通配符,你可以說

ls customer_!(rows)* 

,或者更可能的是,像

for f in customer_!(rows)*; do 
    : something with "$f" 
done 

符合POSIX shell或傳統的Bourne,你可以說

for f in customer_*; do 
    case $f in customer_rows*) continue ;; esac 
    : something with "$f" 
done 
相關問題