mktemp

create unique temporary file 

Command


SYNOPSIS

mktemp [-dqu] [-t prefix] template ...

mktemp [-dqu] -t prefix


DESCRIPTION

The mktemp utility takes each of the file name templates specified on the command line and uses it to create a unique file name.

Each template is a file name with a string of trailing Xs (for example, /tmp/temp.XXX). To create the unique file name, mktemp replaces the trailing Xs with a string composed of some combination of the current process number and a unique letter combination. The number of possible unique file names that mktemp can create is based on the number of trailing Xs. For example, six trailing Xs results in 26^6 possible file names.

When mktemp successfully generates a unique file name, it also creates a file with that name (unless the -u option is specified) and displays the name.

The mktemp utility can create any number of temporary files with a single command. This includes one generated from the internal template built from the -t prefix option.

Traditionally, many shell scripts name temporary files by appending the process ID to the program name. However, this is a predictable naming scheme and creates a race condition that is easy for the attacker to win. A more sophisticated approach is to create a temporary directory using the same naming scheme and while this makes sure a temporary file isn't subverted, it still permits a simple denial of service attack. The mktemp utility avoids these problems and for this reason, it is the recommended approach for creating temporary files.

Options

-d 

Creates a directory with the name generated rather than a file.

-q 

Causes mktemp to fail silently when an error occurs. This prevents any error messages from being sent to the standard error.

-t prefix 

Generates a template string using prefix and the value of the TMPDIR environment variable. When TMPDIR is not set, mktemp uses /tmp.

Note:

When using this option, you should be careful to make sure it is appropriate to use an environment variable with a value potentially supplied by the user.

-u 

Unlinks the temporary file before mktemp exits. While this is slightly better than the NuTCRACKER Platform's mktemp() function, it still introduces a race condition, and, for that reason, this option is not recommended.


EXAMPLES

The following example shows a potential use of mktemp where the script should exit if it cannot create a safe temporary file:

temp=`basename $0`
TMPFILE=`mktemp -t ${temp}` || exit 1
echo "program output" >> $TMPFILE

This next example shows how the script can catch the error itself:

temp=`basename $0`
TMPFILE=`mktemp -q /tmp/${temp}.XXXXXX`
if [ $? -ne 0 ]; then
	echo "$0: Can't create temp file, exiting..."
	exit 1
fi

DIAGNOSTICS

Possible exit status values are:

0 

Successful completion.

1 

An error occurred.


PORTABILITY

Windows 8.1. Windows Server 2012 R2. Windows 10. Windows Server 2016. Windows Server 2019. Windows 11. Windows Server 2022.


AVAILABILITY

PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
mktemp()

Miscellaneous:
envvar


PTC MKS Toolkit 10.4 Documentation Build 39.