Recursive directory creation with shell brace expansion (#snippet)

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
# β”‚ └── what…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Recursive directory creation with shell brace expansion (#snippet)." Stefan Judis | Sciencx - Wednesday December 29, 2021, https://www.scien.cx/2021/12/29/recursive-directory-creation-with-shell-brace-expansion-snippet/
HARVARD
Stefan Judis | Sciencx Wednesday December 29, 2021 » Recursive directory creation with shell brace expansion (#snippet)., viewed ,<https://www.scien.cx/2021/12/29/recursive-directory-creation-with-shell-brace-expansion-snippet/>
VANCOUVER
Stefan Judis | Sciencx - » Recursive directory creation with shell brace expansion (#snippet). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/29/recursive-directory-creation-with-shell-brace-expansion-snippet/
CHICAGO
" » Recursive directory creation with shell brace expansion (#snippet)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2021/12/29/recursive-directory-creation-with-shell-brace-expansion-snippet/
IEEE
" » Recursive directory creation with shell brace expansion (#snippet)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2021/12/29/recursive-directory-creation-with-shell-brace-expansion-snippet/. [Accessed: ]
rf:citation
» Recursive directory creation with shell brace expansion (#snippet) | Stefan Judis | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.