Create multiple folders using Bash

By

Learn how to create many numbered folders at once in Bash using the mkdir {1..30} brace expansion one-liner, instead of making each folder by hand.

~~~

You can create many folders at once in Bash with a single mkdir command, using brace expansion:

mkdir {1..30}

This creates 30 folders, named 1 to 30.

I had the need to create 30 folders from 1 to 30 and I got tired making them manually after the first 4. So I looked on Google and found this Bash one-liner.

How does it work?

The trick is not in mkdir. It’s the shell.

Before running the command, Bash expands {1..30} into 30 separate arguments. mkdir receives them as if you typed mkdir 1 2 3 4 and so on, up to 30.

You can see the expansion yourself with echo:

echo {1..30}
# 1 2 3 4 5 6 7 8 9 10 ... 30

Since the expansion happens before the command runs, this works with any command that accepts multiple arguments, not just mkdir.

Adding a prefix

You can put text before or after the braces. The shell repeats it for every value:

mkdir day{1..30}

This creates day1, day2, up to day30.

Zero-padded numbers

If you want 01, 02, 03 instead of 1, 2, 3 (they sort better in file listings), pad the first number:

mkdir {01..30}

Be careful here. This needs Bash 4 or later, or Zsh. The default Bash on macOS is the old 3.2 version, which drops the padding and creates 1, 2, 3 anyway.

Letters and nested folders

Ranges also work with letters, like {a..z}. And you can combine comma lists with mkdir -p to build a whole tree in one go:

mkdir -p projects/{src,tests}/{2023..2025}

This creates 6 folders: projects/src/2023, projects/src/2024, projects/src/2025, and the same 3 under projects/tests.

A common pitfall

Don’t quote the braces. This:

mkdir "{1..30}"

creates a single folder literally named {1..30}, because quoting disables the expansion. If you end up with that weird folder, that’s why. Remove the quotes and run it again.

One last note: this also works on Zsh but not on Fish (the shell I use). In Fish you can get the same result with mkdir (seq 1 30).

Tagged: CLI · All topics
~~~

Related posts about cli: