Read in 3 minutes

last updated 

How to Create Directories in Linux with the mkdir Command

In Linux systems, you can create new directories either from the command line or with your desktop file manager. The command that allows you to create directories (also known as folders) is mkdir.

In this tutorial, we’ll cover the basics of using the mkdir command, including everyday examples.

Linux mkdir Command Syntax

The syntax for the Linux mkdir command is as follows:

mkdir [OPTION] [DIRECTORY]

The command can take one or more directory names as its arguments.

To create a directory with the mkdir command you need to have write permissions on the parent directory. Otherwise you will receive a permission denied error.

How to Create a New Directory

To create a directory in Linux using the mkdir command simply pass the name of directory as the argument the mkdir command:

mkdir newdir

You can verify that the directory was created by listing the contents using the ls command:

ls -l
drwxrwxr-x 2 username username 4096 Jan 20 03:39 newdir

You can pass the -v (--verbose) option if you want mkdir to print a message for the created directory.

When providing only the directory name, it will be created in the current working directory. The current working directory is the directory from which you are running the commands. You can display the location of your current working directory using the pwd command. To change the current working directory, use the cd (change directory) command.

To create a directory in another location you’ll need to provide the absolute or relative file path to the parent directory. For example to create a new directory in the /tmp directory you would type:

mkdir /tmp/newdir

If you try to create a directory in a parent directory where the user does not have sufficient permissions you will receive Permission denied error:

mkdir /root/newdir
mkdir: cannot create directory '/root/newdir': Permission denied

How to Create Parent Directories

A parent directory is a directory that is above another directory in the directory tree. To create parent directories use the -p option.

Let’s say you want to create a directory /home/linuxize/Music/Rock/Gothic. If any of the parent directories don’t exist you will get an error as shown below:

mkdir /home/linuxize/Music/Rock/Gothic
mkdir: cannot create directory '/home/linuxize/Music/Rock/Gothic': No such file or directory

Instead of creating all missing parent directories one by one you can use the -p option:

mkdir -p /home/linuxize/Music/Rock/Gothic

When using -p, mkdir will create the directory only if it doesn’t exist.

For example if you try to create a directory that already exists and you are not using the -p option, mkdir will print File exists error:

mkdir newdir
mkdir: cannot create directory 'newdir': File exists

How to Set Permissions when Creating a Directory

To set permissions when creating a directory use the -m (-mode) option. The syntax for passing permissions is the same as with the chmod command.

In the following example the new directory will be accessible only to the user creating it:

mkdir -m 700 newdir

If the -m option is not passed to mdkir the newly created directories will have 775 or 755 permissions, depending on on the umask value.

How to Create Multiple Directories

To create multiple directories, specify the directories’ names as arguments, separated by space:

mkdir dir1 dir2 dir3

The mkdir command also allows you to create a complex directory trees with one command:

mkdir -p Music/{Jazz/Blues,Folk,Disco,Rock/{Gothic,Punk,Progressive},Classical/Baroque/Early}

The command above will create the following directory tree:

Music/
|-- Classical
|   `-- Baroque
|       `-- Early
|-- Disco
|-- Folk
|-- Jazz
|   `-- Blues
`-- Rock
    |-- Gothic
    |-- Progressive
    `-- Punk

Conclusion

By now you should have a good understanding of how to use the Linux mkdir command. For more information about the mkdir command, see the mkdir man page.