mkdir

mkdir linux command cheatsheet by Thamizhiniyan C S

Introduction

mkdir ( make directory ) is used to create directory(ies), if they don't exist already.


Syntax

mkdir [OPTION]... DIRECTORY...


Important Flags

FlagDescription

-p

Creates parent directories if they don't exist.

-m a=[rwx] [dir_name]

Sets the file modes, i.e., permissions, for the new directory.

-v

Displays a message for each created directory.

--version

Displays the version number and information about the license and exits.

-Z

Sets the SELinux security context for directories.

-help

Displays help with information about mkdir options.


Examples

CommandDescription
mkdir Linux

To create a single directory named Linux

mkdir "My Documents"

To create a directory with spaces in its name, enclose the entire directory name in quotes.

mkdir /tmp/example

To create a directory or directories in a specific location other than your current working directory, specify the full directory path.

mkdir my_folder/sub_folder

This command creates a directory structure with “my_folder” as the parent directory and “sub_folder” as its subdirectory.

mkdir dir1 dir2 dir3

To create multiple directories at once, list the directory names separated by a space.

mkdir {test1,test2,test3}

Use mkdir with curly brackets to create multiple directories without spaces.

mkdir dir{1..15}

Create a batch of directories starting with the same pattern, from dir1 to dir15.

mkdir $USER

mkdir allows you to use environment variables in directory names. This example creates a directory with the current user being the directory name.

mkdir –p Linux/dirtest1/dirtest2

To build a structure with multiple subdirectories. This ensures that mkdir adds any missing parent directories in the process. If the directories exist, no error is specified.

mkdir -p first/second/third

If the first and second directories do not exist, due to the -p option, mkdir will create these directories. If the -p option is not specified, an error is returned.

mkdir –m 777 DirM

To add all permissions for all users when creating a directory, specify the -m option with 777.

mkdir -m a=rwx [directories]

This option is used to set the file modes, i.e. permissions, for the created directories.

mkdir -m 755 public

Use the ‘-m’ option to set specific permissions for a directory. This command sets read, write, and execute permissions for the owner, and read and execute permissions for others.

mkdir -v [directories]

To see the details of the mkdir process. It displays a message for every directory created.

mkdir --help

It displays help-related information and exits.

mkdir --version

It displays the version number, some information regarding the license and exits.

Last updated