How to Use mkdir Command in Linux

The mkdir is the short form for “Make Directory” in Linux. Creating a directory is a common task that you may perform when working in Linux. You can use the mkdir command in Linux to create directories from the command line terminal. 

While working in Linux, you will often create directories and subdirectories to transfer the files across multiple directories. A directory is just a collection of files and folders. Directories are usually created to keep files and data organized. You can achieve this easily by using the command line interface on Linux. 

In this article, we will look at different ways you can use the mkdir command in Linux. We will also look at its flags to fully utilize the command and become efficient at it.

The mkdir Command Syntax

The syntax of the mkdir command is as follows:

mkdir [options] [directories]

In the “options” area, you can add multiple flags to customize the command’s functionality. To view the list of all possible flags, use the man command.

For example:

man mkdir

Alternatively, you can also use the “--help” flag with the mkdir command to view its flags.

For example:

mkdir --help
use mkdir command in Linux

In the “directories” area, you will write the name of the directory that you wish to create. Both the options and directories are passed as an argument to the mkdir command.

Create a New Directory Using the mkdir Command

To create a new directory, type the command, followed by the directory’s name as an argument. 

Syntax:

mkdir <directory name>

For example:

mkdir newDir

To verify that the new directory has been created, use the ls command. For example:

ls

You can see your directory in this long list of files.

Use mkdir command on Linux

Alternatively, you can use the ls command with the -l flag to view the recently created directory. 

ls -l

You have created this directory in your current working directory. To create a new directory at some other location, you will pass the relative or absolute path with the mkdir command. For example: 

mkdir /myFolder/newDir

Alternatively, you can navigate to that particular directory using the cd command. After switching the directory, you can now create a new directory at that location.

Another option is to create the directory in the user’s home directory while staying in the current directory. For this, you will type:

mkdir ~newDir

The ~ symbol will create the directory in the home directory instead of the current working directory.

Create a Parent Directory Using the mkdir Command

A parent directory is a directory above your current directory. To create a parent directory, you will use the mkdir command with the directory path. For example

mkdir /newDir/Distroid/Article/NewAtrticle

In case if any of the parent directories do not exist, then you will get an error as shown in the image below:

Use mkdir command in Linux

Furthermore, you will also get an error if the directory already exists. Hence, to automatically create all the parent directories, use the -p flag when invoking the mkdir command. This will automatically create the missing parent directories too. The syntax for this flag is:

mkdir -p /path/directory

For example:

mkdir -p /newDir/Distroid/Article/NewAtrticle

Set Permissions on Directory 

While creating the directory using the mkdir command, you can also set permissions. To set permissions, you will use the -m (mode) flag while invoking the mkdir command. 

Syntax:

mkdir -m <mode> <directory>

For example:

mkdir -m 700 newDir

In this example, you have created a directory with 700 permissions. It means that only the user creating the directory can access it. You can view the permissions using the ls command with -la flag.

directory permissions

If you do not use the -m flag, the directory will be created with 775 or 755 modes. To add the read, write and execute permission to the directory, use the 777 modes. There are several read, write and execute modes, which are listed below:

  • 0: No permission
  • 1: Execute permission
  • 2: Write permission
  • 3: Execute and write permission
  • 4: Read permission
  • 5: Read and execute permission
  • 6: Read and write permission
  • 7: All permissions

Create Multiple Directories Using the mkdir Command

You can also create multiple directories at the same time. To perform this step, type the directory names with the mkdir command. Also, make sure to separate these arguments using the Space key. Syntax:

mkdir directory_1 directory_2 directory_3

For example:

mkdir dir1 dir2 dir3
Use mkdir command in Linux

Through the mkdir command, you can also create a complex hierarchical directory structure. The syntax for this is:

mkdir -p directory/{subdirectory/maindirectory}

For example: 

mkdir -p newDir/{Articles/NewArticles,OldArticles,CurrentArticles/{Writer1,Writer2,Writer3},News/WebContent/NewsContent}

This command will generate the hierarchical tree as shown below:

  • newDir/
    • News
      • WebContent
      • NewsContent
    • Writer1
    • Writer2
    • Writer3
    • Articles
      • OldArticles
      • CurrentArticles
      • NewArticles

Get Verbose Output

You can also receive a message after creating the directory. To get the display message for each directory you create, you will use the -v flag. The syntax for this flag is:

mkdir -v [directory name]

For example:

mkdir -v newDir

As you can see, you received a message after the directory was created.

Use mkdir to create new directory

Finally, you have created a new directory.

View Version Information

You can also view the version of your mkdir command in Linux. To perform this step, type --version with the mkdir command.

For example:

mkdir --version

Set Default Linux Rules

One more flag that you can use at the time of directory creation is the -Z flag. This allows you to create the directory. In addition, it also sets the default Linux rules on that particular directory at the time of creation. The syntax of this flag is:

mkdir -Z directory_name

For example: 

mkdir -Z myDir

Remove Directory

You can also remove the directory that you have created. You might want to delete the directory in case it is no longer required. Deleting or removing unnecessary directories is important to clean up the space in your systems. For this, we will use the rm command. The syntax for this command is:

rmdir directory_name

For example:

rmdir myDir3

Hence, this command will successfully delete the existing directory. You can also check that you have deleted it successfully by using the ls command. 

Remove directory in Linux

You can also use the -rf flag with the rm command to delete all the subfolders inside the main folder. Finally, you have deleted the folders.

In this article, we have discussed how to use the mkdir command in Linux to create directories in various ways. In addition, the mkdir is a handy utility for system administrators and users. By following this article, you will gain expertise on the mkdir command. If you have any queries, feel free to leave a comment. For more information on mkdir, check out its official guide.

If this guide helped you, please share it.

Leave a Reply
Related Posts