Skip to content
Latchkey

mkdir -p Command Reference for CI Scripts

mkdir -p creates a directory and any missing parents, without erroring if it already exists.

mkdir -p is the idempotent way to ensure a path exists. It is the right default in CI because re-running a step should not fail just because the directory is already there.

Common flags/usage

  • -p / --parents: create parent directories as needed, no error if present
  • -m MODE: set the permission mode on creation
  • -v: print a message for each created directory
  • mkdir -p a/b/c: creates the whole chain in one call

Example

shell
mkdir -p "${HOME}/.cache/pip" "${HOME}/.local/bin"
mkdir -p -m 700 "${HOME}/.ssh"
artifacts="dist/${GITHUB_RUN_ID}" ; mkdir -p "${artifacts}"

In CI

Plain mkdir errors with "File exists" on a re-run and fails the step under set -e; mkdir -p is idempotent and creates intermediate parents in one call. Use -m to set the mode atomically (e.g. 700 on ~/.ssh) rather than a separate chmod, which avoids a brief window of loose permissions.

Key takeaways

  • mkdir -p is idempotent and will not fail if the directory already exists.
  • It creates all missing parent directories in a single call.
  • -m sets the mode at creation time, avoiding a separate chmod step.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →