Recursive directory creation with shell brace expansion
Written by Stefan Judis
- Published at
- Updated at
- Reading time
- 1min
I came across this shell one-liner command to create a bunch of new directories recursively today! πͺ
mkdir -p new-dir/{foo,baz}/whatever-{1,2}/{a,b};
# new-dir
# βββ baz
# β βββ whatever-1
# β β βββ a
# β β βββ b
# β βββ whatever-2
# β βββ a
# β βββ b
# βββ foo
# βββ whatever-1
# β βββ a
# β βββ b
# βββ whatever-2
# βββ a
# βββ b
The one-liner's magic is based on two things: mkdir's -p flag and a shell feature called brace expansion.
-p instructs mkdir to create intermediate directories as required. It's recursive directory creation so to say.
And brace expansion allows magic like the following.
$ echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
Magic!

If you enjoyed this article...
Join 6.5k readers and learn something new every week with Web Weekly.
Reply to this post and share your thoughts via good old email.
