mkdir Command
The mkdir command creates directories (folders) in Linux and Unix systems. It's essential for organizing files and creating directory structures for projects, data storage, and system administration.
Syntax
mkdir [options] directory_name...
Description
The mkdir command creates one or more directories with the specified names. If a directory already exists, mkdir will report an error unless you use specific options to handle this situation.
Key behaviors:
- Creates directories with default permissions based on umask
- Can create multiple directories in one command
- Fails if parent directories don't exist (unless using -p)
- Requires write permission in the parent directory
Examples
Create a single directory
mkdir documents
Creates a directory named "documents" in the current location
Create multiple directories
mkdir projects downloads music
Creates three directories: projects, downloads, and music
Create nested directories
mkdir -p projects/web/frontend/src
Creates the entire directory path, including all parent directories
Create directory with specific permissions
mkdir -m 700 private_folder
Creates directory with read/write/execute for owner only (700)
Verbose directory creation
mkdir -pv backup/2025/january
# Output: mkdir: created directory 'backup'
# mkdir: created directory 'backup/2025'
# mkdir: created directory 'backup/2025/january'
# Output: mkdir: created directory 'backup'
# mkdir: created directory 'backup/2025'
# mkdir: created directory 'backup/2025/january'
Shows each directory as it's created
Create directories with spaces in names
mkdir "My Documents" 'Project Files'
Use quotes to create directories with spaces in their names
Common Use Cases
- Project Organization: Create directory structures for development projects
- Backup Systems: Organize backups by date or category
- Data Management: Separate different types of files and documents
- System Administration: Create log directories and configuration folders
- User Home Setup: Initialize directory structure for new users
💡 Tips and Best Practices
- Use -p for scripts: Prevents errors if directories already exist
- Meaningful names: Use descriptive directory names
- Consistent naming: Follow a naming convention (lowercase, underscores, etc.)
- Plan structure: Design directory hierarchy before creating
- Check permissions: Ensure appropriate access levels for security
⚠️ Common Errors
- "No such file or directory": Parent directory doesn't exist (use -p)
- "Permission denied": No write access to parent directory
- "File exists": Directory already exists (use -p to ignore)
- "Invalid argument": Invalid characters in directory name