The mkdir
(make directory) command creates new directories in Linux. This guide covers everything from basic usage to advanced directory creation techniques.
Basic Syntax
mkdir [options] directory_name(s)
Common Options
-p
: Create parent directories if they don’t exist-m
: Set directory permissions-v
: Print a message for each created directory-Z
: Set SELinux security context
Real-World Examples
1. Basic Directory Creation
# Create single directory
$ mkdir projects
# Create multiple directories
$ mkdir docs images videos
2. Create Nested Directories
# Create parent directories automatically
$ mkdir -p projects/website/src/components
# Create multiple nested directories
$ mkdir -p ~/projects/{web,mobile}/{src,docs,tests}
3. Set Permissions While Creating
# Create directory with specific permissions
$ mkdir -m 755 secure_folder
# Create public directory
$ mkdir -m 777 public_folder
Common Use Cases
-
Project Setup
# Create typical project structure mkdir -p project/{src,tests,docs,build}
-
Web Development
# Create web project structure mkdir -p website/{css,js,images,fonts}
-
System Administration
# Create backup directories mkdir -p /var/backups/$(date +%Y-%m-%d)
Tips and Tricks
-
Create Multiple Nested Directories
# Using brace expansion mkdir -p project/{src/{components,utils},tests/{unit,integration}}
-
Verbose Output
# See what's being created mkdir -pv deep/nested/directory/structure
-
Date-Based Directories
# Create year/month/day structure mkdir -p $(date +%Y/%m/%d)
Best Practices
-
Always Use -p for Scripts
# Prevents errors if directory exists mkdir -p "$directory_path"
-
Set Proper Permissions
# Create secure directory mkdir -m 700 ~/.ssh
-
Use Absolute Paths in Scripts
mkdir -p "/home/user/project/build"
Common Errors and Solutions
-
Permission Denied
# Solution: Use sudo if appropriate sudo mkdir /opt/application
-
File Exists
# Solution: Use -p flag mkdir -p existing_directory
-
No Such File or Directory
# Solution: Create parent directories mkdir -p /path/to/new/directory
Related Commands
rmdir
: Remove empty directoriesrm -r
: Remove directories and contentscp -r
: Copy directoriesmv
: Move/rename directories
Advanced Usage
1. Create with Template
# Create directory and copy template files
mkdir project && cp -r template/* project/
2. Create with Custom Permissions
# Create directory with specific user/group
sudo mkdir -m 755 /var/www/site
sudo chown www-data:www-data /var/www/site
3. Scripting Examples
#!/bin/bash
# Create backup structure
backup_dir="/backup/$(date +%Y-%m-%d)"
mkdir -p "${backup_dir}"/{system,user,database}
# Set permissions
chmod 700 "${backup_dir}"
Directory Structure Templates
1. Web Project
mkdir -p website/{
public/{css,js,images,fonts},
src/{components,layouts,pages},
docs,
tests
}
2. Application Development
mkdir -p app/{
src/{models,views,controllers},
tests/{unit,integration},
config,
docs,
scripts,
logs
}
3. Data Analysis Project
mkdir -p analysis/{
data/{raw,processed},
scripts,
notebooks,
results,
reports
}
Remember that proper directory structure is crucial for project organization. The mkdir
command, while simple, is essential for creating organized and maintainable file systems in Linux.