0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Create Directory Structure Easily

Use the Bash command mkdir -p path/to/directory inside a script to create a directory structure, including any necessary parent directories.
📋

Examples

Inputmkdir -p myfolder
OutputCreates a single directory named 'myfolder' if it does not exist.
Inputmkdir -p projects/2024/april
OutputCreates nested directories 'projects', '2024', and 'april' all at once.
Inputmkdir -p /tmp/test/dir1/dir2
OutputCreates the full path '/tmp/test/dir1/dir2' including all missing parent directories.
🧠

How to Think About It

To create a directory structure in Bash, think about the full path you want to build. The mkdir command can create one directory or many nested directories. Using the -p option tells Bash to create all parent directories if they don't exist, so you don't have to create each level one by one.
📐

Algorithm

1
Get the directory path input from the user or script variable.
2
Use the <code>mkdir</code> command with the <code>-p</code> option to create the full directory path.
3
Check if the directories were created successfully.
4
Print a confirmation message.
💻

Code

bash
#!/bin/bash

# Directory path to create
DIR_PATH="projects/2024/april"

# Create directory structure
mkdir -p "$DIR_PATH"

# Confirm creation
if [ -d "$DIR_PATH" ]; then
  echo "Directory structure '$DIR_PATH' created successfully."
else
  echo "Failed to create directory structure."
fi
Output
Directory structure 'projects/2024/april' created successfully.
🔍

Dry Run

Let's trace creating 'projects/2024/april' through the script

1

Set directory path

DIR_PATH="projects/2024/april"

2

Run mkdir with -p

mkdir -p "projects/2024/april" creates all missing directories

3

Check if directory exists

[ -d "projects/2024/april" ] returns true if directory exists

StepActionValue
1Set DIR_PATHprojects/2024/april
2Create directoriesmkdir -p projects/2024/april
3Check directoryDirectory exists: true
💡

Why This Works

Step 1: Using mkdir -p

The -p option tells mkdir to create parent directories as needed, so the full path is created in one command.

Step 2: Checking directory existence

The test [ -d "$DIR_PATH" ] checks if the directory now exists to confirm success.

Step 3: Printing confirmation

A message is printed to inform the user whether the directory structure was created successfully.

🔄

Alternative Approaches

Using a loop to create each directory level
bash
#!/bin/bash

DIRS=(projects 2024 april)
CURRENT=""
for d in "${DIRS[@]}"; do
  CURRENT="$CURRENT/$d"
  mkdir "$CURRENT"
done

echo "Directories created step by step."
This creates directories one by one without -p, but is longer and less efficient.
Using mkdir without -p for single directory
bash
#!/bin/bash

mkdir myfolder

echo "Single directory created."
Only works if parent directories exist; fails if they don't.

Complexity: O(n) time, O(1) space

Time Complexity

The time depends on the number of directories to create (n). mkdir -p creates each missing directory once, so it's linear in the depth of the path.

Space Complexity

The script uses constant extra space, only storing the directory path string and temporary variables.

Which Approach is Fastest?

Using mkdir -p is fastest and simplest compared to looping through each directory level manually.

ApproachTimeSpaceBest For
mkdir -pO(n)O(1)Creating nested directories efficiently
Loop creating each directoryO(n)O(1)Manual control but more code
mkdir without -pO(1)O(1)Single directory, parents must exist
💡
Always use mkdir -p to safely create nested directories without errors if parents are missing.
⚠️
Forgetting the -p option causes errors if parent directories don't exist.