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*)}

Comments

No comments posted yet.

Leave a reply

NOTE: A name and a comment (max. 1024 characters) must be provided; all other fields are optional. Equations will be processed if surrounded with dollar signs (as in LaTeX). You can post up to 5 comments per day.