Sometimes you want to match filenames but the standard shell expansion won't do it. For example, you want all the files but not the ones that end with .bak - there's no easy way to do that with standard wildcards, but bash can turn on "extended globbing". You do that with " shopt -s extglob". With that in place, you can do things like:
# everything but .html, .jpg or ,gif files ls -d !(*.html|*gif|*jpg) # list file9, file22 but not fileit ls file+([0-9]) # begins with apl or un only ls -d +(apl*|un*)
The lead-in characters recognized when extglob is set are (from the man page):
?(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 exactly one of the given patterns !(pattern-list) Matches anything except one of the given patterns
Got something to add? Send me email.
More Articles by Tony Lawrence © 2009-11-07 Tony Lawrence
Being able to break security doesn’t make you a hacker anymore than being able to hotwire cars makes you an automotive engineer. (Eric Raymond)