mkdir command is one of the basic Unix commands which allows you to create new directories.
In its simplest form, mkdir takes one or more directory names as command line parameters.
Creating only one directory:
Creating a few directories at a time:
It’s very easy to verify that these commands have been successful, but getting the list of directories matching the newdir mask in /tmp:
Sometimes you get the mkdir cannot create directory error, usually that’s because specified directory exists or some element of path is missing or restricts you with file/directory permissions.
Sometimes you want to create a whole branch of directories tree, with a number of (initially empty) branched directories.
For example, if you decided to create a /tmp/mydir/newdir, and there is no /tmp/mydir directory present in /tmp, you will get an error:
The reason you get an error is because /tmp/mydir directory does not exist, so your request to create a newdir subdirectory in /tmp/mydir is invalid. The normal approach would be to create /tmp/mydir first, and then issue the same command again:
However, there is a special command line option in mkdir for taking care of non-existent parent directories like this: it’s -p parameter.
Here is the full example of using it, first you see that without the -p you would get an error, and then we verify that the /tmp/mynewdir parent directory was created as part of the mkdir -p command line: