Terminal GuideTerminal Guide

mkdir Command Guide

The mkdir command creates directories. Learn how to create single or nested directory structures efficiently.

4 min readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

mkdir dirCreate directory
mkdir d1 d2 d3Create multiple
mkdir -p a/b/cCreate nested dirs

Options

-pCreate parents
-vVerbose output
-m 755Set permissions

Common

mkdir -p src/{a,b,c}Brace expansion
mkdir $(date +%Y%m%d)Date-named dir
mkdir -p && cd $_Create and enter

Downloadable 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 dir3

Common Options

mkdir Options

-pCreate parent directories as needed
-vVerbose output
-m modeSet 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/components
Tip
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/public

Verbose 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_10

Create temporary directory

bash
# Create temp directory with unique name
tmpdir=$(mktemp -d)
echo "Created: $tmpdir"

# Or specify a template
mktemp -d /tmp/myapp.XXXXXX

mkdir 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/dir

Error 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 error

Summary

mkdir is simple but essential. Key takeaways:

  • Use mkdir -p for nested directories
  • Use mkdir -m to set permissions
  • Use mkdir -v for verbose output
  • Use brace expansion for multiple directories
  • -p is safe - won't fail if directory exists

Related Articles