How the mkdir command works in Linux

Johnny Simpson - Mar 10 '22 - - Dev Community

On Linux and unix-like systems, like Mac, we use mkdir to make a new directory from the terminal. To do this, open up a new terminal, and make sure you're in the right directory using the cd command.

The syntax for mkdir looks like this, where dir_name is the name of your new directory, and [OPTIONS] are optional settings.

mkdir [OPTIONS] dir_name
Enter fullscreen mode Exit fullscreen mode

For example, to create a new directory called "hello_world", we would type:

mkdir hello_world
Enter fullscreen mode Exit fullscreen mode

If you want to make multiple directories, just put them in curly brackets, and separate each directory by a comma. For example, the below code makes two directories called "hello" and "world":

mkdir {hello,world}
Enter fullscreen mode Exit fullscreen mode

Options for mkdir

mkdir has 3 options which we can add to our command:

  • -p - this allows us to make multiple directories within each other.
  • -v - this outputs information on the directory or directories created.
  • -m - this lets us set the chmod/mode value for our directory, i.e. 777.

Making multiple directories within each other with mkdir -p

Let's say we wanted to create a folder structure, where we have a project folder within a parent folder, within a master folder. If we use just mkdir, we would have to make each individually. Instead, we can use mkdir -p to make them all at once.

mkdir -p master/parent/project
Enter fullscreen mode Exit fullscreen mode

This will make three directories, each within the other.

Verify a folder is created with mkdir

If we want to see a message about if mkdir was successful or not, we can use mkdir -v.

mkdir -v master
Enter fullscreen mode Exit fullscreen mode

The above will output the following message:

mkdir: created directory 'master'
Enter fullscreen mode Exit fullscreen mode

Unfortunately, -p and -v will not work together - so you have to use one or the other.

Setting the chmod or mode of a directory with mkdir

If we want to set the mode of a directory, we can set it directly with mkdir. For example, to create a directory with a chmod value of 777, we would write the following:

mkdir -m777 master
Enter fullscreen mode Exit fullscreen mode

Creating multiple directories with a certain chmod or mode using mkdir

We can combine -m and -p for mkdir into one command, if we want. For example, the below code will create the folder structure master/parent/project, and set each directory to a mode of 777:

mkdir -p -v -m777 master/parent/project
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .