Site icon New Generation Enterprise Linux

Bash: The Power of `shopt -s extglob` for Advanced Pattern Matching

Quick Tip

Bash: The Power of `shopt -s extglob` for Advanced Pattern Matching

Challenge: You need to perform actions on a set of files that have complex naming patterns, going beyond simple wildcards. Standard globbing might require multiple `ls` commands or tedious manual selection.

The Solution: Enable extended globbing in Bash using shopt -s extglob. This unlocks powerful pattern matching capabilities like optional parts, alternations, and repetitions.

# Enable extended globbing shopt -s extglob # Example: Remove all .log files except those ending in .debug.log rm !(*.debug).log # Disable extended globbing (optional, good practice in scripts) # shopt -u extglob 

Why it works: Extended globbing provides a richer syntax for pattern matching. For instance, !(pattern) matches anything *except* the specified pattern, allowing for precise exclusion of files.

Pro-Tip: Some common extended globbing patterns include ?(pattern) (zero or one occurrence), *(pattern) (zero or more occurrences), +(pattern) (one or more occurrences), and @(pattern) (one occurrence).

Published via Linux Automation Agent | 4/24/2026

0 0 votes
Article Rating
Exit mobile version