User Commands mktemp(1) NAME mktemp - make temporary file, filename or directory SYNOPSIS /usr/bin/mktemp /usr/bin/mktemp [ options ] [ prefix ] ksh93 mktemp [ options ] [ prefix ] DESCRIPTION mktemp creates a temporary file with optional base name prefix prefix. If prefix is omitted then tmp_ is used and --tmp is implied. If prefix contains a directory prefix then that directory overrides any of the directories described below. A temporary file will have mode rw------- and a temporary directory will have mode rwx------, subject to umask(1). Generated paths have these attributes: * Lower case to avoid clashes on case ignorant filesystems. * Pseudo-random part to deter denial of service attacks. * Pseudo-random part may be 3-chars.3-chars on filesystems which only support 8.3 file names. A consecutive sequence of X's in prefix is replaced by the pseudo-random part. If there are no X's then the pseudo-random part is appended to the prefix. OPTIONS The following options are supported: -d, --directory Create a directory instead of a regular file. -m, --mode=mode Set the mode of the created temporary to mode. mode is symbolic or octal mode as in chmod(1). Relative modes assume an initial mode of u=rwx. -p, --default=directory Use directory if the TMPDIR environment variable is not defined. Implies --tmp. -q, --quiet Suppress file and directory error diagnostics. -t, --tmp|temporary-directory Create a path rooted in a temporary directory. -u, --unsafe|dry-run Check for file/directory existence but do not create. Who would want to do that --help Prints basic help information --version Prints version information OPERANDS The following operands are supported: template template can be any filename with one or more Xs appended to it, for example /tmp/tfile.XXXXXXXXXX. If template is not specified, a default of tmp.XXXXXXXXXX is used and the -t flag is implied. EXAMPLES Example 1: Using mktemp The following example illustrates a simple use of mktemp in a sh(1) script. In this example, the script quits if it cannot get a safe temporary file. TMPFILE=`mktemp /tmp/example.XXXXXX` if [ -z "$TMPFILE" ]; then exit 1; fi printf "program output\n" >> "$TMPFILE" Example 2: Using mktemp to Support TMPDIR The following example uses mktemp to support for a user's TMPDIR environment variable: TMPFILE=$(mktemp -t example.XXXXXX) || exit 1 if [[ -z $TMPFILE ]]; then exit 1; fi printf "program output\n" >> "$TMPFILE" Example 3: Using mktemp Without Specifying the Name of the Temporary File The following example uses mktemp without specifying the name of the temporary file. In this case the -t flag is implied. TMPFILE=$(mktemp) || exit 1 if [[ -z "$TMPFILE" ]]; then exit 1; fi printf "program output\n" >> "$TMPFILE" Example 4: Using mktemp with a Default Temporary Directory Other than /tmp The following example creates the temporary file in /extra/tmp unless the user's TMPDIR environment variable specifies otherwise: TMPFILE=$(mktemp -p /extra/tmp example.XXXXX) || exit 1 if [[ -z "$TMPFILE" ]]; then exit 1; fi printf "program output\n" >> "$TMPFILE" Example 5: Using mktemp to Remove a File The following example attempts to create two temporary files. If creation of the second temporary file fails, mktemp removes the first file before exiting: TMP1=$(mktemp -t example.1.XXXXXXXXXX) if [[ -z $TMP1 ]]; then exit 1; fi TMP2=$(mktemp -t example.2.XXXXXXXXXX) if [[ -z $TMP2 ]]; then rm -f "$TMP1" exit 1 fi Example 6: Using mktemp The following example does not exit if mktemp is unable to create the file. That part of the script has been protected. TMPFILE=$(mktemp -q -t example.XXXXXXXXXX) if [[ ! -z $TMPFILE ]] then # Safe to use $TMPFILE in this block printf "%s\n" data > "$TMPFILE" ... rm -f "$TMPFILE" fi ENVIRONMENT VARIABLES See environ(5) for descriptions of the following environment variables that affect the execution of mktemp with the -t option: TMPDIR. EXIT STATUS The following exit values are returned: 0 Successful completion. 1 An error occurred. ATTRIBUTES See attributes(5) for descriptions of the following attributes: ____________________________________________________________ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | |_____________________________|_____________________________| | Availability | SUNWcsu | |_____________________________|_____________________________| | CSI | Enabled | |_____________________________|_____________________________| | Interface Stability | Commited | |_____________________________|_____________________________| SEE ALSO sh(1), ksh93(1), mkdtemp(3C), mkstemp(3C), mktemp(3C), attributes(5), environ(5) NOTES It is STRONGLY recommended that scripts/applications which use more than one temporary file during runtime create a directory for the temporary files. The mktemp utility appeared in OpenBSD 2.1. The current implementation is based on the AT&T AST version of "mktemp" SunOS 5.10 Last change: 4 Jun 2004 4