MKTEMP(1) General Commands Manual MKTEMP(1)

mktempcreate temporary file or directory

mktemp [-dqu] [-p directory] [-s suffix]
mktemp [-dqu] [-t|-p directory] [-s suffix] template

Creates a file u=rw - umask (directory u=rwx - umask if -d), replacing a run of ‘X’ characters in the template, and writes its path to the standard output stream. If no template, defaults to "tmp.XXXXXXXXXX" and -t. The final path is "[directory[/]]template[suffix]".

The template must contain at least three consecutive ‘X’es after its final slash (if any). These are replaced with random alphanumeric characters until a path that doesn't already exist is generated; this behaviour, coupled with the mode, makes mktemp suitable for safely generating unique temporary files (unless -u).

, --directory
Create a directory instead of a regular file.
, --quiet
Suppress error output. The EXIT STATUS is unaffected.
, --dry-run
Don't create anything, just output a matching path that doesn't exist. Do use this — there's no guarantee the file can be created in the first place, and the file may be used or subverted by a third party before the mktemp output is used.
, --tmpdir
If the TMPDIR environment variable is set, equivalent to -p TMPDIR, otherwise to -p /tmp.
, --tmpdir=directory
Prepend directory[/] to the template.
, --suffix=suffix
Append suffix to the template parsing it: no amount of ‘X’es will interfere with the template pattern.

Used if -p, -t, or no template.

if -u and stat(2) failed for a reason other than ENOENT, otherwise if open(2)/mkdir(2) failed for a reason other than EEXIST. In case of a write error, created files are removed.

$ mktemp
/tmp/tmp.K2EHQCo6LG
$ mktemp -p .cache
.cache/tmp.Vp2q7gVUX5

$ mktemp inXXX.jpeg
inpBH.jpeg
$ TMPDIR=~/.cache mktemp -t inXXX.jpeg
/home/cicada/.cache/inq8e.jpeg

Or, as part of a script:

#!/bin/sh
tmpfile="$(mktemp)" || exit
echo "Program output." > "$tmpfile"

#!/bin/sh
exec > "$(mktemp /var/tmp/cleanup-XXXXXXXX.log)" || exit

mkdir(2), open(2), mkstemp(3)

Compatible with the GNU system; short -s and allowing -s when template doesn't end with an ‘X’ are extensions.

OpenBSD supports -dqutp with TMPDIR overriding -p, its template must end with the ‘X’es, and the minimum amount thereof is . NetBSD allows any amount of ‘X’es, -p overrides TMPDIR, supports any amount of templates, and its -t prefix injects a "prefix.XXXXXXXX" template. FreeBSD is as NetBSD but drops -p. All three of those implementations first create the file with -u, then remove it. An OpenBSD-compatible mktemp also exists in the illumos gate.

The only strictly-IEEE Std 1003.1-2008 (“POSIX.1”)-compliant way to emulate mktemp is to use

echo 'mkstemp(/tmp/prefixXXXXXX)' | m4

Originates from OpenBSD 2.1 as mktemp(1):

mktemp - make temporary file name (unique)
Supporting -dqu, defaulting to a tmp.XXXXXXXXXX template.

A compatible implementation appeared in FreeBSD 3.0, adding -t prefix, which constructs a template in the form "${TMPDIR:-/tmp}[/]prefix.XXXXXXXX" and allows any amount of templates afterward, with a default mktemp prefix. This was imported into NetBSD 1.5.

OpenBSD 3.0 adds -tp, as present-day.

NetBSD 6.1 adds -p tmpdir, with the highest priority.

November 23, 2022 voreutils pre-v0.0.0-latest