The mkdir
command in Linux stands for “make directory”. It is used to create new directories (folders) in the filesystem. With mkdir
, you can create single directories, multiple directories, or even nested directory structures in one go. The command is highly flexible, allowing you to set permissions, handle directory names with spaces or special characters, and create parent directories automatically if they don’t exist.
Whether you’re organizing files, setting up project structures, or writing scripts, mkdir
is an essential tool for managing directories efficiently in Linux.
Create a Single Directory
mkdir dirname
Creates a directory named dirname
in the current location.
Create Multiple Directories
mkdir dir1 dir2 dir3
Creates three directories (dir1
, dir2
, dir3
) in the current location.
Create Nested Directories
mkdir -p parent/child/grandchild
- The
-p
flag creates parent directories if they don’t exist. - Creates the full path
parent/child/grandchild
.
Set Permissions While Creating
mkdir -m 755 secure_dir
-m
sets directory permissions (e.g.,755
=rwxr-xr-x
).
Create Directories with Spaces
mkdir "New Folder"
Use quotes to create directories with spaces in their names.
Create Directories with Special Characters
mkdir dir\#name # Backslash escapes special characters
mkdir 'dir$name' # Quotes handle special characters
Works for names containing $
, #
, !
, etc.
Verbose Mode (Show Confirmation)
mkdir -v logs
-v
prints a message after creating the directory:mkdir: created directory 'logs'
.
Create Directories with a Timestamp
mkdir "backup_$(date +%Y%m%d)"
Creates a directory like backup_20250301
.
Create Multiple Nested Directories
mkdir -p project/{src,dist,test}
- Uses brace expansion to create:
project/src
,project/dist
,project/test
.
Create Directories from a List in a File
xargs mkdir -p < directories.txt
- Reads directory names from
directories.txt
and creates them.
Set Default Permissions Using umask
umask 022 && mkdir new_dir
umask 022
ensures new directories have755
permissions by default.
Create Temporary Directories
mkdir -p /tmp/mytempdir
Useful for scripts that need short-lived directories.
Combine with &&
for Scripts
mkdir -p /opt/app/config && cd "$_"
- Creates
/opt/app/config
and navigates into it usingcd "$_"
.
Create Hidden Directories
mkdir .hidden_dir
Hidden directories start with a .
(e.g., .config
).
Create Directories with Specific Ownership
sudo mkdir /var/www/site && sudo chown www-data:www-data /var/www/site
Creates a directory and sets ownership for web server use.
Key Notes:
- Use
-p
(parents) to avoid errors when directories already exist. - Always quote directory names with spaces or special characters.
- Use
ls -l
to verify directory creation and permissions.