Bash Mkdir Command
Create directories and folder structures
📁 What is the Mkdir Command?
The mkdir command (make directory) creates new directories and folder structures in your file system. It's essential for organizing files, setting up projects, and building directory hierarchies quickly and efficiently from the command line interface.
# Create a directory
mkdir myfolder
Common Mkdir Operations
Single Directory
Create one folder
mkdir folder
Multiple Directories
Create many folders at once
mkdir dir1 dir2 dir3
Nested Directories
Create parent and child folders
mkdir -p parent/child
Set Permissions
Create with specific access rights
mkdir -m 755 folder
🔹 Create a Single Directory
The basic mkdir command creates individual directories in current locations or specified paths. Directories receive default permissions based on system umask settings. This fundamental operation organizes files into logical groups, establishes project structures, and maintains systematic file organization. It's among the most essential commands for effective file system management.
# Create directory in current location
mkdir myfolder
# Create directory with full path
mkdir /home/user/documents/projects
# Create directory with spaces (use quotes)
mkdir "My Documents"
Result:
Directory 'myfolder' created (No output means successful creation)
🔹 Create Multiple Directories
Multiple directories can be created simultaneously by listing names in single commands. For example, mkdir docs images scripts creates three separate directories. This batch approach saves time when establishing project structures, organizing files into categories, or setting up systematic directory hierarchies for various applications and workflows.
# Create multiple directories at once
mkdir folder1 folder2 folder3
# Create project structure
mkdir src docs tests assets
# Create directories with similar names
mkdir project_{dev,test,prod}
Result:
folder1/ created folder2/ created folder3/ created
🔹 Create Nested Directories
The -p option creates parent directories automatically when they don't exist. This allows building complete directory trees with single commands like mkdir -p project/{src,doc,test}. Without -p, mkdir fails if parent directories are missing. This capability is invaluable for quickly establishing complex project structures with multiple organizational levels.
# Create nested directory structure
mkdir -p parent/child/grandchild
# Create complex project structure
mkdir -p project/src/components/ui
# Create multiple nested paths
mkdir -p docs/{api,guides,tutorials}
# Create deep hierarchy
mkdir -p app/src/main/java/com/example/project
How -p works:
- Creates all missing parent directories
- No error if directory already exists
- Essential for building directory trees
- Safe to use in scripts
🔹 Set Directory Permissions
The -m option sets specific permissions during directory creation using octal notation. Values like 755 (rwxr-xr-x) or 700 (rwx------) control read, write, and execute access. This is crucial for security-sensitive directories, shared environments, and compliance scenarios requiring precise access control from creation onward.
# Create directory with specific permissions
mkdir -m 755 public_folder
# Create private directory (owner only)
mkdir -m 700 private_data
# Create read-only directory
mkdir -m 555 readonly_folder
# Combine with -p for nested directories
mkdir -p -m 750 secure/data/files
Common Permission Modes:
- 755 - Owner: full, Others: read+execute
- 750 - Owner: full, Group: read+execute, Others: none
- 700 - Owner only (private)
- 777 - Everyone: full access (not recommended)
🔹 Verbose Output
The -v option displays confirmation messages for each created directory. This verbose output verifies successful operations, particularly when creating multiple directories or running automated scripts. It provides immediate feedback and eliminates manual verification, enhancing workflow efficiency and script reliability.
# Show creation messages
mkdir -v newfolder
# Verbose with multiple directories
mkdir -v dir1 dir2 dir3
# Verbose with nested structure
mkdir -pv parent/child/grandchild
Output:
mkdir: created directory 'newfolder' mkdir: created directory 'dir1' mkdir: created directory 'dir2' mkdir: created directory 'dir3'
🔹 Useful Mkdir Options
Mkdir provides several options to customize directory creation for various scenarios. Beyond -p and -v, options handle permission setting, existing directory handling, and verification processes. These flags make mkdir versatile for both interactive use and automated scripting, accommodating diverse directory creation requirements across different environments.
Common Options:
- -p : Create parent directories as needed
- -m : Set permission mode (like chmod)
- -v : Verbose output (show what's created)
- -Z : Set SELinux security context
# Combine multiple options
mkdir -pv parent/child
# Create with permissions and verbose
mkdir -m 755 -v public_folder
# Safe directory creation (no error if exists)
mkdir -p existing_folder
# Create multiple nested structures
mkdir -p {project1,project2}/{src,docs,tests}
🔹 Practical Examples
Everyday uses of head include validating data files, monitoring log heads, and creating data samples. A data scientist might run head -1000 large_dataset.csv > sample.csv to create a manageable sample for testing scripts. A developer might use head -20 application.log to check recent activity after a deployment. These scenarios highlight head's role as a gatekeeper, providing a safe, controlled way to interact with the beginning of files and streams, which is often where the most current or defining information resides.
# Create web project structure
mkdir -p website/{css,js,images,fonts}
# Setup development environment
mkdir -p ~/projects/{personal,work,learning}
# Create date-based backup folders
mkdir -p backups/$(date +%Y)/$(date +%m)
# Organize media files
mkdir -p media/{photos,videos,music}/{2023,2024}
# Create application directory structure
mkdir -p app/{src/{components,utils,api},public,tests}
# Setup log directories
mkdir -p /var/log/myapp/{access,error,debug}
# Create user directories
mkdir -p ~/Documents ~/Downloads ~/Pictures ~/Videos
🔹 Error Handling
Understanding common mkdir errors facilitates quick troubleshooting and robust script development. Typical issues include existing directories, missing parent paths, and permission denials. Using appropriate options like -p prevents many errors, while proper error checking in scripts ensures reliable directory creation across different environments and conditions.
Common Errors:
- Directory exists - Use -p to ignore
- Permission denied - Need write access to parent
- No such file or directory - Parent missing, use -p
- Invalid argument - Check for special characters
# Avoid error if directory exists
mkdir -p existing_folder
# Check if directory was created
if mkdir newfolder; then
echo "Directory created successfully"
else
echo "Failed to create directory"
fi