This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis
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!
Reply to Stefan
This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis
Stefan Judis | Sciencx (2021-12-29T23:00:00+00:00) Recursive directory creation with shell brace expansion (#snippet). Retrieved from https://www.scien.cx/2021/12/29/recursive-directory-creation-with-shell-brace-expansion-snippet/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.