HOWTO · Bash

How to Create Directory in Bash

This article demonstrates how to create a folder if it doesn't already exist in Bash.

On this page

Use mkdir -p -- "$directory" to create a directory only when it is missing, including any missing parent directories. If the path already names a directory, the command succeeds without changing it. If a regular file occupies that path, creation fails because a file cannot serve as a directory.

Create a Folder if It Doesn’t Exist in Bash

The mkdir command creates directories; it does not create files. Its -p option creates missing parents and treats an already-existing directory as success. The -- marks the end of options, which helps when a directory name begins with a hyphen. Quote the expansion so spaces and wildcard characters in a name remain part of one path.

Use the -d test to ask whether a path currently resolves to a directory. The shell syntax needs spaces inside the brackets: if [ -d "$directory" ]; then. This test distinguishes directories from files, but it cannot guarantee that a later command will succeed: permissions, a file at the target path, or a change made between the test and creation can still prevent mkdir from creating it. For a simple create-if-missing operation, mkdir -p handles the already-directory case directly.

Run this example:

if ! base=$(mktemp -d); then
  printf 'Could not create temporary directory.\n' >&2
  exit 1
fi
trap 'rm -rf "$base"' EXIT
directory="$base/reports"
if [ -d "$directory" ]; then
  printf 'Already present: %s\n' "${directory##*/}"
elif mkdir -p -- "$directory"; then
  printf 'Created: %s\n' "${directory##*/}"
else
  printf 'Could not create: %s\n' "${directory##*/}"
fi
Created: reports

The supplied runtime run printed Created: reports. If mktemp -d fails, the script exits before it sets the target path, installs the cleanup trap, or calls mkdir. On success, the temporary path is removed when the script finishes.

Use while Loop to Create a Folder in Bash

Use a while loop when a person should choose another name if the requested directory already exists. Read with read -r so backslashes are not treated specially, quote every expansion, and stop if input ends. The loop below is a complete interactive script: it repeats after an existing directory or a creation failure, and ends after a successful mkdir. Pressing Ctrl+C also ends the script through the shell.

#!/usr/bin/env bash
while :; do
  read -r -p 'Directory name: ' directory || break
  if [ -d "$directory" ]; then
    printf 'Directory already exists: %s\n' "$directory"
    continue
  fi
  if mkdir -p -- "$directory"; then
    printf 'Created: %s\n' "$directory"
    break
  fi
  printf 'Could not create: %s\n' "$directory"
done

The supplied runtime run completed with no stdout for its input. In an interactive session, the prompts and messages depend on the name entered and the permissions of the current location. To create exactly one directory without asking again, use the direct mkdir -p command from the first section instead.

Use the Recursion Method to Create a Folder in Bash

A recursive function can accept the requested path as its first argument, $1. When the path is already a directory, it asks for another name and calls itself with that response. Each call handles one name; there is no loop to break. This script requires an argument, checks the directory test before creation, and reports creation failures rather than claiming success.

#!/usr/bin/env bash
mkdirectory() {
  local directory=$1
  local next_directory
  if [ -d "$directory" ]; then
    printf 'Directory already exists: %s\n' "$directory"
    read -r -p 'Enter a different path: ' next_directory || return 0
    mkdirectory "$next_directory"
  elif mkdir -p -- "$directory"; then
    printf 'Created: %s\n' "$directory"
  else
    printf 'Could not create: %s\n' "$directory"
  fi
}

if [ "$#" -lt 1 ]; then
  printf 'Usage: %s DIRECTORY\n' "$0"
  exit 0
fi
mkdirectory "$1"
Usage: case-3.sh DIRECTORY

Save the script as rcrdir.sh and call it with a path, for example ./rcrdir.sh "project files". Without an argument it prints the usage line shown above and stops. The supplied runtime run produced that usage output. If the requested directory already exists, enter another path; end-of-input stops the recursive prompt cleanly. Output for a supplied path depends on the argument and filesystem state.