The Linux mkdir command is used to create a directory with the specified name. It requires the user creating the directory to have write permission in the current directory, and the specified directory name cannot be an existing directory in the current directory.
1. Command Format
mkdir [option] directory
2. Command function
The mkdir command enables you to create a folder or directory named with DirName (the specified file name) in the specified location. The user who wants to create the folder or directory must have write access to the parent folder of the created folder.
Also, the created folder (directory) cannot have the same name as the file name in its parent directory (i.e., parent folder), i.e., it cannot have the same name in the same directory (case-sensitive).
3. Command parameters
-m, --mode=mode, set permissions <mode> (similar to chmod) instead of rwxrwxrwx minus umask
-p, --parents can be a path name. If some directories in the path do not exist yet, adding this option will automatically create those that do not exist yet, i.e. multiple directories can be created at once.
-v, --verbose show information every time a new directory is created
--help displays this help and exits
--version output version information and exit`
4. Command examples
Example 1: Create an empty directory
Command.
mkdir test1
Output.
[root@localhost soft]# cd test
[root@localhost test]# mkdir test1
[root@localhost test]# ll
Total 4
drwxr-xr-x 2 root root 4096 10-25 17:42 test1
[root@localhost test]#
Example 2: Recursively create multiple directories
Command.
mkdir -p test2/test22
Output.
[root@localhost test]# mkdir -p test2/test22
[root@localhost test]# ll
Total 8
drwxr-xr-x 2 root root 4096 10-25 17:42 test1
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
[root@localhost test]# cd test2/
[root@localhost test2]# ll
Total 4
drwxr-xr-x 2 root root 4096 10-25 17:44 test22
[root@localhost test2]#
Example 3: Create a directory with permission 777
Command.
mkdir -m 777 test3
Output.
[root@localhost test]# mkdir -m 777 test3
[root@localhost test]# ll
Total 12
drwxr-xr-x 2 root root 4096 10-25 17:42 test1
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
[root@localhost test]#
Note: test3’s permissions are rwxrwxrwx
Example 4: Create a new directory and display the information
Command.
mkdir -v test4
Output.
[root@localhost test]# mkdir -v test4
mkdir: created directory "test4"
[root@localhost test]# mkdir -vp test5/test5-1
mkdir: directory "test5" created
mkdir: directory "test5/test5-1" created
[root@localhost test]#
Example 5: A command to create the directory structure of a project
Reference: http://www.ibm.com/developerworks/cn/aix/library/au-badunixhabits.html
Command.
mkdir -vp scf/{lib/,bin/,doc/{info,product},logs/{info,product},service/deploy/{info,product}}
Output.
[root@localhost test]# mkdir -vp scf/{lib/,bin/,doc/{info,product},logs/{info,product},service/deploy/{info,product}}
mkdir: Created directory "scf"
mkdir: directory "scf/lib" created
mkdir: directory "scf/bin" created
mkdir: directory "scf/doc" created
mkdir: directory "scf/doc/info" created
mkdir: directory "scf/doc/product" created
mkdir: directory "scf/logs" created
mkdir: directory "scf/logs/info" created
mkdir: directory "scf/logs/product" created
mkdir: directory "scf/service" created
mkdir: directory "scf/service/deploy" created
mkdir: directory "scf/service/deploy/info" created
mkdir: directory "scf/service/deploy/product" created
[root@localhost test]# tree scf/
scf/
|-- lib
|-- bin
|-- doc
|-- info
|-- product
|-- logs
|-- info
-- product
|-- service
|-- deploy
|-- info
|-- product
12 directories, 0 files
[root@localhost test]#
Reference