LFCS Phase 1 Part 23: Creating and Managing Directories with mkdir

Master the mkdir command for creating directories in Linux. Learn the -p flag for parent directories, setting permissions, organizing directory structures, and best practices for LFCS certification.

25 min read

Imagine you're organizing a new project and need to create a directory structure like project/src/components/ all at once. Or maybe you need to create dozens of dated backup directories. The mkdir command is your essential tool for creating and organizing directories in Linuxβ€”and understanding it thoroughly is fundamental for both system administration and the LFCS exam.

The mkdir (make directory) command creates new directories with a single command. Unlike creating files with touch, directories serve as containers for organizing your filesystem, and mkdir provides powerful options to create complex directory structures efficiently.

πŸ’‘

🎯 What You'll Learn:

  • Basic mkdir syntax for creating directories
  • Understanding directory permissions and how they're set
  • Using -p flag to create parent directories automatically
  • Verbose mode with -v for operation confirmation
  • Setting specific permissions during creation with -m
  • Creating multiple directories at once
  • Using wildcards and brace expansion with mkdir
  • Directory naming conventions and best practices
  • Understanding directory vs file differences
  • Common errors and how to fix them
  • Real-world directory organization patterns

Series: LFCS Certification - Phase 1 (Post 23 of 52)

Previous Post: Part 22: Copying Files with cp Command

Next Post: Part 24: Understanding Absolute vs Relative Paths


Understanding Directories

Before diving into mkdir, let's understand what directories are and how they differ from files.

What is a Directory?

A directory (also called a folder) is a special type of file that contains references to other files and directories. It's a container for organizing your filesystem.

Key characteristics:

  • Directories organize files into a hierarchical structure
  • Directories can contain files AND other directories (subdirectories)
  • Directories have permissions just like files
  • Directories show as d in file listings (not -)

Directory vs File

AspectFileDirectory
PurposeStores data/contentOrganizes files and directories
Type indicator- (dash)d (directory)
Can containText, binary data, nothingFiles, subdirectories
Default permissions644 (rw-r--r--)755 (rwxr-xr-x)
Execute permissionRun as program/scriptEnter/access the directory
Read permissionView file contentsList directory contents
Write permissionModify file contentsCreate/delete files inside

Basic mkdir Syntax

The mkdir command creates new directories.

Syntax

mkdir [OPTIONS] DIRECTORY_NAME

Components:

  • mkdir: Make directory command
  • OPTIONS: Flags like -p, -v, -m
  • DIRECTORY_NAME: Name(s) of directory to create

Example 1: Creating a Simple Directory

# Navigate to /tmp for testing
cd /tmp
pwd

Output:

/tmp
# Create a directory
mkdir data

No output means success! Now verify:

# List the directory (shows contents)
ls -l data

Output:

total 0

The directory exists but is empty.

# List directory itself (shows permissions and metadata)
ls -ld data

Output:

drwxr-xr-x. 2 centos9 centos9 6 Oct 31 19:23 data

Breakdown:

  • d - This is a directory (not a file)
  • rwxr-xr-x - Permissions (owner can read/write/execute, others can read/execute)
  • 2 - Link count
  • centos9 centos9 - Owner and group
  • 6 - Size in bytes (minimal for empty directory)
  • Oct 31 19:23 - Creation timestamp
  • data - Directory name
πŸ’‘

πŸ’‘ Understanding the "d" prefix: In Linux, everything has a type indicator in listings:

  • - = regular file
  • d = directory
  • l = symbolic link
  • c = character device
  • b = block device

Permission Denied Errors

You need write permission in the parent directory to create subdirectories.

Example: Cannot Create in Root Directory

# Try to create directory in / (root filesystem)
mkdir /files

Output:

mkdir: cannot create directory '/files': Permission denied

Why?

  • / (root directory) is owned by root
  • Regular users don't have write permission in /
  • Only root can create directories there

Fix:

# Use sudo if you really need to create there
sudo mkdir /files

# Or create in a location where you have permissions
mkdir ~/files
mkdir /tmp/files

The Parent Directory Problem

One of the most common mkdir challenges is creating nested directories.

Example: Missing Parent Directory

# Try to create files/data (but 'files' doesn't exist)
mkdir files/data

Output:

mkdir: cannot create directory 'files/data': No such file or directory

What happened?

  • You're trying to create data inside files
  • But files doesn't exist yet!
  • mkdir won't create parent directories by default

Manual Solution:

# Create parent first
mkdir files

# Then create child
mkdir files/data

# Verify
ls -ld files/data

Output:

drwxr-xr-x. 2 centos9 centos9 6 Oct 31 19:25 files/data

This works, but it's tedious for deep structures!


The -p Flag: Create Parent Directories

The -p (parents) flag automatically creates parent directories as needed.

Example: Create Nested Structure

# Create nested structure in one command
mkdir -p project/src/components

No error! Let's verify:

# See the full structure
ls -R project/

Output:

project/:
src

project/src:
components

project/src/components:

What -p did:

  1. Checked if project exists β†’ Created it
  2. Checked if project/src exists β†’ Created it
  3. Created project/src/components

Multiple Levels Example

# Create very deep structure
mkdir -p website/assets/images/logos/svg

# Verify
ls -R website/

Output:

website/:
assets

website/assets:
images

website/assets/images:
logos

website/assets/images/logos:
svg

website/assets/images/logos/svg:
βœ…

βœ… Best Practice: Always use -p when creating nested directories. It's safer (won't error if parent exists) and more convenient.

# Good: Always works
mkdir -p /tmp/backup/2024/december

# Bad: Fails if parents don't exist
mkdir /tmp/backup/2024/december

Verbose Mode with -v

The -v (verbose) flag shows what mkdir is creating.

Example: Verbose Output

# Create with verbose mode
mkdir -pv logs/2024/december

Output:

mkdir: created directory 'logs'
mkdir: created directory 'logs/2024'
mkdir: created directory 'logs/2024/december'

What you see:

  • Each directory created is listed
  • Useful for debugging and verification
  • Great for scripts and automation

Combining -p and -v

# Create multiple nested structures verbosely
mkdir -pv backups/{daily,weekly,monthly}/2024

Output:

mkdir: created directory 'backups'
mkdir: created directory 'backups/daily'
mkdir: created directory 'backups/daily/2024'
mkdir: created directory 'backups/weekly'
mkdir: created directory 'backups/weekly/2024'
mkdir: created directory 'backups/monthly'
mkdir: created directory 'backups/monthly/2024'

Creating Multiple Directories

You can create several directories at once by listing them.

Example: Multiple Directories

# Create three directories
mkdir docs downloads images

# Verify
ls -ld docs downloads images

Output:

drwxr-xr-x. 2 centos9 centos9 6 Oct 31 19:30 docs
drwxr-xr-x. 2 centos9 centos9 6 Oct 31 19:30 downloads
drwxr-xr-x. 2 centos9 centos9 6 Oct 31 19:30 images

All three created simultaneously!

With Brace Expansion

# Create numbered directories
mkdir folder{1..10}

# Verify
ls -ld folder*

Output:

drwxr-xr-x. 2 centos9 centos9 6 Oct 31 19:31 folder1
drwxr-xr-x. 2 centos9 centos9 6 Oct 31 19:31 folder2
...
drwxr-xr-x. 2 centos9 centos9 6 Oct 31 19:31 folder10

Complex Brace Expansion

# Create project structure
mkdir -p project/{src,tests,docs,config}

# Verify
ls -ld project/*/

Output:

drwxr-xr-x. 2 centos9 centos9 6 Oct 31 19:32 project/config/
drwxr-xr-x. 2 centos9 centos9 6 Oct 31 19:32 project/docs/
drwxr-xr-x. 2 centos9 centos9 6 Oct 31 19:32 project/src/
drwxr-xr-x. 2 centos9 centos9 6 Oct 31 19:32 project/tests/

Setting Permissions with -m

The -m (mode) flag sets specific permissions during directory creation.

Default Directory Permissions

By default, directories get 755 permissions:

  • Owner: rwx (read, write, execute)
  • Group: r-x (read, execute)
  • Others: r-x (read, execute)

Example: Custom Permissions

# Create directory with 700 permissions (owner-only access)
mkdir -m 700 private

# Verify permissions
ls -ld private

Output:

drwx------. 2 centos9 centos9 6 Oct 31 19:35 private

What -m 700 means:

  • 7 (owner): rwx (4+2+1 = read+write+execute)
  • 0 (group): --- (no permissions)
  • 0 (others): --- (no permissions)

Common Permission Modes

ModePermissionsUse Case
755rwxr-xr-xDefault - everyone can read/access
700rwx------Private - owner only
750rwxr-x---Owner + group access
775rwxrwxr-xOwner + group can modify
777rwxrwxrwxWorld-writable (avoid!)

Directory Naming Best Practices

Choosing good directory names makes navigation and automation easier.

Naming Rules

Valid characters:

  • Letters: a-z, A-Z
  • Numbers: 0-9
  • Underscore: _
  • Hyphen: -
  • Dot: . (but don't start with it unless you want it hidden)

Avoid:

  • Spaces (possible but require quoting)
  • Special characters: * ? [ ] ( ) { } ; | & $ # ! ~ \
  • Starting with hyphen (can be confused with command options)

Good vs Bad Examples

Good βœ…Bad ❌Why
my_projectmy projectSpaces require quoting
backup-2024backup*2024Asterisk is wildcard
logs_2024-12logs(2024)Parentheses problematic
web_devweb&devAmpersand runs in background
temp_data-tempLooks like command option

Professional Naming Conventions

# Date-based (ISO format for sorting)
mkdir -p logs/2024-12-08

# Environment-based
mkdir -p {dev,staging,production}/configs

# Semantic names
mkdir -p src/{components,utils,services,models}

# Version-based
mkdir -p releases/v{1..5}.0

Real-World Directory Structures

Let's look at common professional directory organizations.

Web Application Structure

# Create complete web app structure
mkdir -pv webapp/{public/{css,js,images},src/{components,utils,api},tests,docs,config}

Output:

mkdir: created directory 'webapp'
mkdir: created directory 'webapp/public'
mkdir: created directory 'webapp/public/css'
mkdir: created directory 'webapp/public/js'
mkdir: created directory 'webapp/public/images'
mkdir: created directory 'webapp/src'
mkdir: created directory 'webapp/src/components'
mkdir: created directory 'webapp/src/utils'
mkdir: created directory 'webapp/src/api'
mkdir: created directory 'webapp/tests'
mkdir: created directory 'webapp/docs'
mkdir: created directory 'webapp/config'

Backup Directory Structure

# Create dated backup structure
mkdir -pv backups/{daily,weekly,monthly}/$(date +%Y)

Project with Multiple Environments

# Create environment-specific configs
mkdir -pv project/{dev,staging,prod}/{logs,data,config}

Understanding rmdir (Removing Empty Directories)

While mkdir creates directories, rmdir removes empty directories.

Basic rmdir

# Create and remove empty directory
mkdir testdir
rmdir testdir

# Verify removal
ls -ld testdir

Output:

ls: cannot access 'testdir': No such file or directory

rmdir Only Works on Empty Directories

# Create directory with file
mkdir testdir
touch testdir/file.txt

# Try to remove (will fail)
rmdir testdir

Output:

rmdir: failed to remove 'testdir': Directory not empty

Fix:

# Remove contents first
rm testdir/file.txt

# Then remove directory
rmdir testdir

rmdir with -p (Remove Parents)

# Create nested structure
mkdir -p a/b/c/d

# Remove all empty parent directories
rmdir -p a/b/c/d

# Verify all removed
ls -ld a

Output:

ls: cannot access 'a': No such file or directory
⚠️

⚠️ Important: For removing directories with content, use rm -r (recursive), not rmdir.

# Remove directory and all contents
rm -r directory_name

# Safe version (prompts before each deletion)
rm -ri directory_name

πŸ§ͺ Practice Labs

Time for hands-on practice! These 20 labs will build your mkdir mastery.

πŸ’‘

πŸ’‘ Lab Setup: Create a practice directory:

mkdir ~/mkdir-practice
cd ~/mkdir-practice

Lab 1: Basic Directory Creation (Beginner)

Task: Create a directory called documents. Verify it was created using ls -ld.

Show Solution
# Create directory
mkdir documents

# Verify creation
ls -ld documents

Expected output:

drwxr-xr-x. 2 username groupname 6 Dec  8 19:00 documents

Key points:

  • d indicates directory
  • Default permissions are 755 (rwxr-xr-x)

Lab 2: Permission Denied Error (Beginner)

Task: Try to create a directory in / (root). Understand why it fails.

Show Solution
# Try to create in root directory
mkdir /testdir

Expected error:

mkdir: cannot create directory '/testdir': Permission denied

Explanation:

  • Regular users don't have write permission in /
  • Only root can create directories there
  • Use sudo mkdir /testdir if necessary (or create in ~/ instead)

Lab 3: Nested Structure Without -p (Beginner)

Task: Try to create level1/level2/level3 without using -p. See the error. Then create it properly.

Show Solution
# Try without -p (will fail)
mkdir level1/level2/level3

Error:

mkdir: cannot create directory 'level1/level2/level3': No such file or directory

Fix - Method 1 (manual):

mkdir level1
mkdir level1/level2
mkdir level1/level2/level3

Fix - Method 2 (with -p):

mkdir -p level1/level2/level3

Verify:

ls -R level1/

Key lesson: -p creates parent directories automatically.


Lab 4: Using -p Flag (Beginner)

Task: Create a deep directory structure project/src/utils/helpers in one command using -p.

Show Solution
# Create nested structure
mkdir -p project/src/utils/helpers

# Verify
ls -R project/

Output:

project/:
src

project/src:
utils

project/src/utils:
helpers

project/src/utils/helpers:

Key lesson: -p makes creating deep structures easy.


Lab 5: Verbose Mode (Beginner)

Task: Create logs/2024/december with verbose output to see each directory being created.

Show Solution
# Create with verbose mode
mkdir -pv logs/2024/december

Output:

mkdir: created directory 'logs'
mkdir: created directory 'logs/2024'
mkdir: created directory 'logs/2024/december'

Key lesson: -v shows exactly what mkdir is creating.


Lab 6: Creating Multiple Directories (Intermediate)

Task: Create five directories in one command: dir1, dir2, dir3, dir4, dir5.

Show Solution
# Create multiple directories
mkdir dir1 dir2 dir3 dir4 dir5

# Verify
ls -ld dir*

Output:

drwxr-xr-x. 2 user group 6 Dec  8 19:05 dir1
drwxr-xr-x. 2 user group 6 Dec  8 19:05 dir2
drwxr-xr-x. 2 user group 6 Dec  8 19:05 dir3
drwxr-xr-x. 2 user group 6 Dec  8 19:05 dir4
drwxr-xr-x. 2 user group 6 Dec  8 19:05 dir5

Key lesson: List multiple directory names to create them all at once.


Lab 7: Brace Expansion (Intermediate)

Task: Use brace expansion to create directories folder1 through folder20.

Show Solution
# Use brace expansion
mkdir folder{1..20}

# Verify count
ls -ld folder* | wc -l

Output:

20

List first few:

ls -ld folder{1..5}

Key lesson: Brace expansion is powerful for creating numbered directories.


Lab 8: Setting Permissions with -m (Intermediate)

Task: Create a directory called private_data with 700 permissions (owner-only access).

Show Solution
# Create with specific permissions
mkdir -m 700 private_data

# Verify permissions
ls -ld private_data

Output:

drwx------. 2 user group 6 Dec  8 19:10 private_data

Verify no one else can access:

  • Owner: rwx (full access)
  • Group: --- (no access)
  • Others: --- (no access)

Key lesson: -m sets permissions during creation.


Lab 9: Project Structure Creation (Intermediate)

Task: Create a complete project structure with src, tests, docs, and config directories.

Show Solution
# Create project structure
mkdir -p myproject/{src,tests,docs,config}

# Verify
ls -ld myproject/*/

Output:

drwxr-xr-x. 2 user group 6 Dec  8 19:12 myproject/config/
drwxr-xr-x. 2 user group 6 Dec  8 19:12 myproject/docs/
drwxr-xr-x. 2 user group 6 Dec  8 19:12 myproject/src/
drwxr-xr-x. 2 user group 6 Dec  8 19:12 myproject/tests/

Key lesson: Brace expansion creates multiple siblings at once.


Lab 10: Nested Brace Expansion (Intermediate)

Task: Create environment-specific structures: app/{dev,staging,prod}/{logs,data}.

Show Solution
# Create nested structure
mkdir -pv app/{dev,staging,prod}/{logs,data}

# Verify
ls -R app/

Output structure:

app/:
dev  prod  staging

app/dev:
data  logs

app/prod:
data  logs

app/staging:
data  logs

Key lesson: Nested brace expansion creates complex structures efficiently.


Lab 11: Removing Empty Directories (Intermediate)

Task: Create a directory, verify it's empty, then remove it with rmdir.

Show Solution
# Create directory
mkdir tempdir

# Verify it's empty
ls -la tempdir/

Output:

total 0
drwxr-xr-x. 2 user group  6 Dec  8 19:15 .
drwxr-xr-x. 5 user group 60 Dec  8 19:15 ..
# Remove with rmdir
rmdir tempdir

# Verify removal
ls -ld tempdir

Error (expected):

ls: cannot access 'tempdir': No such file or directory

Key lesson: rmdir only works on empty directories.


Lab 12: rmdir on Non-Empty Directory (Intermediate)

Task: Create a directory with a file inside. Try to remove it with rmdir. See the error.

Show Solution
# Create directory with file
mkdir testdir
touch testdir/file.txt

# Try to remove with rmdir
rmdir testdir

Error:

rmdir: failed to remove 'testdir': Directory not empty

Solution:

# Remove contents first
rm testdir/file.txt

# Now rmdir works
rmdir testdir

Or use recursive rm:

# Alternative: remove directory and contents
rm -r testdir

Key lesson: rmdir requires empty directories; use rm -r for non-empty.


Lab 13: Dated Backup Directories (Advanced)

Task: Create backup directories for the current year, month, and day.

Show Solution
# Create dated backup structure
mkdir -pv backups/$(date +%Y)/$(date +%m)/$(date +%d)

# Verify
ls -R backups/

Example output (if today is 2024-12-08):

backups/:
2024

backups/2024:
12

backups/2024/12:
08

backups/2024/12/08:

Alternative with single date command:

mkdir -pv backups/$(date +%Y/%m/%d)

Key lesson: Command substitution creates dynamic directory names.


Lab 14: Web Application Structure (Advanced)

Task: Create a complete web application directory structure.

Show Solution
# Create webapp structure
mkdir -pv webapp/{public/{css,js,images},src/{components,utils,api},config,tests,docs}

# Verify
tree webapp/
# or
ls -R webapp/

Structure created:

webapp/
β”œβ”€β”€ public/
β”‚   β”œβ”€β”€ css/
β”‚   β”œβ”€β”€ js/
β”‚   └── images/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ utils/
β”‚   └── api/
β”œβ”€β”€ config/
β”œβ”€β”€ tests/
└── docs/

Key lesson: Complex structures can be created in one command.


Lab 15: Avoiding Spaces in Names (Advanced)

Task: Create directory with spaces (shows why it's problematic), then create proper version.

Show Solution
# Create with spaces (requires quotes)
mkdir "my project files"

# Accessing requires quotes every time
cd "my project files"
pwd
cd ..

# Better approach: use underscores or hyphens
mkdir my_project_files

# Much easier to access
cd my_project_files
pwd
cd ..

# Cleanup
rmdir "my project files"
rmdir my_project_files

Key lesson: Avoid spaces in directory names; use _ or - instead.


Lab 16: Combining mkdir with cp (Advanced)

Task: Create a backup directory and copy files into it in one workflow.

Show Solution
# Create backup directory
mkdir -pv backup_$(date +%Y%m%d)

# Copy important files
touch important1.txt important2.txt important3.txt
cp -v *.txt backup_$(date +%Y%m%d)/

# Verify
ls -l backup_*/

Key lesson: Combine mkdir with other commands for complete workflows.


Lab 17: rmdir with -p Flag (Advanced)

Task: Create nested directories, then remove them all with rmdir -p.

Show Solution
# Create nested structure
mkdir -p a/b/c/d

# Verify
ls -R a/

# Remove all levels (all must be empty)
rmdir -pv a/b/c/d

Output:

rmdir: removing directory, 'a/b/c/d'
rmdir: removing directory, 'a/b/c'
rmdir: removing directory, 'a/b'
rmdir: removing directory, 'a'

Verify all removed:

ls -ld a

Error (expected):

ls: cannot access 'a': No such file or directory

Key lesson: rmdir -p removes parent directories if empty.


Lab 18: Multi-Environment Setup (Advanced)

Task: Create a complete multi-environment deployment structure.

Show Solution
# Create environment structure
mkdir -pv deployments/{dev,staging,production}/{app,database,nginx,logs}/{config,data}

# Verify
tree deployments/
# or
ls -R deployments/ | head -30

Creates structure like:

deployments/
β”œβ”€β”€ dev/
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── data/
β”‚   β”œβ”€β”€ database/
β”‚   β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── data/
β”‚   └── ...
└── production/
    └── ...

Key lesson: Complex nested structures with multiple brace levels.


Lab 19: Permission Testing (Advanced)

Task: Create directories with different permissions and test access.

Show Solution
# Create directories with different permissions
mkdir -m 755 public_dir   # Everyone can access
mkdir -m 700 private_dir  # Owner only
mkdir -m 750 group_dir    # Owner + group

# Verify permissions
ls -ld *_dir

Output:

drwx------. 2 user group 6 Dec  8 19:25 private_dir
drwxr-x---. 2 user group 6 Dec  8 19:25 group_dir
drwxr-xr-x. 2 user group 6 Dec  8 19:25 public_dir

Test access:

# Can enter all (as owner)
cd private_dir && pwd && cd ..
cd group_dir && pwd && cd ..
cd public_dir && pwd && cd ..

Key lesson: Different permission levels control who can access directories.


Lab 20: Complete Project Initialization (Advanced)

Task: Create a complete project with proper structure, permissions, and initial files.

Show Solution
# Create complete project
mkdir -pv awesome_project/{src/{components,utils,services},tests,docs,config,logs,data}

# Set private permissions on sensitive directories
chmod 700 awesome_project/logs
chmod 700 awesome_project/data
chmod 700 awesome_project/config

# Create initial documentation files
touch awesome_project/README.md
touch awesome_project/docs/{installation,usage,api}.md

# Create config template
touch awesome_project/config/config.example.json

# Verify structure and permissions
tree awesome_project/
ls -laR awesome_project/

Creates professional project structure:

awesome_project/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ utils/
β”‚   └── services/
β”œβ”€β”€ tests/
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ installation.md
β”‚   β”œβ”€β”€ usage.md
β”‚   └── api.md
β”œβ”€β”€ config/ (drwx------)
β”‚   └── config.example.json
β”œβ”€β”€ logs/ (drwx------)
β”œβ”€β”€ data/ (drwx------)
└── README.md

Key lesson: Combine mkdir with permissions and file creation for complete setup.


πŸ“š Best Practices

βœ… DO

  • Always use -p for nested dirs: mkdir -p a/b/c
  • Use meaningful names: backup_2024 not bk1
  • Use underscores or hyphens: Not spaces
  • Plan structure before creating: Think hierarchy first
  • Use -v for scripts: Verify what's being created
  • Set permissions appropriately: -m 700 for private dirs
  • Use brace expansion: For multiple related directories
  • Follow naming conventions: Consistent across projects
  • Document directory structure: README or diagram
  • Use ISO dates for sorting: 2024-12-08

❌ DON'T

  • Don't use spaces: Requires quoting everywhere
  • Don't use special characters: * ? [ ] ( ) ; |
  • Don't start with hyphen: Looks like option
  • Don't create deep nesting unnecessarily: Keep it simple
  • Don't forget write permissions: Check parent directory first
  • Don't use 777 permissions: Security risk
  • Don't create in /: Unless absolutely necessary
  • Don't use vague names: temp, stuff, misc
  • Don't mix naming styles: Choose snake_case or kebab-case
  • Don't ignore organization: Structure matters for scaling

🚨 Common Pitfalls to Avoid

Pitfall 1: Forgetting -p for Nested Directories

# Wrong: Fails if parents don't exist
mkdir project/src/components

# Right: Always works
mkdir -p project/src/components

Pitfall 2: Using Spaces in Names

# Problematic: Requires quotes everywhere
mkdir "my files"
cd "my files"    # Annoying!

# Better: Use underscores
mkdir my_files
cd my_files      # Easy!

Pitfall 3: Trying to Remove Non-Empty with rmdir

# Wrong: Fails on non-empty directory
rmdir project/

# Right: Use rm -r for directories with content
rm -r project/

Pitfall 4: Wrong Permissions

# Dangerous: Everyone can write
mkdir -m 777 shared_data

# Better: Appropriate permissions
mkdir -m 755 shared_data

Pitfall 5: Creating in Wrong Location

# Wrong: No write permission
mkdir /system_files

# Right: Create in user space
mkdir ~/system_files

πŸ“ Command Cheat Sheet

# ===== BASIC CREATION =====
mkdir dirname                      # Create single directory
mkdir dir1 dir2 dir3              # Create multiple directories
mkdir -p path/to/nested/dir       # Create with parent directories

# ===== WITH OPTIONS =====
mkdir -v dirname                  # Verbose (show what's created)
mkdir -m 700 dirname              # Set specific permissions
mkdir -pv nested/path             # Combine -p and -v

# ===== BRACE EXPANSION =====
mkdir dir{1..10}                  # Create dir1 through dir10
mkdir {src,tests,docs}            # Create multiple named directories
mkdir -p app/{dev,prod}/logs      # Nested with brace expansion

# ===== DATE-BASED =====
mkdir backup_$(date +%Y%m%d)                    # backup_20241208
mkdir -p logs/$(date +%Y)/$(date +%m)          # logs/2024/12
mkdir -p backups/$(date +%Y-%m-%d_%H%M%S)      # backups/2024-12-08_193045

# ===== PROJECT STRUCTURES =====
# Web app
mkdir -p webapp/{public/{css,js},src/{components,utils},config,tests}

# Multi-environment
mkdir -p deploy/{dev,staging,prod}/{app,db,logs}

# Dated backups
mkdir -p backups/{daily,weekly,monthly}/$(date +%Y)

# ===== REMOVING DIRECTORIES =====
rmdir dirname                     # Remove empty directory
rmdir -p a/b/c/d                 # Remove empty nested directories
rmdir -v dirname                 # Verbose removal
rm -r dirname                    # Remove directory with contents
rm -ri dirname                   # Interactive removal (safer)

# ===== PERMISSIONS =====
mkdir -m 755 dirname             # Default (rwxr-xr-x)
mkdir -m 700 private             # Owner only (rwx------)
mkdir -m 750 shared              # Owner + group (rwxr-x---)
mkdir -m 775 group_write         # Owner + group write (rwxrwxr-x)

# ===== VERIFICATION =====
ls -ld dirname                   # Check directory details
ls -R dirname/                   # Recursive listing
tree dirname/                    # Visual tree (if installed)

# ===== REAL-WORLD WORKFLOWS =====
# Create and navigate
mkdir -p project/src && cd project/src

# Create with initial files
mkdir docs && touch docs/{README,INSTALL,LICENSE}.md

# Backup before project modification
mkdir -p ../backups/$(date +%Y%m%d) && cp -a . ../backups/$(date +%Y%m%d)/

# Multi-year structure
mkdir -p archives/{2022..2024}/{Q1,Q2,Q3,Q4}

🎯 Key Takeaways

βœ…

βœ… Master These Concepts:

  1. Basic syntax: mkdir directory_nameβ€”creates new directory
  2. Use -p for nested structuresβ€”auto-creates parents
  3. Use -v for visibilityβ€”see what's being created
  4. Set permissions with -mβ€”control access during creation
  5. Directories marked with dβ€”in file listings
  6. Default permissions are 755β€”rwxr-xr-x
  7. Brace expansion creates multipleβ€”{dir1,dir2,dir3}
  8. rmdir only removes empty directoriesβ€”use rm -r for non-empty
  9. Avoid spaces in namesβ€”use underscores or hyphens
  10. Plan structure before creatingβ€”good organization from the start

LFCS Exam Tips:

  • Know the difference between mkdir and mkdir -p
  • Understand directory permissions (especially execute bit)
  • Remember rmdir vs rm -r usage
  • Practice creating complex structures with brace expansion
  • Know how to set permissions during creation with -m

πŸš€ What's Next?

You've mastered the mkdir command for creating and organizing directories in Linux! You now understand how to create simple and complex directory structures, set permissions, and use advanced features like brace expansion.

In the next post, we'll explore Understanding Absolute vs Relative Paths, including:

  • What paths are and why they matter
  • Absolute paths starting with /
  • Relative paths from current location
  • Special path symbols: . (current), .. (parent), ~ (home)
  • When to use which type of path
  • Path navigation strategies
  • Real-world path examples and best practices

Understanding paths will tie together everything you've learned about navigation, mkdir, and cp!


βœ…

πŸŽ‰ Congratulations! You've mastered directory creation with mkdir! You can now organize filesystems efficiently, create complex structures in single commands, and understand directory permissions.

Practice makes perfect: Start organizing your projects with proper directory structures. Good organization from the beginning saves countless hours later!

Remember: A well-organized directory structure is the foundation of maintainable systems. Take time to plan before creating!

Owais

Written by Owais

I'm an AIOps Engineer with a passion for AI, Operating Systems, Cloud, and Securityβ€”sharing insights that matter in today's tech world.

I completed the UK's Eduqual Level 6 Diploma in AIOps from Al Nafi International College, a globally recognized program that's changing careers worldwide. This diploma is:

  • βœ… Available online in 17+ languages
  • βœ… Includes free student visa guidance for Master's programs in Computer Science fields across the UK, USA, Canada, and more
  • βœ… Comes with job placement support and a 90-day success plan once you land a role
  • βœ… Offers a 1-year internship experience letter while you studyβ€”all with no hidden costs

It's not just a diplomaβ€”it's a career accelerator.

πŸ‘‰ Start your journey today with a 7-day free trial

Related Articles

Continue exploring with these handpicked articles that complement what you just read

More Reading

One more article you might find interesting

45 min read

LFCS Part 52: Bash Startup Files and Configuration (FINAL POST!)

Master bash startup and configuration files. Learn the difference between login and non-login shells, understand /etc/profile, ~/.bashrc, ~/.bash_profile, and when to modify each file. Complete guide with practical examples and 20 hands-on labs for LFCS certification preparation.

#linux#lfcs#certification#bash+4 more