1. Overview

In Linux, the mkdir command is crucial for creating directories. It helps us organize files to make them easier to locate and manage. Additionally, we can use it with options to customize the result.

In this tutorial, we’ll discuss mkdir along with some of its options.

2. Basic Use of mkdir

mkdir (make directory) is a command that enables us to create new directories within the Linux system.

Fortunately, mkdir follows a simple syntax:

$ mkdir [options] directory_name

Let’s break down this syntax:

  • [options]: represents optional parameters to modify the behavior of mkdir
  • directory_name: defines the name of the directory to create

To demonstrate, let’s create a directory named projects:

$ mkdir projects

This command creates the directory in the current working directory.

If the projects directory exists, an error appears:

$ mkdir projects
mkdir: cannot create directory ‘projects’: File exists

On the other hand, no output is displayed on the screen when the directory creation succeeds.

2.1. Creating Multiple Directories Simultaneously

We can create multiple directories by listing the directory names after the mkdir command:

$ mkdir Music Movies Pictures

This command creates three directories in the current working directory, namely Music, Movies, and Pictures. Notably, we need to separate the directory names with spaces.

2.2. Specifying a New Directory Location

By default, mkdir creates directories in the directory we’re currently operating in. However, we can create a directory in a different location by specifying the complete path to the location. Paths use forward slashes (/) between directories:

To illustrate, let’s create a Projects directory in a different location:

$ mkdir ~/Desktop/Work/Projects

This command creates a new directory named Projects within the Work directory, which is located on the desktop. So, for Projects to be created, all the directories listed in the path should exist. For example, if the Work directory doesn’t exist, we get an error:

$ mkdir ~/Desktop/Work/Projects
mkdir: cannot create directory ‘/home/maurice/Desktop/Work/Projects’: No such file or directory

Once we create the Work directory, the error disappears.

3. Advanced Use of mkdir With Options

Although mkdir is useful in its basic form, it offers more flexibility with options.

3.1. Creating Parent Directories

Here, we use the -p option to create parent directories. The -p option ensures that any missing parent directories in the defined path are created:

$ mkdir -p ~/Desktop/Work/Projects/Reports

If the Work and Projects directories don’t exist, mkdir should throw an error when used without the -p option. However, since we utilize the -p option, mkdir creates the Work and Projects directories along with the Reports directory.

3.2. Displaying the Output

Typically, mkdir creates directories silently with no output. When we use mkdir with the -v option, the command provides a detailed output for each directory creation:

$ mkdir -v Reports Movies
mkdir: created directory 'Reports'
mkdir: created directory 'Movies'

The above command shows notifications in the terminal indicating that the Reports and Movies directories were created successfully.

Additionally, we can use -v when creating a new directory in a different location:

$ mkdir -pv ~/Desktop/Work/Projects
mkdir: created directory '/home/maurice/Desktop/Work'
mkdir: created directory '/home/maurice/Desktop/Work/Projects'

Here, we combine -v with the -p option. So, this command displays a notification for the creation of the parent directory, Work, as well as a notification for the creation of the Projects directory.

3.3. Setting Permissions

The default permissions for a new directory are set based on the system’s umask. However, we can customize these permissions with the help of the -m option and a three-digit permission code.

Now, every permission can be represented by a number:

  • 4 – represents the read permission
  • 2 – represents the write permission
  • 1 – represents the execute permission

So, we can combine these digits to represent directory permissions:

$ mkdir -m 755 Projects

The first digit (7) defines the permissions for the owner of the Projects directory. This corresponds to read (4), write (2), and execute (1), indicating that the owner has full permissions. Meanwhile, the second and third digits (5) represent the permissions for the group and others, respectively. This means that both have read (4) and execute (1) permissions, but not write permissions.

4. Conclusion

In this article, we discussed how to use mkdir with examples. When we use mkdir without options, we can easily create single and multiple directories. However, with options, we can create directories with specific instructions, such as creating parent directories and customizing the directory permissions. This enables us to increase productivity when organizing and managing files. Importantly, we can utilize the man command to get additional details on the mkdir command.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.