Create directories or folders in Linux with mkdir command

The Linux command, mkdir command is useful to create one or more new directories.

Determine the type of mkdir command

The mkdir is a file command, go through the below explanation you’ll understand.

type mkdir
# mkdir is /usr/bin/mkdir

Executing type mkdir on terminal gives mkdir is /usr/bin/mkdir as output in Ubuntu 20.04 LTS default(bash) shell.

type -t mkdir
# file

Executing type mkdir on terminal gives file as output in Ubuntu 20.04 LTS default(bash) shell.

This article shows the basic usage of mkdir command with examples.

The mkdir command

Also can be used to create multiple folders/directories.

Syntax

mkdir [OPTION] [DIRECTORY]...

This command accepts one or more directory name separated by space.

Options

  • --help

    It displays the command help information.

  • -v or --verbose

    It displays a verbose message for every directory created.

  • --version

    It displays a command version info.

  • -p

    Useful to create nested directories

How to create a new directory

To create a new directory named practice you would execute the following command:

mkdir practice

The directory named practice will be created in the current working directory & you can verify it using the ls command as shown below.

ls -l
drwxrwxr-x 2 user user 4096 Mar 29 09:35 practice

How to Create Multiple Directories

You can create multiple directories with a single command, specifying the directory names as arguments, each separated by space:

mkdir rainbow fruits

Directory name rainbow and fruits will be created in your present working directory. Verify it with the below command

ls -l
drwxrwxr-x 2 user user 4096 Mar 29 09:39 fruits
drwxrwxr-x 2 user user 4096 Mar 29 09:35 practice
drwxrwxr-x 2 user user 4096 Mar 29 09:39 rainbow

How to Create Nested Directories

The option -p allows you to create multiple nested directories with default permissions.

This also allows you to create a parent directory if it does not exist.

Consider you want to create a directory /home/MeshWorld/Pictures/Fruits/Apples, but executing the below command will give you an error if any of the parent directories do not exist.

mkdir `/home/MeshWorld/Pictures/Fruits/Apples`
mkdir: cannot create directory '/home/MeshWorld/Pictures/Fruits/Apples': No such file or directory

Whereas executing the below command with -p option creates the directory as well as parent directories if missing.

mkdir -p `/home/MeshWorld/Pictures/Fruits/Apples`

Means the -p options creates the /home, /home/MeshWorld, /home/MeshWorld/Pictures, /home/MeshWorld/Pictures/Fruits and /home/MeshWorld/Pictures/Fruits/Apples directories if they do not already exist.

Conclusion

The mkdir command comes in handy in Linux that can be used to create new directories.

The manual(man) is an amazing built-in help for UNIX. For more info with man mkdir(press the q key to exit the man).

Hope you like this!

Keep helping and happy 😄 coding

Vishnu Damwala
Vishnu Damwala

A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳