Site icon New Generation Enterprise Linux

Bash’s `shopt -s extglob` for Advanced File Matching

Quick Tip

Bash’s `shopt -s extglob` for Advanced File Matching

Challenge: You need to perform operations on a set of files that go beyond simple wildcard expansion (like `*` or `?`). Standard globbing can be limiting when dealing with more complex patterns, such as excluding specific files or matching multiple variations.

The Solution: Enable extended globbing in Bash using shopt -s extglob. This unlocks powerful new pattern matching capabilities.

shopt -s extglob ls !(file_to_exclude.txt) rm -v *.{jpg,jpeg,png} rm -v !(temp|backup).log

Why it works: shopt -s extglob enables extended pattern matching features in Bash. This allows you to use operators like `!(pattern)` to match everything *except* the specified pattern, or `?(pattern)`, `*(pattern)`, `+(pattern)`, and `@(pattern)` for more nuanced matching (e.g., zero or one, zero or more, one or more, or exactly one match).

Pro-Tip: You can disable extended globbing with shopt -u extglob. Remember to use it judiciously, as it can make your patterns more complex if overused.

Linux Tips & Tricks | © ngelinux.com | 5/23/2026

0 0 votes
Article Rating
Exit mobile version