Linux Command: mkdir (create/make directory)

Summary

Command Namemkdir
UsageCreate directory with specific name
Section / Group User Command

Signature

mkdir [OPTION]… DIRECTORY…

Usage

Create new directory(s) with the provided name(s). If the directories do not exist then the directories are created.

We get an error if the directory already exists.

Notes

  • Directory and file names in Linux are case-sensitive. So “my_dir1”, “My_dir1” and “MY_Dir1” are 3 different directories in Linux file system.

Arguments

Here is the description of what each part of the “mkdir” command means in the above signature-

ParameterDescriptionOptionalMultiple
OPTIONSOptions and flags for different behavior of the commandTrueTrue
DIRECTORYName of the directories that we want to createTrue

NOTE

  • If the directory already exists then the following message is returned-
    mkdir: cannot create directory ‘sample’: File exists

Here are the options-

OptionDescripitonAlias

Examples

Here are some examples of the “mkdir” command, used in different use cases-

Create Directory

$ mkdir my_projcect

$ ls
my_projcect

$ mkdir my_projcect
mkdir: cannot create directory ‘my_projcect’: File exists
Bash

Create Directory with Parent

If we want to create a nested directory in Linux using a simple “mkdir” command and the parent directories do not exist then we get an error-

$ mkdir ./a/b/c

mkdir: cannot create directory './a/b/c': No such file or directory
Bash

But we can use the “-p” option to auto-create parent directories if the parent directory does not exist.

$ mkdir ./a/b/c -p

$ ls
a  my_projcect

$ ls a
b

$ ls a/b
c
Bash

Create Directory with Absolute Path

$ mkdir /home/bigboxcode/py_test
Bash

Create Multiple Directory

We can create multiple directories using a single “mkdir” command. We just need to pass multiple directory names separated by space.

Here we are creating 3 directories “my_dir1”, “my_dir2”, and “some_extra_dir”

# Create 3 directories
$ mkdir my_dir1 my_dir2 some_extra_dir

# Check the directories
$ ls
a  my_dir1  my_dir2  my_projcect  py_test  some_extra_dir
Bash

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.