Artboard 16light, inspiration, solution, idea, innovation,Google Sheets iconSwift icon
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!

Related Topics

Related Articles