mkdir command

The mkdir command allows you to create directories that you name. This command can create multiple directories at once or even multiple levels creating the parent directories along the way.

Command: mkdir
Synopsis: mkdir [OPTION]… [DIR]…

Options:
OptionLong option nameDescription
-m–mode=MODESet permission mode (as in chmod), not rwxrwxrwx – umask.
-p–parentsNo error if existing, make parent directories as needed.
-v–verbosePrint a message for each created directory.
-Z–context=CONTEXT(SELinux) set security context to CONTEXT.

Examples

The Base Command

mkidr [DIR] – Using the base command, the mkdir will create a new directory in the current location. Below we create a new folder named testdir.

 # mkdir testdir # ls ./  ../  error_log  index.php  testdir/  test.txt     

Create multiple directories in same folder

mkidr [DIR] – Using the same base command, you can create more than one directory at the same time by separating them with a space. Below we create a new folder named testdir and test2.

 # mkdir testdir test2 # ls ./  ../  error_log  index.php  testdir/  test.txt  test2     

Create multiple levels at once

mkidr [DIR] – If you are wanting to create a subdirectory tree that goes down more than one level, use the -p option. Here we create a subdirectory tree under testdir/level1/level2.

 # mkdir -p testdir/level1/level2 # pwd /home/rubelix/public_html/test/testdir/level1/level2     

Was this article helpful? Join the conversation!