¿Pueden los comodines bash especificar coincidencias negativas?

¿Pueden los comodines bash especificar coincidencias negativas?

¿ bashEs posible utilizar comodines para especificar "todos los archivos en el directorio actual EXCEPTO [archivos que coinciden con un patrón específico (comodín)]"? Por ejemplo, "todos los archivos que no coinciden con *~"

O, de manera más general, ¿es posible calificar una especificación de archivo comodín con una segunda especificación de filtrado o negación?

Respuesta1

Por supuesto. Digamos que desea que todos los archivos contengan la cadena "foo" en su nombre, pero ninguno que también contenga "bar". Entonces quieres

foo1
foo2

pero no quieres

 foobar1

Puedes usar globos simples para hacerlo así:

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

más o menos

ls foo[^bar]*

Para más ver aquí:http://www.tldp.org/LDP/abs/html/globbingref.htmlCuidado, ambos métodos tienen sus inconvenientes. Probablemente sea mejor que uses find.

Respuesta2

Gracias bjanssen por señalar bash extglob shoptque permite este tipo de globalización.

Desde la bashpágina de manual:

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

Entonces, para responder a mi pregunta "cómo especificartodos los archivos que no coinciden *~":

!(*~)

o, sin necesidad de extglob:

*[^~]

Y de manera más general, respondiendo a la última parte de mi pregunta:

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.

información relacionada