Mkdir command linux directory creation
Command definition
The mkdir
command in Linux is used to create new directories. It allows users to organize files and folders in a hierarchical structure, making it easier to manage and locate data. The command can create single or multiple directories and can also set permissions for the new directories.
Usage examples
-
Create a single directory: This command creates a new directory named
myfolder
.
bash mkdir myfolder
-
Create multiple directories: This command creates three directories named
folder1
,folder2
, andfolder3
in one go.
bash mkdir folder1 folder2 folder3
-
Create a nested directory: This command creates a directory structure where
myfolder
contains a subdirectory namedsubfolder
.
bash mkdir -p myfolder/subfolder
-
Set permissions while creating a directory: This command creates a directory with specific permissions (e.g., 755).
bash mkdir -m 755 myfolder
-
Create a directory with a specific owner: This command creates a directory and sets the owner to a specified user (requires superuser privileges).
bash sudo mkdir myfolder
-
Create directories with spaces in the name: This command creates a directory with a name that includes spaces.
bash mkdir "my folder"
-
Create directories using a variable: This command creates a directory using a shell variable for the directory name.
bash dir_name=myfolder; mkdir $dir_name
-
Create multiple nested directories: This command creates multiple nested directories in one command.
bash mkdir -p myfolder/subfolder1/subfolder2
-
Check if a directory exists before creating: This command checks if
myfolder
exists and creates it only if it does not.
bash [ ! -d myfolder ] && mkdir myfolder
-
Create a directory and change to it: This command creates a new directory and immediately changes the shell to that directory.
bash mkdir myfolder && cd myfolder