mkdir Command Guide
The mkdir command creates directories. Learn how to create single or nested directory structures efficiently.
4 min read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
mkdir dirCreate directorymkdir d1 d2 d3Create multiplemkdir -p a/b/cCreate nested dirsOptions
-pCreate parents-vVerbose output-m 755Set permissionsCommon
mkdir -p src/{a,b,c}Brace expansionmkdir $(date +%Y%m%d)Date-named dirmkdir -p && cd $_Create and enterDownloadable Image Preview
Failed to generate preview
Basic Usage
Create a single directory in the current location.
bash
# Create a directory
mkdir new_directory
# Create multiple directories
mkdir dir1 dir2 dir3Common Options
mkdir Options
| -p | Create parent directories as needed |
| -v | Verbose output |
| -m mode | Set permissions (like chmod) |
Creating Nested Directories
Use -p to create parent directories.
bash
# Without -p: fails if parent doesn't exist
mkdir project/src/components # Error!
# With -p: creates all necessary parents
mkdir -p project/src/componentsTip
Always use
-p when creating nested directories or when you're not sure if the directory exists. It won't fail if the directory already exists.Setting Permissions
bash
# Create with specific permissions
mkdir -m 755 public_dir
mkdir -m 700 private_dir
# Create nested with permissions
mkdir -p -m 755 project/publicVerbose Output
bash
mkdir -v newdir
# Output: mkdir: created directory 'newdir'
mkdir -pv path/to/nested/dir
# Output:
# mkdir: created directory 'path'
# mkdir: created directory 'path/to'
# mkdir: created directory 'path/to/nested'
# mkdir: created directory 'path/to/nested/dir'Practical Examples
Create project structure
bash
mkdir -p project/{src,tests,docs,config}
# Creates:
# project/
# ├── src/
# ├── tests/
# ├── docs/
# └── config/Create deep nested structure
bash
mkdir -p src/{components/{ui,layout},utils,hooks,styles}
# Creates:
# src/
# ├── components/
# │ ├── ui/
# │ └── layout/
# ├── utils/
# ├── hooks/
# └── styles/Create dated backup directory
bash
mkdir -p backups/$(date +%Y/%m/%d)
# Creates: backups/2024/01/15/Create and enter directory
bash
mkdir new_project && cd new_project
# Or create a function in .bashrc
mkcd() {
mkdir -p "$1" && cd "$1"
}Create numbered directories
bash
mkdir chapter_{01..10}
# Creates: chapter_01, chapter_02, ... chapter_10Create temporary directory
bash
# Create temp directory with unique name
tmpdir=$(mktemp -d)
echo "Created: $tmpdir"
# Or specify a template
mktemp -d /tmp/myapp.XXXXXXmkdir vs install -d
The install command can also create directories:
bash
# Create directory with specific permissions and owner
install -d -m 755 -o user -g group /path/to/dirError Handling
bash
# Check if directory was created
mkdir new_dir && echo "Success" || echo "Failed"
# Create only if doesn't exist
[ -d "dir" ] || mkdir dir
# Using -p (no error if exists)
mkdir -p existing_dir # No errorSummary
mkdir is simple but essential. Key takeaways:
- Use
mkdir -pfor nested directories - Use
mkdir -mto set permissions - Use
mkdir -vfor verbose output - Use brace expansion for multiple directories
-pis safe - won't fail if directory exists