NAME
mktemp —
create temporary file or
directory
SYNOPSIS
mktemp |
[-dqu] [-p
directory] [-s
suffix] |
mktemp |
[-dqu]
[-t|-p
directory] [-s
suffix] template |
DESCRIPTION
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).
At most filenames will be tried. This limit is unlikely to be hit, or hittable, with any more than three ‘X’es.
OPTIONS
-d,--directory- Create a directory instead of a regular file.
-q,--quiet- Suppress error output. The EXIT STATUS is unaffected.
-u,--dry-run- Don't create anything, just output a matching path that doesn't exist. Do
not 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
mktempoutput is used. -t,--tmpdir- If the
TMPDIRenvironment variable is set, equivalent to-pTMPDIR, otherwise to-p/tmp. -p,--tmpdir=directory- Prepend directory[/] to the template.
-s,--suffix=suffix- Append suffix to the template after parsing it: no amount of ‘X’es will interfere with the template pattern.
ENVIRONMENT
TMPDIR- Used if
-p,-t, or no template.
EXIT STATUS
1 if
-u and
faccessat(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.
EXAMPLES
$mktemp/tmp/tmp.K2EHQCo6LG$mktemp-p.cache .cache/tmp.Vp2q7gVUX5$mktempinXXX.jpeg inpBH.jpeg$ TMPDIR=~/.cachemktemp-tinXXX.jpeg /home/cicada/.cache/inq8e.jpeg
Or, as part of a script:
#!/bin/shtmpfile="$(mktemp)" || exit echo "Program output." > "$tmpfile"
#!/bin/shexec > "$(mktemp/var/tmp/cleanup-XXXXXXXX.log)" || exit
SEE ALSO
STANDARDS
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 6.
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)' |
m4HISTORY
Originates from OpenBSD 2.1 as mktemp(1):
mktemp - make
temporary file name (unique)-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.