What does the mkdir -p mean in a script file?
Categories:
Understanding mkdir -p
in Shell Scripts

Explore the mkdir -p
command, its functionality, and why it's essential for robust directory creation in Linux shell scripts.
When working with shell scripts in Linux or Unix-like environments, you often need to create directories. The mkdir
command is fundamental for this task. However, simply using mkdir
can lead to errors if the parent directories don't already exist. This is where the -p
option comes into play, making your scripts more resilient and user-friendly.
The Basic mkdir
Command
The mkdir
command (make directory) is used to create new directories. By default, it can only create a directory if its parent directory already exists. If you try to create a directory whose parent path does not exist, mkdir
will return an error.
# Attempt to create a directory where 'parent_dir' does not exist
mkdir /tmp/parent_dir/new_dir
# Expected output:
mkdir: cannot create directory '/tmp/parent_dir/new_dir': No such file or directory
Example of mkdir
failing without existing parent directories.
Introducing mkdir -p
The -p
(or --parents
) option modifies the behavior of mkdir
significantly. When -p
is used, mkdir
will create any necessary parent directories along the way if they do not already exist. If the directory already exists, mkdir -p
will not return an error; it will simply do nothing, making it idempotent.
# Create a directory and its parents if they don't exist
mkdir -p /tmp/project/data/logs
# This command will:
# 1. Create /tmp/project (if it doesn't exist)
# 2. Create /tmp/project/data (if it doesn't exist)
# 3. Create /tmp/project/data/logs (if it doesn't exist)
# No error will be returned if any of these already exist.
Using mkdir -p
to create a nested directory structure.
flowchart TD A[Start Script] --> B{Does /tmp/project exist?} B -->|No| C[Create /tmp/project] C --> D{Does /tmp/project/data exist?} B -->|Yes| D D -->|No| E[Create /tmp/project/data] E --> F{Does /tmp/project/data/logs exist?} D -->|Yes| F F -->|No| G[Create /tmp/project/data/logs] F -->|Yes| H[Directory already exists] G --> I[Continue Script] H --> I
Flowchart illustrating the logic of mkdir -p /tmp/project/data/logs
.
-p
option is particularly useful in automated scripts where you cannot guarantee the existence of intermediate directories. It prevents script failures due to missing paths.Common Use Cases in Scripting
The mkdir -p
command is invaluable in various scripting scenarios:
- Log File Directories: Ensuring a directory for log files exists before an application attempts to write to it.
- Temporary File Storage: Creating unique temporary directories for processes.
- Configuration Directories: Setting up application-specific configuration paths.
- Build Processes: Preparing output directories for compiled code or build artifacts.
#!/bin/bash
APP_NAME="my_application"
LOG_DIR="/var/log/${APP_NAME}"
CONFIG_DIR="/etc/${APP_NAME}"
DATA_DIR="/opt/${APP_NAME}/data"
# Create all necessary directories, including parents
mkdir -p "${LOG_DIR}"
mkdir -p "${CONFIG_DIR}"
mkdir -p "${DATA_DIR}"
if [ $? -eq 0 ]; then
echo "All application directories created successfully."
else
echo "Error creating one or more directories."
exit 1
fi
# Example of using the directories
echo "Application started at $(date)" >> "${LOG_DIR}/app.log"
echo "Configuration loaded from ${CONFIG_DIR}/config.conf"
exit 0
A shell script demonstrating the robust use of mkdir -p
for application directories.
mkdir -p
handles parent directory creation, it does not set permissions. You might need to follow up with chmod
to set appropriate permissions for the newly created directories.