mkdir command in Linux with Examples



Name

mkdir - make directories

Synopsis

mkdir [OPTION]... DIRECTORY...

Description

The command mkdir stands for “make directory”. It creates each directory specifed on the command line in the order given. It reports an error if DIRECTORY already exists, unless the -p option is given.

The mkdir command in Linux/Unix allows users to create or make new directories (also referred to as folders in some operating systems).

This command creates the DIRECTORY(ies), if they do not already exist. This command can create multiple directories at once as well as set the permissions for the directories (folders).

It is important to note that for executing this command, users must have the right permissions to create a directory in the parent directory, otherwise the users would receive a ‘permission denied’ error.

Options

TagDescription
-m, --mode=MODEset file mode (as in chmod), not a=rwx - umask
-p, --parentsno error if existing, make parent directories as needed
-v, --verboseprint a message for each created directory
-Zset SELinux security context of each created directory to the default type
--context[=CTX]like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX
--helpdisplay this help and exit
--versionoutput version information and exit

Examples

Example-1

Create a simple directory at current folder/directory

$ mkdir book_titles

output:

$ ls
book_titles

Example-2

Create the directory at Home

$ mkdir ~/examples

output:

$ cd ~
$ ls
examples

Example-3

Create the directory at desired location

$ mkdir /tmp/examples

output:

$ cd /tmp
$ ls
examples

Example-4

Create more than one directories using single command

$ mkdir book_titles/history book_titles/social_science

output:

$ cd book_titles
$ ls -R
.:
history  social_science

./history:

./social_science:

Example-5

Create multiple directories using special characters

$ mkdir book_titles/{maths,science,biology}

output:

$ cd book_titles
$ ls -R
.:
biology  history  maths  science  social_science

./biology:

./history:

./maths:

./science:

./social_science:

Example-6

Create directory and to set the file permissions

(a) Using file attributes

$ mkdir -m a=rwx book_titles

output:

$ cd book_titles
$ ls
drwxrwxrwx 2 expert expert 4096 Sep 28 20:45 book_names/

(b) Using numeric values

$ mkdir -m 777 book_names

output:

$ cd book_titles
$ ls
drwxrwxrwx  2 expert expert 4096 Sep 28 20:48 book_names/

Example-7

Create parent directories if they do not already exist. It sets their file permission bits to the umask modified by ‘u+wx’.

(a) Simple example

$ mkdir -p vegitables/roots/carrot

output:

$ cd vegitables
$ ls -R
vegitables/:
roots

vegitables/roots:
carrot

vegitables/roots/carrot:

(b) Hard example

$ mkdir -p book_titles/{biology/{botany,zoology},english/{literature,grammer},geography,maths/{algebra,trigonometry,statistics}}

output:

$ cd book_titles
$ ls -R
.:
biology  english  geography  history  maths  science  social_science

./biology:
botony  zoology

./biology/botony:

./biology/zoology:

./english:
grammer  litrerature

./english/grammer:

./english/litrerature:

./geography:

./history:

./maths:
algebra  statistics  trigonometry

./maths/algebra:

./maths/statistics:

./maths/trigonometry:

./science:

./social_science:

Advertisements