bash 와일드카드가 부정 일치 항목을 지정할 수 있나요?

bash 와일드카드가 부정 일치 항목을 지정할 수 있나요?

bash와일드카드를 사용하여 "[특정(와일드카드) 패턴과 일치하는 파일]을 제외한 현재 디렉터리의 모든 파일"을 지정할 수 있습니까 ? 예: "일치하지 않는 모든 파일 *~"

아니면 더 일반적으로 두 번째, 필터링 또는 부정 사양으로 와일드카드 파일 사양을 한정하는 것이 가능합니까?

답변1

물론. 이름에 "foo"라는 문자열이 포함된 모든 파일을 원하지만 "bar"도 포함하는 파일은 없다고 가정해 보겠습니다. 그래서 당신은 원한다

foo1
foo2

하지만 당신은 원하지 않아요

 foobar1

간단한 globbing을 사용하여 다음과 같이 할 수 있습니다.

for f in foo!(bar)*; echo $f; done

그 쯤

ls foo[^bar]*

자세한 내용은 여기를 참조하세요.http://www.tldp.org/LDP/abs/html/globbingref.html두 가지 방법 모두 함정이 있으니 주의하세요. 를 사용하는 것이 더 나을 수도 있습니다 find.

답변2

extglob shopt이런 종류의 글로빙을 허용하는 bash를 지적해 주신 bjanssen에게 감사드립니다 .

맨페이지 에서 bash:

If the extglob shell option is enabled using the shopt builtin, several
extended pattern matching operators are recognized. In the following 
description, a pattern-list is a list of one or more patterns separated 
by a |.  Composite patterns may be formed using one or more of the following
sub-patterns:

          ?(pattern-list)
                 Matches zero or one occurrence of the given patterns
          *(pattern-list)
                 Matches zero or more occurrences of the given patterns
          +(pattern-list)
                 Matches one or more occurrences of the given patterns
          @(pattern-list)
                 Matches one of the given patterns
          !(pattern-list)
                 Matches anything except one of the given patterns

그래서 내 질문에 "지정하는 방법"에 대답하려면일치하지 않는 모든 파일 *~" :

!(*~)

또는 다음이 필요하지 않습니다 extglob.

*[^~]

그리고 더 일반적으로 내 질문의 마지막 부분에 대답하면 다음과 같습니다.

The GLOBIGNORE shell variable may be used to restrict the set of file names
matching a pattern. If GLOBIGNORE is set, each matching file name that also
matches one of the (colon-separated) patterns in GLOBIGNORE is removed from
the list of matches.

관련 정보