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

Quick Tip

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

Challenge: Standard bash globbing can be limiting when you need to match complex patterns of files, especially when excluding certain files or matching alternatives. For example, deleting all files except those ending in `.log` or `.tmp` can become cumbersome.

The Solution: Utilize the `extglob` shell option to unlock powerful extended globbing patterns.

shopt -s extglob rm !(*.log|*.tmp)

Why it works: `shopt -s extglob` enables extended pattern matching features in bash. The `!(pattern-list)` syntax then negates the specified patterns, effectively matching everything *except* files ending in `.log` or `.tmp`.

Pro-Tip: You can disable `extglob` with `shopt -u extglob` to return to standard globbing. Explore other `extglob` patterns like `*.@(file1|file2)` for matching specific extensions.

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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted