The Linux command mkdir (make directory/make directories) can be used to create new directories.
Creating directories in the current working directory
The most basic usage of mkdir is the following:
This will create a new directory with the given name in the current directory. Therefore, if your username is “me” and in the terminal you are using you are currently in your home folder (/home/me) then running the command
will create a the directory /home/me/backup.
Creating directories using relative or absolute paths
You do not need to be in a directory do create a new directory in it. You can, for example, use relative or absolute paths to create them.
Let’s assume you are in your home folder (/home/me) and you just created the folder backup (/home/me/backup). And now you want to create a folder named “old” in it. Then you can either call mkdir with a relative path:
or with the absolute path:
Note, however, that if you would call
and the directory “backup” would not exist already in the current folder, then you would get an error. This is, because calling mkdir like this:
will create the directory <name> in the directory <path> only if <path> already exists. Otherwise, mkdir will quit with an error message. This is the default behavior of mkdir.
There are a few options which can be quite useful when creating directories.
Under “Basic Usage”, I mentioned that the path you want to create a new directory in, needs to exists or otherwise you will get an error. You can use the option “-p” or “–parents” to change that behavior. With this option, mkdir will create all the directories that don’t exists already, not just the last one. If you would call
mkdir -p backup/old
mkdir --parents backup/old
in your home directory and the directory “backup” would not exist already, then the -p options will tell mkdir to create this directory as well instead of quitting with an error message. This option is therefore very useful if you need to create a directory, but you are not sure if the directory you want to create it in already exists. Even if you know that this directory does not exist yet, you can use the -p option to reduce the number of necessary calls.
If you know that there currently is no backup (or are not sure) and you want to create the folder backup/old, then calling
could result in an error if the directory “backup” would not exist. This is a problem especially in scripts. Without the -p option you would have to make two calls to mkdir; one to create the “backup” directory (to ensure it is there) and one to create the directory “old” itself:
mkdir backup mkdir old
When creating deeper directory structures, more than two calls might be needed. Think about creating the folder “backup/server/2011/11/30″ in your home folder (/home/me). You would have to create every folder separately:
mkdir backup mkdir backup/server mkdir backup/server/2011 mkdir backup/server/2011/11 mkdir backup/server/2011/11/30
With the -p option you just need to call mkdir once:
mkdir -p backup/server/2011/11/30
and everything will be created for you.
It is also worth noting that using the “-p” option will prevent errors in case the directory already exists. Without the “-p” option, running
would result in the following error message in case the directory backup already exists:
mkdir: cannot create directory `backup': File exists
mkdir -p backup
will prevent the error message from being printed. Running mkdir with the “-p” can therefore be interpreted as “make sure the following directory exists”.
When using the “-v”/”–verbose” option, mkdir will ouput a message for the directory it created.
will therefore either output
mkdir: created directory `backup'
if the directory did not exist already or print the previously mentioned error message in case the directory was already there. When combining the “-v” option” with the “-p” option, mkdir will output a message for every directory it created.
mkdir -p -v backup/server/2011/11/30
or even shorter
mkdir -pv backup/server/2011/11/30
would create the following output if none of the directories would already exist:
mkdir: created directory `backup' mkdir: created directory `backup/server' mkdir: created directory `backup/server/2011' mkdir: created directory `backup/server/2011/11' mkdir: created directory `backup/server/2011/11/30'
If, for example, the directory “backup/server” already existed, then mkdir would only need to create the last three directories and therefore the output would be:
mkdir: created directory `backup/server/2011' mkdir: created directory `backup/server/2011/11' mkdir: created directory `backup/server/2011/11/30'
Using this option therefore provides feedback about which directories already existed and which had to be created. Because the “-p” option suppresses error messages about already existing directories, you won’t get error messages about the fact that “backup” and “backup/server” could not be created, because they were already there. The messages from mkdir about which directories have been created would therefore only implicitly tell you which directories existed beforehand.
Using the “-m” option you can set the permission on the directory you want to create. If you want to create a directory which should be fully accessible by everyone, you could type
mkdir -m 777 backup
mkdir -m a=rwx backup
It is important to note that when combining this option with the “-p” option, the mode only affects the last directory. Calling
mkdir -p -m 777 backup/server/2011/11/30
would therefore create all the directories necessary, but the permissions specified with the “-m” option will be only assigned to the directory “30″ and not to the other directories. Their permissions are instead determined by the current umask.
It is also worthy to note that the permissions of the directory “30″ would not be changed in case it already existed. This is especially important to point out, because the “-p” option will prevent all error messages about already existing directories. Therefore, running this command does not give you any feedback about the permissions the directory “30″ has after running the command. If it already existed, the permissions will be left untouched and if it didn’t exist, the permissions will be set as defined by the “-m” option.
Therefore, if you are not sure if the directory already existed, but want to make sure that the permissions are set correctly, you could either run the command with the -v option (which will then tell you if the directory “30″ was created) and then use chmod if necessary or – if you are writing a script – you should just run chmod afterwards instead of using the “-m” option:
mkdir -p backup/server/2011/11/30 chmod 777 backup/server/2011/11/30