2010-03-23 3 views
10

파일 검색에서 특정 문자열을 제외하려고합니다.쉘에서 와일드 카드 검색에서 문자열을 제외하십시오.

파일 목록 (file_Michael.txt, file_Thomas.txt, file_Anne.txt)이 있다고 가정합니다.

내가 할 수와 나에게 file_Michael.txt 및 file_Anne.txt 있지만 file_Thomas.txt를 제공

ls *<and not Thomas>.txt 

같은 것을 쓰고 싶어요.

역은 쉽게 :

ls *[^s].txt 

하지만 어떻게 문자열로 그것을 할 : 하나의 문자로 그 일을

ls *Thomas.txt 

도 쉽다?

세바스찬 떠들썩한 파티와

+1

가능 중복 [방법 내가 역 사용하거나 제외 와일드 때 유닉스/리눅스 쉘 패턴 매칭? (http://stackoverflow.com/questions/216995/how-can-i-use- inverse-or-negative-wildcards-when-pattern-in-a-unix-linu) – Hasturkun

답변

12

당신은이 일을 찾을 수 있습니다

shopt -s extglob 
ls !(*Thomas).txt 

다른 방법

find . -type f \(-iname "*.txt" -a -not -iname "*thomas*" \) 

ls *txt |grep -vi "thomas" 
+1

해답을 보내 주셔서 감사합니다. 첫 번째 라인은 무엇을 의미합니까? – steigers

+3

첫 번째 라인 평균은 확장 된 globbing을 설정합니다. http://www.gnu.org/software/bash/manual/bashref.html#Pattern-Matching for more – ghostdog74

16

:

$ find . -name '*.txt' -a ! -name '*Thomas.txt' 
+0

감사합니다. 그게 내가 필요한 것입니다. – steigers

+1

비슷한 동작을 간단한 와일드 카드에 적용하려면'-maxdepth 1'을 추가해야 할 수도 있습니다 –

2

당신이 와일드 카드를 반복하는 경우, 나머지 부분은 건너 뛰세요. 당신이 제외시키고 자하는 것이 있다면 그것을 사용하십시오.

for file in *.txt; do 
    case $file in *Thomas*) continue;; esac 
    : ... do stuff with "$file" 
done