Linux Commands – mkdir
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed cd command in Linux which is used to change the current working directory in Linux.
https://cloudaffaire.com/linux-commands-cd/
In this blog post, we will discuss mkdir command in Linux. mkdir stands for make directory and is used to create a new directory. You can create a single, multiple, or multilevel directory using mkdir command.
Linux Commands – mkdir:
You can create a new directory simply using mkdir <directory_name>. Multiple directory can also be created at once using mkdir <mydir1> <mydir2> .. <mydirN> or mkdir mydir{1..N}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
############################ ## Linux Commands | mkdir ## ############################ ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##------ ## mkdir ##------ ## mkdir [options] [dir] mkdir mydir1 ## creates a directory names mydir1 ls -l ## returns drwxrwxr-x. 2 user user 6 May 12 12:10 mydir1 mkdir mydir2 mydir3 ## or mkdir mydir{2,3} whihc using parameter expansion {} ls ## returns mydir1 mydir2 mydir3 rm -r my* ## removes all directory with name staring with my |
You need to have sufficient permission in the parent directory to create a directory.
1 |
mkdir /mydir ## cannot create directory ‘/mydir’: Permission denied |
You can use -v or –verbose option with mkdir command to print a message each time a directory is been created.
1 2 3 4 5 6 7 8 9 |
mkdir -v mydir{1..3} ## returns ## mkdir: created directory ‘mydir1’ ## mkdir: created directory ‘mydir2’ ## mkdir: created directory ‘mydir3’ ls ## returns mydir1 mydir2 mydir3 rm -r my* |
You can use mkdir -p or –parents option with mkdir to create a parent directory while creating a child directory. If the parent directory does not exist it will create the parent directory.
1 2 3 4 5 6 |
mkdir mydir1/mydir2/mysubsir1 ## error as parent directory does not exist mkdir -p mydir1/mydir2/mysubsir1 ## creates mydir1/mydir2/mysubsir1 ls -R mkdir mydir1/mydir2/mysubsir2 ## as parent directory exist no -p option required ls -R rm -r my* |
You can also create complex directory structure using combination of -p and parameter expansion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
mkdir -p mydir1/{mydir21/mydir211/{mydir2111,mydir2112},mydir11/mydir111/mydir1111} ## above command create below directory structure ## . ## └── mydir1 ## ├── mydir11 ## │ └── mydir111 ## │ └── mydir1111 ## └── mydir21 ## └── mydir211 ## ├── mydir2111 ## └── mydir2112 rm -r my* |
You can use -m or –mode=MODE option with mkdir command to set file mode (as in chmod).
1 2 3 4 5 6 7 |
mkdir mydir1 ## default mode mkdir -m a=rwx mydir2 ## or mkdir -m 777 mydir2 ls -l ## returns ## drwxrwxr-x. 2 user user 6 May 12 13:04 mydir1 ## drwxrwxrwx. 2 user user 6 May 12 13:04 mydir2 |
Hope you have enjoyed this article. In the next blog post, we will discuss rm command in Linux.
Hi
Good and very useful information about mkdir command. Every option is explained well and easy to understand.