Kurt McKee

lessons learned in production

Hey there! This article was written in 2010.

It might not have aged well for any number of reasons, so keep that in mind when reading (or clicking outgoing links!).

Bash' extglob option

Posted 3 December 2010 in bash

I just love when the functionality I need is already baked into the software I use! Today I needed to create a diff file of some changes I'd made in something like 24 files. The problem was that the files were contained in subdirectories of two directories, and I needed to exclude two types of files that were new and I didn't want to include in the diff file. My first attempt was simplistic:

$ ls feedparser/tests/*/itunes/*explicit*

This got me a listing like:

feedparser/tests/illformed/itunes/itunes_channel_explicit_clean.xml
feedparser/tests/illformed/itunes/itunes_channel_explicit_false.xml
feedparser/tests/illformed/itunes/itunes_channel_explicit_no.xml
feedparser/tests/wellformed/itunes/itunes_channel_explicit_clean.xml
feedparser/tests/wellformed/itunes/itunes_channel_explicit_false.xml
feedparser/tests/wellformed/itunes/itunes_channel_explicit_no.xml

You can see that there are illformed and wellformed versions of these files. The problem was that I needed a way to exclude the 'clean' versions, as these were new files that had no business being in the diff file I was creating! A quick search for "exclude bash glob" got me to Stack Overflow, where someone had already asked this exact question. The solution is the extglob option. With it, far richer glob patterns are available:

$ ls feedparser/tests/*/itunes/*explicit!(_clean*)

This got me the listing I wanted:

feedparser/tests/illformed/itunes/itunes_channel_explicit_false.xml
feedparser/tests/illformed/itunes/itunes_channel_explicit_no.xml
feedparser/tests/wellformed/itunes/itunes_channel_explicit_false.xml
feedparser/tests/wellformed/itunes/itunes_channel_explicit_no.xml

extglob was already turned on in my shell, but if it's not turned on for you just use the shopt command:

$ shopt -s extglob