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
cp Command Reference for CI Scriptscp copies files and directories in CI for staging artifacts. Reference for -r, -p, -a, and trailing-slash beh…
rm -rf Command Reference for CI Scriptsrm -rf removes files and directories in CI cleanup. Reference for -r, -f, and safe-deletion habits, plus the…
mktemp Command Reference for CI Scriptsmktemp creates unique temp files and directories safely in CI. Reference for -d, templates, and -t, plus the…
chmod Command Reference for CI Scriptschmod changes file permissions in CI. Reference for +x, octal modes, -R, and symbolic modes, plus the 600-on-…