Create a Directory

The Command mkdir

The command to create a directory is mkdir.

Syntax:

mkdir [options] dir_name
You must have write permission for the directory that you want to create a new directory in.

For example, to create a new directory named birds under the pet directory, you would type:

mkdir birds

You can use ls command to check if the birds directory was created successfully.

The tree below has been updated and now includes birds.

withbirds.png

You can create a directory in a specific path. For example, to create a directory called thirdDir in the path firstDir/secondDir, you would type:

mkdir firstDir/secondDir/thirdDir

If firstDir and secondDir don’t exist, you will get an error cannot create directory. To avoid such an error, you can use -p flag. -p enables the command to create parent directories as necessary. If the directories exist, there will be no error message and the directory will be created. The previous example can be rewritten as follows.

mkdir -p firstDir/secondDir/thirdDir

If firstDir and secondDir don’t exist, using -p option, mkdir will create these directories automatically.

Useful Flags

Flag

Description

-v

It displays a message for every directory created.

-m

It’s used to set the file modes(permissions) for the created directories