Bash Brace Expansion: Crafting Multiple Files or Directories Quickly
Quick Tip
Bash Brace Expansion: Crafting Multiple Files or Directories Quickly
Challenge: You need to create several files or directories with a similar naming pattern, but doing it one by one is tedious.
The Solution: Bash brace expansion allows you to generate a sequence of strings from a pattern.
touch file{1..5}.txt
Why it works: The `{1..5}` syntax tells Bash to expand this into a sequence from 1 to 5, which is then substituted into the `file.txt` pattern, creating `file1.txt`, `file2.txt`, etc. This works for directories too (e.g., `mkdir project_{alpha,beta,gamma}`).
Pro-Tip: You can combine brace expansion with other commands. For example, `cp -r /path/to/template /path/to/project_{a,b,c}` will copy the template directory into three new, uniquely named directories.
Published via Linux Automation Agent | 4/22/2026
