Efficient File Operations with Bash Globbing and Brace Expansion: A Complete Guide

Bash globbing and brace expansion enable complex file operations without loops by expanding wildcards and generating strings before command execution.

The jlevy/the-art-of-command-line repository treats Bash as the primary interactive shell and dedicates significant coverage to filename globbing and brace expansion. Mastering efficient file operations with bash globbing and brace expansion allows you to work with collections of files in single, concise expressions without writing external scripts.

Understanding Bash Globbing for File Matching

Globbing is the process by which Bash expands wildcards such as *, ?, and character classes ([a-z]) into matching pathnames. According to the repository's documentation in README.md at line 62, the * wildcard matches any string, while quoting influences whether expansion occurs at all. This mechanism operates on existing files in the filesystem, returning a list of matches for the command to process.

Common globbing patterns include:

  • * — Matches any string of characters
  • ? — Matches exactly one character
  • [a-z] — Matches any character in the specified range

Leveraging Brace Expansion for Batch Operations

Brace expansion happens before any other expansion, including globbing, and generates multiple strings from patterns enclosed in curly braces. As documented at line 143 of README.md, this feature constructs repetitive filenames or directory trees in a single command. Unlike globbing, brace expansion does not require matching files to exist—it purely generates strings.

Typical use cases include:

  • mv foo.{txt,pdf} some-dir — Moves both files in one command
  • mkdir -p test-{a,b,c}/subtest-{1,2,3} — Creates nested directory structures

Combining Expansion Mechanisms

Because brace expansion executes first in the Bash expansion order, you can combine it with globbing to create highly expressive one-liners. The brace component expands into separate path segments, and then globbing operates on each resulting path.

Practical Code Examples

Listing Files with Wildcards

List all log files with detailed information:

ls -lh *.log

Match specific character patterns:

ls a?.[0-9]*

Bulk Renaming and Moving

Rename multiple extensions simultaneously:

mv report.{txt,pdf} report-final.{txt,pdf}

Creating Complex Directory Trees

Generate nested project structures without multiple commands:

mkdir -p proj-{alpha,beta,gamma}/src-{main,test}

This produces:

  • proj-alpha/src-main
  • proj-alpha/src-test
  • proj-beta/src-main
  • proj-beta/src-test
  • proj-gamma/src-main
  • proj-gamma/src-test

Advanced Pattern Operations

Copy specific file types from multiple source directories:

cp {src,docs,examples}/*.{txt,md} backup/

Here, {src,docs,examples} expands first, then each directory is globbed with *.{txt,md}.

Sequential File Management

Delete a range of numbered files:

rm image_{001..100}.jpg

Brace Expansion in Control Structures

Use brace patterns within loops for systematic operations:

for dir in backup-{2021,2022,2023}; do
    mkdir -p "$dir"/{logs,configs}
done

This creates backup-2021/logs, backup-2021/configs, and corresponding directories for 2022 and 2023.

Summary

  • Globbing operates on existing files using *, ?, and character classes as described at line 62 of README.md in jlevy/the-art-of-command-line
  • Brace expansion generates arbitrary strings before other expansions, documented at line 143
  • Brace expansion occurs first in the Bash expansion order, enabling combination with globbing for complex patterns
  • These mechanisms eliminate the need for explicit loops when managing large file collections

Frequently Asked Questions

What is the difference between bash globbing and brace expansion?

Globbing matches existing files in the filesystem using wildcards like * and ?, returning only actual pathnames. Brace expansion generates text strings from patterns like {a,b,c} regardless of whether corresponding files exist, making it useful for creating new files or directories.

Does brace expansion happen before or after globbing?

Brace expansion happens before globbing in the Bash expansion order. When you execute cp {dir1,dir2}/*.txt backup/, the brace expansion first creates dir1/*.txt and dir2/*.txt, then globbing resolves the * wildcards in each directory.

How do I prevent bash from expanding globs?

Use single or double quotes to suppress glob expansion. For example, echo '*.log' outputs the literal string *.log rather than listing files matching that pattern. Unquoted patterns undergo expansion as described in the README.md file at line 62.

Can brace expansion generate number sequences?

Yes, use the {start..end} syntax to generate sequential numbers. The pattern {001..100} produces zero-padded numbers from 001 to 100, useful for creating or removing numbered files like image_{001..100}.jpg.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →