This tutorial explains Linux “mkdir” command, options and its usage with examples.
Usage:
mkdir [OPTION]… DIRECTORY…
Create the DIRECTORY(ies), if they do not already exist.
Here’s the listing of example usage of “mkdir” command:
1. To create directory(mkdir dir_name):
sanfoundry-> mkdir sample sanfoundry-> ls sample
2. To create multiple directories(mkdir dir1_name di2_name dir3_name):
sanfoundry-> mkdir folder1 folder2 folder3 sanfoundry-> ls folder1 folder2 folder3 sample
3. To control the permissions of new directories(mkdir -m permissions directory_name):
New directories are by default created with the read, write and execute (i.e., run as a program if a program) permissions enabled for the owner and group and the read and execute permissions enabled for other users. Thus, for example, to create a directory named sample for which all three types of permissions were enabled for all users use:
sanfoundry-> mkdir -m 777 sample sanfoundry-> ls -l total 4 drwxrwxrwx 2 himanshu himanshu 4096 Jul 7 19:31 sample
4. To create specified intermediate directories for a new directory if they do not already exist(mkdir -p path):
sanfoundry-> mkdir folder1/folder2/sample mkdir: cannot create directory `folder1/folder2/sample': No such file or directory sanfoundry-> mkdir -p folder1/folder2/sample sanfoundry-> cd folder1/folder2/ sanfoundry-> ls sample
5. To show verbose message(mkdir -v directory_name):
sanfoundry-> mkdir -pv folder1/folder2/sample mkdir: created directory `folder1' mkdir: created directory `folder1/folder2' mkdir: created directory `folder1/folder2/sample' sanfoundry-> mkdir -v fol1 fol2 fol3 mkdir: created directory `fol1' mkdir: created directory `fol2' mkdir: created directory `fol3'
6. To know the version(mkdir –version):
sanfoundry-> mkdir --version mkdir (GNU coreutils) 8.13 Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
More options
"-Z" : set the SELinux security context of each created directory to CTX