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 5.5k readers and learn something new every week with Web Weekly.
Reply to this post and share your thoughts via good old email.