MKTEMP(1) | General Commands Manual | MKTEMP(1)

# NAME

`mktemp` — make temporary file or directory (unique)

# SYNOPSIS

`mktemp` | [`-dqtu`] [`-p` directory] [template]

# DESCRIPTION

The `mktemp` utility takes the specified file name template and overwrites a portion of it to create a unique file name. The template may be any file name containing at least six ‘`Xs`’ in the last component of the path, for example /tmp/tfile.XXXXXXXXXX or /tmp/editor.XXXXXXXXXX.txt. If there is more than one run of ‘`Xs`’ in the template, only the last one will be considered. If no template is specified, a default value of tmp.XXXXXXXXXX is used and the `-t` flag is implied (see below).

The final ‘`Xs`’ are replaced with a unique digit and letter combination. The name chosen depends both on the number of ‘`Xs`’ in the template and the number of collisions with pre-existing entries. The number of unique names `mktemp` can return depends on the number of ‘`Xs`’ provided; ten ‘`Xs`’ will result in `mktemp` testing roughly 26 ** 10 combinations.

If `mktemp` successfully generates a unique name, the file (or directory) is created with permissions such that it is only readable and writable by its owner (unless the `-u` flag is given) and the name is printed to the standard output.

`mktemp` is provided to allow shell scripts to safely use temporary files. Traditionally, many shell scripts take the name of the program with the PID as a suffix and use that as a temporary file name. This kind of naming scheme is predictable and the race condition it creates is easy for an attacker to win. A safer, though still inferior approach, is to make a temporary directory using the same naming scheme. While this does allow one to guarantee that a temporary file will not be subverted, it still allows a simple denial of service attack. For these reasons it is suggested that `mktemp` be used instead.

The options are as follows:

`-d`
  Make a directory instead of a file.
`-p` directory
  Use the specified directory as a prefix when generating the temporary name. The directory will be overridden by the user's `TMPDIR` environment variable if it is set. This option implies the `-t` flag (see below).
`-q`
  Fail silently if an error occurs. This is useful if a script does not want error output to go to standard error.
`-t`
  Generate a path rooted in a temporary directory. This directory is chosen as follows:
  - If the user's `TMPDIR` environment variable is set, the directory contained therein is used.
  - Otherwise, if the `-p` flag was given the specified directory is used.
  - If none of the above apply, /tmp is used.

  In this mode, the template (if specified) should be a directory component (as opposed to a full path) and thus should not contain any forward slashes.

`-u`
  Operate in “unsafe” mode. The temp file will be unlinked before `mktemp` exits. This is slightly better than mktemp(3) but still introduces a race condition. Use of this option is not encouraged.

# ENVIRONMENT

`TMPDIR`
  directory in which to place the temporary file when in `-t` mode

# EXIT STATUS

The `mktemp` utility exits 0 on success, and >0 if an error occurs.

# EXAMPLES

The following sh(1) fragment illustrates a simple use of `mktemp` where the script should quit if it cannot get a safe temporary file:

    TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1
    echo "program output" >> $TMPFILE

The same fragment with support for a user's `TMPDIR` environment variable can be written as follows:

    TMPFILE=`mktemp -t example.XXXXXXXXXX` || exit 1
    echo "program output" >> $TMPFILE

This can be further simplified if we don't care about the actual name of the temporary file. In this case the `-t` flag is implied:

    TMPFILE=`mktemp` || exit 1
    echo "program output" >> $TMPFILE

In some cases, it may be desirable to use a default temporary directory other than /tmp. In this example the temporary file will be created in /extra/tmp unless the user's `TMPDIR` environment variable specifies otherwise:

    TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1
    echo "program output" >> $TMPFILE

In other cases, we want the script to catch the error. For instance, if we attempt to create two temporary files and the second one fails we need to remove the first before exiting:

    TMP1=`mktemp -t example.XXXXXXXXXX.1` || exit 1
    TMP2=`mktemp -t example.XXXXXXXXXX.2`
    if [ $? -ne 0 ]; then
    	rm -f $TMP1
    	exit 1
    fi

Or perhaps you don't want to exit if `mktemp` is unable to create the file. In this case you can protect that part of the script thusly:

    TMPFILE=`mktemp -q -t example.XXXXXXXXXX` && {
    	# Safe to use $TMPFILE in this block
    	echo data > $TMPFILE
    	...
    	rm -f $TMPFILE
    }

# DIAGNOSTICS

One of the following error messages may be displayed if `mktemp` does not succeed and the `-q` option was not specified:

`insufficient number of Xs in template`
  The specified template contained fewer than six ‘`Xs`’ at the end.
`template must not contain directory separators in -t mode`
  The template contained one or more directory components and the `-t` option was specified.
`cannot make temp dir`
  `mktemp` was unable to create the temporary directory for any of the reasons specified by mkdtemp(3).
`cannot make temp file`
  `mktemp` was unable to create the temporary file for any of the reasons specified by mkstemp(3).
`cannot allocate memory`
  `mktemp` was unable to allocate memory for any of the reasons specified by malloc(3).

# SEE ALSO

mktemp(3)

# HISTORY

The `mktemp` utility first appeared in OpenBSD 2.1.

March 3, 2024 | Debian