Search⌘ K
AI Features

Creating Directories

Explore how to use the mkdir command to create directories and nested subdirectories efficiently without changing working directories. Understand options like -p for full path creation and avoiding errors, and learn to use brace expansion to quickly build complex directory structures from the command line.

The mkdir command

While we can create files in several ways, we’ll use the mkdir command to create directories. We learned how to use this command in Creating Directories in the introduction, but let’s dig in a little deeper by performing a common task: setting up a hierarchy of directories to store documents.

Let’s create a directory by the name of files:

$ mkdir files

Then, we use the ls command to verify the new directory exists:

$ ls -l
Terminal 1
Terminal
Loading...

Creating a subdirectory

Now, let’s create a directory inside of the files directory called photos. Based on what we’ve learned so far, our first instinct might be to do this by changing the working directory to the files directory and then issuing another mkdir command, but that’s not efficient. Instead, we specify the full relative path to the new directory:

$ mkdir files/photos

Use the terminal below to practice these commands.

Terminal 1
Terminal
Loading...

Like the ls, cat, and cd commands, the mkdir command accepts both relative and absolute paths, supporting the double-dot syntax for parent directories. We don’t have to change our working directory when we want to manipulate files or directories in other locations.

Let’s create two more directories underneath files: one for movies and one for music files. We can pass multiple paths to the mkdir command so that we can create both directories like this::

$ mkdir files/movies files/music

Use the terminal below to practice these commands.

Terminal 1
Terminal
Loading...

We have to specify the path to each new directory, though. If we execute the command mkdir files/movies music, then the movies directory is placed under files, but the music directory ends up in our current working directory.

Let’s create a structure to hold documents like Markdown files and diagrams. Specifically, let’s make the structure look like this:

We might be thinking we can do the following:

$ mkdir files/docs/diagrams files/docs/markdown

Unfortunately, we’d see this error message:

mkdir: cannot create directory ‘files/docs/diagrams’: No such file or directory
mkdir: cannot create directory ‘files/docs/markdown’: No such file or directory

To create a directory with the mkdir command, the parent directory must exist first. The files directory exists, but the docs directory doesn’t. So, we could create the docs directory and then reissue these two commands, but we don’t have to.

Creating the entire path

The -p switch tells mkdir to create the entire path, including any directories that don’t yet exist. Let’s try it out:

$ mkdir -p files/docs/diagrams files/docs/markdown

Use the terminal below to practice these commands.

Terminal 1
Terminal
Loading...

That’s pretty handy. The -p switch also suppresses errors if we attempt to create a directory that already exists. For example, we’d get the error mkdir: cannot create directory ‘files’: File exists if we tried to execute the following command again:

$ mkdir files

But if we use the -p switch, we get no errors. This comes in handy when we automate things with scripts.

Creating multiple subdirectories

Let’s add one more structure: a place for the source code. We make a dev directory, and we create the directories elm, go, and js beneath it. Instead of specifying each path, we use a handy shortcut named brace expansion. We can place the four directories we want to create inside of curly braces, separated by commas, like this:

$ mkdir -p files/code/{elm,go,js}

Use the terminal below to practice this command.

Terminal 1
Terminal
Loading...

This command creates all three directories under the files/code directory by taking the prefix files/code and appending it to each entry in the list between the braces. It’s a powerful shortcut, and we can use it in many places.

Our directory structure now looks like this:

If we’re trying to save time, we can create this entire structure in a single command since we can nest braces inside of braces:

$ mkdir -p files/{movies,music,photos,docs/{diagrams,markdown},code/{go,js,elm}}

We built this entire tree with a series of commands without ever leaving our current working directory. This ends up being a huge time saver if we find ourselves creating structures like this for projects. We can use the mkdir command to create directory structures anywhere on the filesystem as long as we have access to the parent directory. And we can prefix the mkdir command with sudo if we need to create the directory elsewhere.