Wednesday, 4 March 2026

Standard Globbing vs Extended Globbing

 

Understanding Bash Wildcards and Extended Globbing

When working in Linux or Unix shells like Bash, wildcards (also called globbing) allow you to match multiple files using patterns instead of typing full filenames. This makes file management faster and more powerful.

In this article, we’ll explore:

  • Basic wildcards

  • Pattern matching

  • Extended globbing for advanced matching


1. Sample Files Used for Demonstration

Let’s start by looking at the files available in the directory.

[root@oel01db Shell-Scripting]# ls -1 files/*
files/bar.jpg
files/bar.txt
files/baz.jpg
files/baz.txt
files/foofoo.txt
files/foo.jpg
files/foo.txt
[root@oel01db Shell-Scripting]#

2. The * Wildcard (Matches Anything)

The * wildcard matches any number of characters (including zero characters).

Example: Match all .txt files.

[root@oel01db Shell-Scripting]# ls files/*.txt
files/bar.txt files/baz.txt files/foofoo.txt files/foo.txt
[root@oel01db Shell-Scripting]#

Here, *.txt matches any filename ending with .txt.


3. The ? Wildcard (Matches Exactly One Character)

The ? wildcard matches exactly one character.

Example: Match files with exactly three characters before .txt.

[root@oel01db Shell-Scripting]# ls files/???.txt
files/bar.txt files/baz.txt files/foo.txt
[root@oel01db Shell-Scripting]#

Note:
foofoo.txt is ignored because it contains six characters, not three.

Another example:

[root@oel01db Shell-Scripting]# ls files/fo?.txt
files/foo.txt
[root@oel01db Shell-Scripting]#

This matches files starting with fo followed by one character.


4. Character Sets with [ ] (Bracket Matching)

Square brackets allow matching any one character from a set.

Example: Match files starting with b or f.

[root@oel01db Shell-Scripting]# ls files/[bf]*
files/bar.jpg files/bar.txt files/baz.jpg files/baz.txt files/foofoo.txt files/foo.jpg files/foo.txt
[root@oel01db Shell-Scripting]#

This pattern matches:

  • b* → bar, baz

  • f* → foo, foofoo


5. Extended Globbing (Advanced Pattern Matching)

Standard globbing is powerful, but it lacks logical operators like OR and NOT.

Extended globbing adds advanced pattern matching similar to regular expressions.

By default, it is usually disabled in Bash.

Enable it using:

shopt -s extglob

[root@oel01db Shell-Scripting]# shopt extglob extglob on [root@oel01db Shell-Scripting]#


6. The "Big Five" Extended Globbing Patterns

Once enabled, Bash provides five powerful operators.

PatternMeaningLogic
?(list)Zero or one occurrenceOptional
*(list)Zero or more occurrencesAny amount
+(list)One or more occurrencesAt least one
@(list)Exactly one of the listOR
!(list)Anything except the listNOT

7. Using Extended Globbing

@(list) — OR Logic

Match only foo.txt OR bar.txt.

[root@oel01db Shell-Scripting]# ls files/@(foo|bar).txt
files/bar.txt files/foo.txt

!(list) — NOT Logic

Match everything except .jpg files.

[root@oel01db Shell-Scripting]# ls files/!(*.jpg)
files/bar.txt files/baz.txt files/foofoo.txt files/foo.txt

+(list) — One or More Occurrences

Match files containing "foo" once or multiple times.

[root@oel01db Shell-Scripting]# ls files/+(foo).txt
files/foofoo.txt files/foo.txt

This matches:

  • foo.txt

  • foofoo.txt


*(list) — Zero or More Occurrences

This is a very flexible pattern.

Example: Match files starting with f, followed by zero or more "oo" sequences, ending in .txt.

[root@oel01db Shell-Scripting]# ls files/f*(oo).txt
files/foofoo.txt files/foo.txt

?(list) — Optional Pattern

Match files where the second "foo" is optional.

[root@oel01db Shell-Scripting]# ls files/foo?(foo).txt
files/foofoo.txt files/foo.txt

This matches:

  • foo.txt

  • foofoo.txt


Pro Tip: Test Globs Safely Before Using Dangerous Commands

Before running commands like rm, it's useful to check what the shell expands the pattern to.

You can do this safely using echo.

Example:

[root@oel01db Shell-Scripting]# echo files/!(bar*)
files/baz.jpg files/baz.txt files/foofoo.txt files/foo.jpg files/foo.txt

This helps verify your pattern before executing potentially destructive commands.


Conclusion

Bash globbing provides powerful ways to work with files efficiently:

  • * → Match anything

  • ? → Match a single character

  • [ ] → Match specific characters

  • Extended globbing → Adds OR, NOT, and repetition logic

Once you get comfortable with these patterns, managing large numbers of files in Linux becomes much faster and easier.

No comments:

Post a Comment

JFrog Artifactory - How to install

JFrog Artifactory OSS Installation Guide CentOS 9 + PostgreSQL 17 This guide provides a structured workflow to install JFrog Artifactory OSS...