Harness the Combinatoric Power of Command-Line Tools and Utilities
Making Directory Structures
Published January 23, 2019
Use the mkdir command and brace expansion to create a complex directory structure in a single command.
Transcript
Hi everyone. This is Brian and today we’re going to look at creating complex directory structures quickly
using the CLI. To get this done we’ll use the mkdir
command and a feature called Brace expansion.
We’re going to create this structure which is completely contrived, but mirrors the kind of structure you might find in a modern static site project:
cat files.txt
project
├── assets
│ ├── images
│ ├── sass
│ └── ts
├── content
├── originals
├── public
│ └── assets
│ ├── css
│ ├── images
│ └── js
└── templates
We’ll have directories for Typescript, Sass, and unoptimized image files in the assets
directory,
our content in the content
directory, a directory called originals
to hold any raw content for the
site like high-res images, EPS files, things like that. We’ll have our templates in their own directory, and finally we’ll have a pubic
directory
where the css, images, js files will live, along with the HTML files.
To create this we’ll use the mkdir
command with the -p
switch which lets us create nested directories.
Let’s build the command up. We’ll start by specifying the top directories:
mkdir -p project/assets project/content project/originals project/public project/templates
That’s a lot of repetition. Let’s use brace expansion to get rid of that repetition:
mkdir -p project/{assets,content,originals,public,templates}
We can nest the brace expansion, which means we can handle those deeper directories:
mkdir -p project/{assets/{ts,sass,images},content,originals,public/assets/{js,css,images},templates}
That should do it. We’ll run the command and verify it with tree
:
tree project
project/
├── assets
│ ├── images
│ ├── sass
│ └── ts
├── content
├── originals
├── pages
├── public
│ └── assets
│ ├── css
│ ├── images
│ └── js
└── templates
There you go. a one-line command to create a directory structure using brace expansion. Watch this video again, then see if you can run this command from memory. I recommend constructing it just like I did.