# 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 November 10, 2026

## 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:

```bash
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:

```bash
echo trip-{beach,city,mountains}
```

```text
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:

```bash
mkdir -p trip-{beach,city,mountains}
ls
```

```text
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:

```bash
echo {beach, city}
```

```text
{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:

```bash
echo {1..5}
echo {01..12}
```

```text
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:

```bash
mkdir -p receipts/2026-{01..12}
ls receipts
```

```text
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:

```bash
echo school/{biology,history}/{notes,homework}
```

```text
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:

```bash
mkdir -p school/{biology,history}/{notes,homework}
ls school/biology
```

```text
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:

```bash
echo {jan,feb}.txt
echo *.txt
```

```text
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:

```bash
touch lease.pdf
echo cp lease.pdf{,.bak}
cp lease.pdf{,.bak}
ls
```

```text
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.

## Key terms

- [Brace Expansion](https://www.ask-codey.com/glossary/brace-expansion)
- [Wildcard](https://www.ask-codey.com/glossary/wildcard)
- [mkdir](https://www.ask-codey.com/glossary/mkdir)

## Cheat sheets

- [Brace Expansion: Twelve Folders in One Line](https://www.ask-codey.com/cheat-sheets/bash-brace-expansion-cheat-sheet)

## Related

- [Wildcards: \*, ? and \[ \]](https://www.ask-codey.com/lessons/bash-wildcards)
- [Making Folders and Files: mkdir and touch](https://www.ask-codey.com/lessons/bash-making-folders-and-files)

Source: https://www.ask-codey.com/lessons/bash-brace-expansion
