Linux mkdir Command Help and Examples

Linux mkdir Command Help and Examples

Linux mkdir Command Help and Examples :How do I create a directory on Linux/Unix? How do I create a directory with specific permissions? Creating directory structure using Linux mkdir command? Examples of mkdir command.

Linux/Unix mkdir or “make directory” command is used for creating directories/folders on the filesystem. This is a simple and frequently used Linux command. Every system admin must know these options.

Syntax:

mkdir command uses following syntax with few options.

$ mkdir <directory name>

How To Create A New Directory

Use mkdir followed by new directory name to create directory in current location.

$ mkdir folder1

Create Multiple Directories

Creating multiple directories in current directory in single command. Below command will create three directories folder1, folder2, folder3 in current directory.

$ mkdir folder1 folder2 folder3

Create Directory at Specific Location

Create directory to other location without changing current directory. Type full path of directory like below.

$ mkdir /var/www/folder1/public

Create Directory with Intermediate Directory

The above command will failed with mesage “mkdir: cannot create directory `/var/www/folder1/public’: No such file or directory”, if any of intermediate directory does not exist. In that situation use -p option to tell mkdir to create intermediate directories if not exists.

$ mkdir -p /var/www/folder1/public

Create A Directory with Specific Permission

During creation of directories, you can set specific permissions using -m option. This is similiar to use chmod command.

$ mkdir -m 644 folder1
$ mkdir -m a=rwx folder1
$ mkdir -m u=rw,g=r,o=r folder1

Create Multiple Directories at Specific Location

This is very lesser known option for creating multiple directories on different location with single command. This will create directories named one, two, three in folder1 directory.

$ mkdir -p folder1/{one|two|three}
 
Enjoyed this video?
Linux mkdir Command Help and Examples
"No Thanks. Please Close This Box!"