Bash’s `shopt -s extglob`: Tame Complex File Matching

Quick Tip

Bash’s `shopt -s extglob`: Tame Complex File Matching

Challenge: You need to perform operations on a set of files that go beyond simple wildcards (like `*` or `?`), requiring more sophisticated pattern matching for file selection.

The Solution: Enable the `extglob` shell option in Bash to unlock extended globbing patterns.

shopt -s extglob # Now you can use patterns like: # *@(pattern1|pattern2) - Matches pattern1 OR pattern2 # !(pattern) - Matches anything EXCEPT pattern # +(pattern) - Matches one or more occurrences of pattern # *(pattern) - Matches zero or more occurrences of pattern # ?(pattern) - Matches zero or one occurrence of pattern # Example: Remove all .log and .tmp files except those ending in _old rm !(*_old).@(log|tmp) 

Why it works: The `shopt -s extglob` command enables a set of powerful extended pattern matching features within Bash’s globbing, allowing for more precise and complex file selections than standard wildcards.

Pro-Tip: You can check the current status of `extglob` with `shopt extglob` and disable it with `shopt -u extglob`.

Linux Tips & Tricks | © ngelinux.com | 6/3/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted