mktemp(1)mktemp(1)NAMEmktemp - Create a secure and uniquely named file or directory
SYNOPSISmktemp [-dqt] [-p prefix_directory] [template]
OPTIONS
Makes a directory instead of a file. Fails silently if an error
occurs. Prepends the specified path to the name generated for the tem‐
porary file or directory.
This option implies the -t option. Therefore, the value of the
TMPDIR environment variable, if set, overrides the path prefix
specified by the -p option. Creates a directory or file whose
path prefix is as follows (in highest to lowest order of prece‐
dence): The value of the TMPDIR environment variable The argu‐
ment to the -p option /tmp
OPERANDS
Specifies a template used by mktemp to form a unique name for the file
or directory being created. The template is a combination of characters
that are included in the name, trailed by uppercase X characters (for
example, fpm.XXXXXXXX or fpmXXXXXX). When creating the name for the
file or directory, mktemp substitutes a random combination of letters
and numbers in place of the uppercase X characters.
If the trailer contains fewer than six X characters, mktemp
increases the number of X characters to six before substituting
the random combination of letters and numbers. However, in
scripts that might be ported to other platforms, specify at
least six X characters explicitly.
If template is omitted from the command, the value
tmp.XXXXXXXXXX is used, and the -t option is implied.
When including both the template operand and the -t option, tem‐
plate specifies the final component of the path and cannot con‐
tain any slash (/) characters.
DESCRIPTION
The mktemp utility is provided to give shell scripts a safer way to
create temporary files, particularly when writing to world-writeable
areas, such as /tmp or /var/tmp. Traditional methods for creating tem‐
porary files, such as combining the name of the program with the
process ID of the user running it, are highly predictable and can allow
system security to be compromised.
The mktemp utility creates a directory or file with a randomized name
and safe protection mode. In the event that the generated name speci‐
fies an existing directory, regular file, symbolic link, hard link, or
fifo, the utility automatically retries with a different name.
If mktemp is successful at generating a unique file name, the file is
created with mode 0600 and its name is written to standard output. If
mktemp is successful at generating a unique directory name, the direc‐
tory is created with mode 0700 and its name is written to standard out‐
put.
The mktemp utility is derived from OpenBSD software.
EXIT STATUS
Success. An error occurred.
ERRORS
The following diagnostics may be printed to standard error during the
normal course of utility operation. mktemp: template must not contain
directory separators in -t mode
If -t is included (or implied by the -p option), template must
specify only the final node of the path.
This error is also returned if the -p option specifies a direc‐
tory that does not exist when the temporary file is created.
mktemp: cannot allocate memory
Insufficient memory is available to the utility for one of its
operations. mktemp: path is too long
The length of the pathname for the generated file exceeded
PATH_MAX. mktemp: cannot make temporary directory
The -d option was included and a directory could not be created
for one of the following reasons: The utility could not generate
a directory name that is unique. An intermediate node in the
specified path does not exist. The process does not have
required permissions. Insufficient disk space is available for
directory creation. The maximum number of inodes for the parent
directory has been reached. mktemp: cannot make temporary file
The file could not be created for one of the following reasons:
The utility could not generate a file name that is unique. An
intermediate node in the specified path does not exist. The
process does not have required permissions. Insufficient disk
space is available for file creation. The maximum number of
inodes for the parent directory has been reached.
EXAMPLES
The following example shows a simple use of mktemp in which the tem‐
plate operand specifies the absolute path to the temporary file and the
script exits if it cannot create the file:
TMPFILE=`/bin/mktemp /tmp/fpm.XXXXXX` if [ $? -ne 0 ] then
echo "Cannot create temporary file; exiting..."
exit 1 fi
Because neither the -t nor -p option is included in the command
line, the value of TMPDIR cannot override the file's path at run
time.
When specifying the absolute path to the temporary file, do not
include $0 as shown in the following example because $0 by
itself sometimes represents an absolute path:
TMPFILE=`/bin/mktemp /tmp/$0.XXXXXX`
Instead, include $0 along with basename as shown in the follow‐
ing example:
TMPFILE=`/bin/mktemp /tmp/\`basename $0\`.XXXXXX` The -t option
in the following example allows the utility to use the setting
of the TMPDIR environment variable (or, by default, /tmp) as the
directory prefix for the temporary file:
file_basename=`basename $0` TMPFILE=`mktemp -q-t ${file_base‐
name}.XXXXXX` In the following example, the -p option specifies
a default temporary directory other than /tmp; the temporary
file will be created in /extra/tmp unless the setting of the
TMPDIR environment variable at the time the script runs over‐
rides this path:
TMPFILE=`/bin/mktemp -p /extra/tmp fpm.XXXXXXXXXX`
Note that this example will cause the script to fail if the
/extra/tmp directory does not exist or is not writeable when the
utility attempts to create the temporary file.
ENVIRONMENT VARIABLES
Provides a default value for the internationalization variables that
are unset or null. If LANG is unset or null, the corresponding value
from the default locale is used. If any of the internationalization
variables contain an invalid setting, the utility behaves as if none of
the variables had been defined. If set to a non-empty string value,
overrides the values of all the other internationalization variables.
Determines the locale for the format and contents of diagnostic mes‐
sages written to standard error. Determines the locale for interpret‐
ing sequences of text data as characters (for example, as single-byte
or multibyte characters in operands and option arguments). Determines
the location of message catalogs for the processing of LC_MESSAGES.
Determines the directory in which the temporary file is created when
the -t option is implicitly or explicitly specified.
SEE ALSO
Functions: mktemp(3)
Others: environ(5)mktemp(1)