Linux mkdir command: Create directories
Linux mkdir command Function Description
Directories can be created on a Linux system using the mkdir command.
The mkdir command is used to create directories. This command creates the directory named by dirname.
- If the directory name is not preceded by any pathname, the directory specified by dirname will be created in the current directory;
- if an existing path is given, a specified directory will be created in that directory. When creating a directory, you should make sure that the newly created directory does not overlap with the files in the directory it is in.
Note: When creating files, do not store all files in the main directory, you can create subdirectories and organize files more efficiently through them. It is best to use consistent naming to distinguish files from directories. For example, directory names can start with a capital letter so that the directory name appears first in the directory listing.
A subdirectory should contain files of similar type or similar purpose. For example, a subdirectory should be created that contains all database files, another subdirectory should contain spreadsheet files, another subdirectory should contain word processing documents, and so on. Directories are also files, they follow the same naming rules as regular files, and a directory can be uniquely specified using full paths.
Linux mkdir command Syntax
mkdir [Option] [Directory]
The meaning of each option in the command is shown in the table:
Option | Description |
---|---|
-m<Permission Mode> |
Set permissions for newly created directories, the default permissions are 755 when there is no -m option |
-v | Display a message each time a new directory is created |
-p | It can be a path name. If some directories in the path do not exist yet, adding this option will automatically create those directories that do not exist yet, i.e. multiple directories can be created at once. |
Linux mkdir command Example
Create directory newdir1 with default permissions of 755
[root@rhel ~]# mkdir newdir1
Create the directory newdir2 with 777 permissions
[root@rhel ~]# mkdir -m 777 newdir2
Multiple directories can be created at once
mkdir -p
: A flag which enables the command to create parent directories as necessary. If the directories exist, no error is specified.
mkdir -p apidemos-aaa/bbb/ccc
Output: