Quick Tip
Bash’s `shopt -s extglob`: Tame Complex File Matching
Challenge: You need to select a group of files that match a complex pattern, going beyond simple wildcards like `*` and `?`. Standard globbing can be limiting when you need to exclude certain file types or match alternatives.
The Solution: Enable extended globbing in Bash using `shopt -s extglob`. This unlocks a powerful set of pattern matching operators.
shopt -s extglob ls !(file1.txt|*.log) # Or to match files ending in .txt or .sh ls *.@(txt|sh) # To remove all .bak files except for config.bak rm *.bak -- !config.bak
Why it works: `shopt -s extglob` enables extended pattern matching features in Bash, such as `!(pattern)` for negation, `@(pattern1|pattern2)` for alternation, and `*(pattern)` for zero or more repetitions, allowing for much more sophisticated file selection.
Pro-Tip: Use `shopt -u extglob` to disable extended globbing when you’re done with it. You can check if it’s enabled with `shopt extglob`.
Linux Tips & Tricks | © ngelinux.com | 5/25/2026
