last modified March 3, 2025
The mkdir command in Linux is used to create directories. It is a
fundamental tool for organizing files and directories in the file system. This
tutorial covers basic and advanced usage of mkdir with practical
examples.
mkdir is commonly used for creating single directories, nested
directories, and setting permissions during creation.
This example demonstrates how to create a single directory.
mkdir mydir
The mkdir command creates a directory named mydir in
the current working directory.
This example shows how to create multiple directories at once.
mkdir dir1 dir2 dir3
The mkdir command creates three directories: dir1,
dir2, and dir3.
This example demonstrates how to create nested directories.
mkdir -p parent/child/grandchild
The -p option creates parent directories as needed. This command
creates a nested directory structure: parent/child/grandchild.
This example shows how to set permissions while creating a directory.
mkdir -m 755 secure_dir
The -m option sets the directory permissions to 755,
which means read, write, and execute for the owner, and read and execute for
others.
This example demonstrates how to create directories with spaces in their names.
mkdir "My Documents"
Use quotes to create directories with spaces, such as My Documents.
This example shows how to use verbose mode to display details of directory creation.
mkdir -v logs
The -v option prints a message for each directory created. In this
case, it confirms the creation of logs.
-p for Nested Directories: Avoid errors by creating parent directories automatically.-m to set appropriate permissions during creation.-v to confirm directory creation.
In this article, we have explored various examples of using the mkdir
command for creating directories, including advanced features like nested
directories and setting permissions.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Linux tutorials.