One of the most common and fundamental commands in Linux is the mkdir command. The most basic way to use this command is to create one or multiple directories. If you’re coming from Windows as a newcomer to Linux, you probably know directories as folders. The mkdir command can do a few other things with directories as well.
The mkdir command can set the permissions for a specified directory or multiple directories. To use the mkdir command, the user must have permission to create directories in the parent directory. Attempting to use mkdir command without these permissions will result in a “permission denied” error.
While the mkdir command is one of the most basic Linux commands, it can still get a little more complex than we’ve explained so far with its many different options that you can use to create and edit directories. You’ll find a few of these options very helpful, although that isn’t to say that mkdir on its own doesn’t already cover a wide array of administration situations.
In this tutorial, you’ll learn how to use the mkdir command in Linux through examples. Follow along below to learn about the various options that you can use with this command.
In this tutorial you will learn:
- How to use the mkdir command on Linux
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Frequently Used Options
The mkdir command creates a directory in the current working directory, unless some other path is specified. The options you can use with mkdir are mostly just to create and edit directories in various, specific ways to meet certain needs and provide extra information.
mkdir command in Linux Basic Examples
- Running the mkdir command by itself, without any additional options, will just create a directory in your present working directory. The current directory is the directory in which a user is working at a given time.
$ mkdir directory01
As you can see in the screenshot above, we have succesfully created a new directory. This output will always be the same, in which the mkdir command silently creates a directory in the background, if you run mkdir without any additional options. You can change this result by using one of the various options with mkdir. We’ll cover that in the next example.
- Using the
-v(verbose) option, we can get the mkdir command to tell us what it is doing in the background so we can see it in action. In the example below, we’ll show you how to use the syntax in your Linux terminal to get this output.
$ mkdir -v directory02
As you can see in the screenshot above, the output from mkdir -vcreates a directory and tells us what it did in the background.
- In the examples above, we’ve shown how the mkdir command can be used to create a directory. But as you’ll recall from the introduction of this post, the mkdir command can create multiple directories at a time. We can achieve this by typing more than one directory following the mkdir command.
$ mkdir directory03 directory04
As you can see in the screenshot above, creating multiple directories can be achieved by typing each directory name after the command.
The mkdir command is pretty simple. But as you’ve observed throughout the examples section of this article, there are many other options we can use with it. Many of these options are sometimes overlooked, and even some experienced system administrators may not know them. However, they can come in handy in various situations. In this section, we’ll show you a few of the lesser-known options of the mkdir command that we think are useful.
mkdir command in Linux Advanced Examples
- Another way you can use the mkdir command is to tell the created directories to give read, write, and execute permissions for the contents of the created directories. This command-line option is essential for system administrating. So, committing this to memory will benefit you greatly if you’re in this line of work. In the syntax below, you’ll notice we used
a=rwx. This is just how the permissions we mentioned above are specified.
$ mkdir -m a=rwx
As you can see in the screenshot above, the
-moption lets us grant all users permissions to read from, write to, and execute the contents of the directories that we created.
- Use the
--versionoption to display the version number and some additional information about the license and exits.
$ mkdir --version
- Now that you know how to use the mkdir command to create multiple directories, we can start learning how to use the
-poption for creating parent directories. By default, using the mkdir command to make parent directories will cause the command to display the following message
If we use the
-poption, the mkdir command is able to create the directories and will not display an error message.
$ mkdir -p directory1/directory2/directory3
-poption, the mkdir command was able to create the
directory1/directory2/directory3directories without getting an error message.
In this tutorial, we learned all about the mkdir command on Linux. The mkdir command is important to master for personal use and system administration to create and edit directories. It’s especially useful for granting access to users read, write, and execute permissions by appending the