Harness the Combinatoric Power of Command-Line Tools and Utilities
Create Multiple Files With Brace Expansion
Published May 17, 2024
❗ This article is more than six months old. Some things may not work as written.
You know you can create files using the touch
command, but if you have to create multiple files you have to specify each filename.
touch chapter1.md chapter2.md chapter3.md chapter4.md
Brace expansion is a mechanism for generating arbitrary strings. Using brace expansion and a range, you can create these files with the following command:
touch chapter{1..4}.md
In this example, brace expansion works by expanding the sequence inside the braces and combining it with the surrounding text.
- The sequence
{1..4}
generates the list 1, 2, 3, 4. - Each item in the generated list is combined with the surrounding text
chapter
and.md
.
You can use brace expansion when creating directories as well. For example, to create a website
directory with images
, stylesheets
, and scripts
folders, you’d use the following command:
mkdir -v -p website/{images,stylesheets,scripts}
You’ll see the following output:
mkdir: created directory 'website'
mkdir: created directory 'website/images'
mkdir: created directory 'website/stylesheets'
mkdir: created directory 'website/scripts'
Using brace expansion can save some typing, especially if you have a large number of files and folders.