How to Use the mkdir Command in Linux

mkdir is a fundamental command in Linux, used for creating directories within the file system. Standing for ‘make directory,’ this command allows users to construct a new directory at a specified path, enabling the organization and management of files and folders. With various options such as -p, which enables the creation of nested directories, and integration with permissions and security contexts, mkdir provides flexibility in controlling the directory structure.

Whether you’re a system administrator or a regular user, understanding how to use mkdir can be vital for managing files and creating a well-organized file system.

General syntax for mkdir command:

$ mkdir [OPTION...] [DIRECTORY...]
1.Display detailed information of command
mkdir -v

Use the -v (verbose) option to see what the mkdir command is doing in the background.

Example:

When you execute the mkdir -v newdir command in Linux, it creates a directory named “newdir,” and the -v (verbose) option tells it to display a message describing what it’s doing. The typical output of this command would look something like this:

mkdir: created directory 'newdir'
2.Create multiple directories
mkdir [dir1] [dir2] [dir3]

Create multiple directories by specifying the directory names as command arguments, separated by spaces.

Example:

The command mkdir newdir1 newdir2 newdir3 will create three new directories named newdir1, newdir2, and newdir3 in the current working directory. Typically, this command will not produce any output if it’s successful.

If you run ls immediately after that command, you might see output like this, assuming there were no other files or directories in the current directory:

newdir1  newdir2  newdir3
3.Set directory permissions
mkdir -m [permission] [dir]

Use the -m (-mode) option with the mkdir command to create a directory with specific permissions.

Example:

When you run the command mkdir -m 755 mydirectory, the newly created directory named mydirectory will have the permissions set to 755. This means the owner will have read, write, and execute permissions, while the group and others will have read and execute permissions.

Here’s a breakdown of the 755 permissions:

  • 7 (owner): Read (4) + Write (2) + Execute (1) = 7
  • 5 (group): Read (4) + Execute (1) = 5
  • 5 (others): Read (4) + Execute (1) = 5
4.Create directory with SELinux context
mkdir -Z [dirname]

This option is used to set default SELinux (Security-Enhanced Linux) rules for a specific directory at creation time.

5.Create multi-level directories
mkdir -p [directory_tree]

Use the -p option with the mkdir command to create a complete directory structure.

Example:

Here’s an example:

mkdir -p /path/to/your/directory/tree

This command will create the entire directory path if it does not exist. If any part of the path (/path/to/your/directory) is missing, it will be created, along with the final tree directory. If the directories already exist, mkdir -p will not report an error, and no changes will be made to existing directories.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail