Brace Expansion: Twelve Folders in One Line
Braces turn one word into many before a command runs, from a list such as {beach,city} or a range such as {01..12}, so mkdir makes a folder for every month in one line and cp file{,.bak} makes a quick backup.
Apprentice · Unix · Linux · Updated
After this lesson you can
- Make many folders or files in one line with a brace list or a range such as {01..12}
- Tell brace expansion from a wildcard, and back up a file with cp file{,.bak}
Key takeaways
- A list in braces, such as trip-{beach,city}, becomes one word for each item. Leave no spaces after the commas.
- A range such as {1..5} or {01..12} counts for you. A leading zero makes every number the same width, so the names sort in order.
- Braces make names whether or not those files exist. Wildcards only match names that are already there.
- cp file{,.bak} is a quick backup. The shell turns it into cp file file.bak.
Tax season is coming, and you want your receipts in a folder for each month: 2026-01, 2026-02, all the way to 2026-12. That's twelve folders. In a file manager it's twelve rounds of New Folder, and even mkdir means typing twelve names, any one of which could get a typo. Wildcards can't help, because they only find names that already exist, and these folders don't exist yet. The shell has another trick for making names: braces.
These examples work in a new, empty practice folder:
mkdir -p ~/terminal-practice/braces && cd ~/terminal-practice/braces
A list in braces
Put a list of words between braces, { } (read "braces" or "curly brackets"), with commas between them. The shell turns the word into one copy for each item, with whatever sits before and after the braces attached. As with wildcards, echo shows you the result first:
echo trip-{beach,city,mountains}
trip-beach trip-city trip-mountains
The shell does this before the command runs, so any command gets the full list. mkdir makes three folders:
mkdir -p trip-{beach,city,mountains}
ls
trip-beach trip-city trip-mountains
Leave no spaces after the commas. A space splits the line into separate arguments, and the braces are left as they were:
echo {beach, city}
{beach, city}
A range: {01..12}
Two numbers with .. (read "dot dot", or here "to") between them make a range, and the shell counts for you:
echo {1..5}
echo {01..12}
1 2 3 4 5
01 02 03 04 05 06 07 08 09 10 11 12
Starting with 01 makes every number two digits wide. That matters for folder names, because names sort one character at a time. Without the zero, 2026-10 sorts before 2026-2; with it, 2026-09 comes right before 2026-10, and the months stay in order. Now the twelve receipt folders take one line:
mkdir -p receipts/2026-{01..12}
ls receipts
2026-01 2026-03 2026-05 2026-07 2026-09 2026-11
2026-02 2026-04 2026-06 2026-08 2026-10 2026-12
Ranges also count down, as in {5..1}, and work with letters, as in {a..e}. zsh, the shell on a Mac, makes lists and number ranges the same way.
Two sets of braces
Two sets of braces give you every combination. A school term with a notes folder and a homework folder for each class:
echo school/{biology,history}/{notes,homework}
school/biology/notes school/biology/homework school/history/notes school/history/homework
Two classes times two folders makes four paths, and mkdir -p makes them all, with school and each class on the way:
mkdir -p school/{biology,history}/{notes,homework}
ls school/biology
homework notes
Braces make names, wildcards find them
Braces and wildcards look alike, and both are expanded by the shell, but they do opposite jobs. A wildcard looks in the folder and matches names that are already there. Braces make names whether or not anything has them. This folder has no .txt files:
echo {jan,feb}.txt
echo *.txt
jan.txt feb.txt
*.txt
So braces suit making new things, and wildcards suit things you already have, such as moving every photo.
One brace trick is worth knowing by heart. Leave the first item empty, and lease.pdf{,.bak} becomes lease.pdf and lease.pdf.bak: a backup copy in one short word. Put echo in front of the whole command to see what will really run:
touch lease.pdf
echo cp lease.pdf{,.bak}
cp lease.pdf{,.bak}
ls
cp lease.pdf lease.pdf.bak
lease.pdf receipts trip-beach trip-mountains
lease.pdf.bak school trip-city
Check yourself
2 questions to try on the lesson page.
So what
A set of folders is no longer a dozen rounds of typing. With braces, it's one line you can read back, and check with echo, before you press Enter.