## Negating globbing patterns in Bash

Posted by Diego Assencio on 2015.11.12 under Linux (Shell)

Shell users often run commands with globbing patterns. For example, in the command below, the star (*) expands into a list of all files and subdirectories inside the current working directory which end with .pdf:

ls *.pdf


Bash also allows you to negate globbing patterns. For example, in the command below, the expression !(*.pdf) expands into a list of all files and subdirectories which do not end with .pdf (e.g. file.txt or song.mp3):

ls !(*.pdf)


This works for any globbing pattern. Notice that in expressions with curly braces, the negation is applied to each globbing pattern individually. To be precise, the command:

ls !({*.pdf,prefix*})


is identical to the one below:

ls {!(*.pdf),!(prefix*)}