← Full reference

mktemp

Safely create temporary files and directories with unique names

mktemp creates a temporary file (or, with -d, a directory) with an unpredictable unique name and prints its path. The file is created with mode 600, the directory with 700: only the owner has access. Save the path in a variable and leave cleanup to trap. The sandbox has GNU coreutils 9.4.

Syntax

mktemp [options] [template]

Without a template: $TMPDIR/tmp.XXXXXXXXXX (if TMPDIR is not set — /tmp). Each trailing X in the template is replaced with a random letter or digit; there must be at least three.

Options

OptionWhat it does
-dcreate a directory instead of a file
-p dircreate in this directory; the template is just a name. -p "" or -p without a value — $TMPDIR, otherwise /tmp
--tmpdir[=dir]long form of -p
-tthe template is a name only; the directory is $TMPDIR if set (it takes precedence over -p!), otherwise from -p, otherwise /tmp. Deprecated, use --tmpdir instead
--suffix=.extappend a suffix after the Xs: tmp.Ab3dE.conf
-ucreate nothing, only print a name (unsafe: someone else may grab the name first)
-qdon't print an error if creation fails (the exit code is still 1)

Where the file ends up

CallWhere it is created
mktemp$TMPDIR or /tmp
mktemp app.XXXXXXin the current directory: the template is a relative path
mktemp /var/tmp/app.XXXXXXexactly at the path in the template
mktemp -p /var/tmp/var/tmp/tmp.XXXXXXXXXX — -p takes precedence over TMPDIR
mktemp -t app.XXXX$TMPDIR/app.XXXX or /tmp/app.XXXX
mktemp --tmpdir app.XXXXXX.json$TMPDIR or /tmp; a suffix after the Xs is allowed too

Cleanup with trap

#!/bin/bash
set -euo pipefail

tmp=$(mktemp -d)
trap 'rm -rf -- "$tmp"' EXIT     # fires on any exit

curl -fsSL https://example.com/data.json -o "$tmp/data.json"
jq '.items' "$tmp/data.json"

In bash the EXIT trap fires on normal termination, on exit via set -e and on termination by SIGTERM/SIGINT. It won't fire only on SIGKILL (kill -9) — nothing helps there.

Examples

# temporary file
tmp=$(mktemp)
sort data.txt > "$tmp" && mv "$tmp" data.txt

# temporary directory
work=$(mktemp -d)

# a readable prefix — easier to find in /tmp if something is left over
mktemp -t backup.XXXXXX

# with an extension — some programs look at it
mktemp --suffix=.json

# next to the target file (same FS) — for an atomic replace via mv
tmp=$(mktemp -p /etc/myapp app.conf.XXXXXX)

# check that creation succeeded
tmp=$(mktemp) || { echo 'failed to create a temporary file' >&2; exit 1; }

Pitfalls

  • Don't make up names yourself. /tmp/myscript.$$ is predictable: another user can create a link with that name in advance, and the script will write data wherever it points. mktemp creates the file atomically and with mode 600.
  • Too few Xs: mktemp app.XX — "too few X's in template". You need at least three, preferably six or more.
  • A template without a path is created in the current directory, not in /tmp. Want it in /tmp — use -t, -p or a full path.
  • Mode 600. If the temporary file later becomes permanent (via mv), it keeps mode 600 — change it with chmod.
  • Without trap, junk piles up: the script crashes halfway — the file stays in /tmp. Set trap right after mktemp.
  • Single quotes in trap. In trap 'rm -rf -- "$tmp"' EXIT the variable is expanded when the trap fires. With double quotes it is substituted immediately — that works, but breaks if the path contains spaces or quotes.

See also

kill · set · exit-codes · mv · rm · umask · variables