1. Home
  2. Directory Operations
  3. mkdir Command Examples with Usage

mkdir Command Examples with Usage

The command MKDIR. The basic command in Unix/Linux is for creating directories in consists of the mkdir command and the name of the directory. Here you find the mkdir command examples and usages in different scenarios.

Description

The mkdir (make directory) command in the Unix/Linux, Microsoft Windows, and many more of operating systems is used to make a new directory. It is also available in the EFI shell and in the PHP scripting language. In DOS, OS/2, Windows and ReactOS, the command is often abbreviated to md

SYNOPSIS

mkdir [OPTION]… DIRECTORY…

OPTIONS

Mandatory arguments to long options are mandatory for short options too.

-m, --mode=MODE

Set file mode (as in chmod), not a=rwx – umask.

-p, --parents

No error if existing, make parent directories as needed

-v, --verbose

Print a message for each created directory

-Z

Set SELinux security context of each created directory to the default type.

--context[=CTX]

Like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX.

--help display this help and exit
--version

Output version information and exit.

mkdir Command Usage

As you can add options to this command, the syntax normal usage is as straight forward as follows:

mkdir name_of_directory
mkdir Command Example and Usage
mkdir Command Example and Usage

Where the “name_of_directory” is the name of the directory you wants to create. I’ve created the xyzfile directory.

mkdir Command Examples

Create a directory in current directory or given path:

mkdir directory

Your can creates multiple directories in the current location. Do not use spaces inside {}

mkdir {dir1,dir2,dir3}

Create directories recursively (useful for creating nested dirs): An example of -p for creating a directory structure in action is:

mkdir -p xyzfile/a/b/c
Creates a directory structure - mkdir command example
Creates a directory structure – mkdir command example

If /xyzfile/a exists but /xyzfile/a/b does not, mkdir will create /xyzfile/a/b before creating /xyz/a/b/c.

I’ve checked the result with tree command. you can fine more about tree command in Linux command line section.

You can assign permission while creating a directory. This example creates a directory and sets full read, write, execute permissions for all users.

mkdir –m 777 your_directory_name	

And an even more powerful command, creating a full tree at once (this however is a Shell extension, nothing mkdir does itself):

mkdir -p tmpdir/{trunk/sources/{includes,docs},branches,tags}

If one is using variables with mkdir in a bash script, POSIX `special’ built-in command ‘eval’ would serve its purpose.

DOMAIN_NAME=includes,docs
eval "mkdir -p tmpdir/{trunk/sources/{${DOMAIN_NAME}},branches,tags}"

This will create:

          tmpdir
    ________|______
   |        |      |
branches   tags  trunk
                   |
                 sources
               ____|_____
              |          |
          includes     docs

Read more about mkdir command in in different operating systems.

Updated on September 7, 2021

Was this article helpful?

Related Articles

Leave a Comment