How to Create a Directory in Linux Operating System

In Linux mkdir command use to create directory in Linux. If no path specified new  directory will be created inside the current working directory.

mkdir dir1

To create a directory in a different location, we need to specify the absolute path of the directory.

mkdir /var/folder1

New Folder called folder1 will be created inside the /var directory.

How to Create a Directory in Linux using mkdir command

 

Wrap the name inside quotation marks If the directory name contain spaces, otherwise mkdir command will make separate folders.

Example

mkdir 'My Documents'

As per the above Example, I gave directory name inside single quotes since the folder name contain spaces. If I did not use quotes mkdir command will create two separate folders called My and Documents.

Create Parent Directory

Use -p option to create parent directory (one or more) If the parent directory do not exist already. For Example, let's think I want to create a directory called folder1 inside the /var/documents folder, But the parent folder documents do not exist.

mkdir -p /var/documents/folder1

If the parent directory documents do not exist, mkdir command will create it before creating the folder1.

Crate More Than One Directory

Linux mkdir command can make more that one folder at the same time. You can specify multiple directories by separating them using a space.

mkdir folder1 folder2 folder3

Three new folders will be created folder1, folder2 and folder3.

Create Multiple Directories With Wildcard

Example

mkdir dir{1,2,5}

Create Three new Directories dir1, dir2 and dir5.

mkdir dir{1..10}

This time we have specified range (1 to 10) inside the brace expressions. mkdir command will make 10 directories dir1,dir2,....,dir10.

Create Multiple Directories linux mkdir command

Make Directory with File Permission

Another useful mkdir command options is -m option or --mode option, which allows us to specify file permission for the new directory.

mkdir -m 777 dir1

The Linux mkdir command will make a new directory called dir1 with 777 file permissions.

mkdir mode set permission

mkdir Command Examples

mkdir dir1

Create a directory Called dir1

mkdir dir1 dir1 dir3

Create Three Directories dir1, dir2 and dir3.

mkdir -p /server/backup/mysql

Make a folder called mysql inside /server/backup directory. Parent Directories will be created if not exist already.

Questions

How to Create a Directory in Linux Operating System?

The Linux mkdir command uses to create Directory in Linux Operating System.

Which Command Option make parent Directories if not Exist?

Command Option -p use to make parent directories if needed.