bash ワイルドカードは否定一致を指定できますか?

bash ワイルドカードは否定一致を指定できますか?

bashワイルドカードを使用して、「現在のディレクトリ内の [特定の (ワイルドカード) パターンに一致するファイル] を除くすべてのファイル」を指定することは可能ですか? 例: 「*~ に一致しないすべてのファイル」

または、より一般的には、ワイルドカード ファイル仕様を 2 番目のフィルタリングまたは否定仕様で修飾することは可能ですか?

答え1

もちろんです。ファイル名に「foo」という文字列を含むファイルをすべて取得したいが、「bar」という文字列を含むファイルは取得したくないとします。

foo1
foo2

でもあなたは望んでいない

 foobar1

次のように単純なグロブを使用してこれを行うことができます。

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.

関連情報