How to Create Directories in Linux Using the mkdir Command
Introduction
The mkdir
command enables the creation of directories in a file system. It's also known as make directory (mkdir
) and allows you to create directories and sub-directories to organize the file system structure.
This article explains how to use the mkdir
command in Linux to create and manage directories.
mkdir
Command Syntax
Below is the basic mkdir
command syntax:
$ mkdir [options] directory_name...
Within the above command, [options]
includes optional flags that modify the command behavior, and directory_name...
specifies the name of the directory to create. You can center multiple names separated by a comma ,
to create multiple directories simultaneously.
mkdir
Command Options
Option | Description |
---|---|
-p |
Creates parent directories. |
-m |
Sets permissions for the created directory. |
-v |
Displays a message for each newly created directory. |
Practical Examples of the mkdir
Command
Create a new directory.
console$ mkdir my_directory
The above command creates a new directory
my_directory
in your active working directory.Output:
Create multiple directories.
console$ mkdir dir1 dir2 dir3
The above command creates three new directories
dir1
,dir2
, anddir3
in the active working directory.Output:
Create a new directory with specific permissions.
console$ mkdir -m 755 dir4
The above command creates a new directory
dir4
with the755
permissions level that enables read, write, execute privileges for the owner, and read, execute privileges for the group and other system users.Output:
Create a parent directory.
console$ mkdir -p /path/to/parent/dir/new_directory
The above command creates a new directory
new_directory
within the/path/to/parent/dir
directory. If parent directory does not exist, it's created.Output:
Create a new directory and output the command progress.
console$ mkdir -v my_directory
The above command creates a new directory
my_directory
and displays the operation's progress.Output:
Create nested directories.
console$ mkdir -p project/{src,docs,tests}
The above command creates a nested directory structure with the sub-directories
src
,docs
, andtests
using the brace expansion method.Output:
Advanced Usage Scenarios
Create a new directory with specific permissions recursively.
console$ mkdir -m 755 -p /path/to/parent/dir/new_directory
The above command creates a new directory
new_directory
in/path/to/parent/dir
with the755
permission level to create the necessary parent directories.Output:
Create new directories with a sequential naming scheme.
console$ mkdir dir{1..5}
The above command creates five directories
dir1
,dir2
,dir3
,dir4
, anddir5
using the brace expansion method.Output:
Create a new directory with spaces in its name.
console$ mkdir "my directory"
The above command creates a new directory
my directory
with a space in its name.Output:
Create new directories with a specific date or timestamp.
console$ mkdir "$(date +%Y-%m-%d)"
The above command creates a new directory with the current system date.
Output:
Create new directories using a loop.
console$ for i in {1..5}; do mkdir "dir_$i"; done
The above command creates five directories
dir_1
,dir_2
,dir_3
,dir_4
, anddir_5
using a loop function.Output:
Create a new directory with specific permissions and ownership privileges.
console$ mkdir -m 770 /home/user/secure_data && sudo chown user:group /home/user/secure_data
The above command creates a new directory
secure_data
with the770
permissions level with ownership privileges touser
andgroup
.Output:
Create new directories and sub-directories with detailed messages.
console$ mkdir -pv projects/{frontend,backend}/{src,dist}
The above command creates a complex directory structure with detailed messages for each new directory.
Output:
Conclusion
You have used the mkdir
command to perform directory creation and management tasks. For more command options, run the man mkdir
command to view the mkdir
manual page.