'\" te .\" Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved. .TH mktemp 1 "23 Jul 2012" "SunOS 5.11" "User Commands" .SH NAME mktemp \- make temporary filename .SH SYNOPSIS .LP .nf \fBmktemp\fR [\fB-dtqu\fR] [\fB--directory\fR] [\fB--quiet\fR] [\fB--dry-run\fR] [\fB-p\fR \fIdirectory\fR] [\fB--suffix\fR=\fIsuff\fR] [\fB--tmpdir\fR[=\fIdir\fR]] [\fItemplate\fR] .fi .SH DESCRIPTION .sp .LP The \fBmktemp\fR utility makes a temporary filename. To do this, \fBmktemp\fR takes the specified filename template and overwrites a portion of it to create a unique filename. See \fBOPERANDS\fR. .sp .LP The template is converted to a path name using the \fBmktemp\fR(3C), \fBmkdtemp\fR(3C), \fBmkstemp\fR(3C), and \fBmkstemps\fR(3C) library functions. .sp .LP If \fBmktemp\fR can successfully generate a unique filename, the file (or directory) is created with file permissions such that it is only readable and writable by its owner (unless the \fB-u\fR flag is given) and the filename is printed to standard output. .sp .LP \fBmktemp\fR allows shell scripts to safely use temporary files. Traditionally, many shell scripts take the name of the program with the \fBPID\fR as a suffix and used that as a temporary filename. 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 guarantees that a temporary file is not subverted, it still allows a simple denial of service attack. Use \fBmktemp\fR instead. .SH OPTIONS .sp .LP The following options are supported: .sp .ne 2 .mk .na \fB\fB-d\fR, \fB--directory\fR\fR .ad .sp .6 .RS 4n Make a directory instead of a file. .RE .sp .ne 2 .mk .na \fB\fB-p\fR \fIdirectory\fR\fR .ad .sp .6 .RS 4n Use the specified directory as a prefix when generating the temporary filename. The directory is overridden by the user's \fBTMPDIR\fR environment variable if it is set. This option implies the \fB-t\fR flag. .RE .sp .ne 2 .mk .na \fB\fB-q\fR, \fB--quiet\fR\fR .ad .sp .6 .RS 4n Fail silently if an error occurs. This is useful if a script does not want error output to go to standard error. .RE .sp .ne 2 .mk .na \fB\fB--suffix\fR=\fIsuff\fR\fR .ad .sp .6 .RS 4n Append \fIsuff\fR to the template. \fIsuff\fR must not contain a slash (\fB/\fR). This option is implied when the template ends in characters other than replaceable X characters, and does not remove the requirement to include such X characters. .RE .sp .ne 2 .mk .na \fB\fB--tmpdir\fR[=\fIdir\fR]\fR .ad .sp .6 .RS 4n \&'template' is relative to \fIdir\fR. If \fIdir\fR is not specified, the user's environment variable \fBTMPDIR\fR is used if set, else \fB/tmp\fR is used. This option does not support a template with an absolute name and unlike with the use of \fB-t\fR, a template may contain slashes. .RE .sp .ne 2 .mk .na \fB\fB-t\fR\fR .ad .sp .6 .RS 4n Generate a path rooted in a temporary directory. This directory is chosen as follows: If the user's \fBTMPDIR\fR environment variable is set, the directory contained therein is used. Otherwise, if the \fB-p\fR flag was given the specified directory is used. If none of the above apply, \fB/tmp\fR 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. .RE .sp .ne 2 .mk .na \fB\fB-u\fR, \fB--dry-run\fR\fR .ad .sp .6 .RS 4n Operate in unsafe mode. The temp file is unlinked before \fBmktemp\fR exits. This is slightly better than \fBmktemp\fR(3C), but still introduces a race condition. Use of this option is discouraged. .RE .SH OPERANDS .sp .LP The following operands are supported: .sp .ne 2 .mk .na \fB\fItemplate\fR\fR .ad .RS 12n .rt \fItemplate\fR can be any filename with three to six \fBX\fRs included in it, for example \fB/tmp/tfile.XXXXXX\fR. .sp If \fItemplate\fR is not specified, a default of \fBtmp.XXXXXX\fR is used and the \fB-t\fR flag is implied. .sp If \fItemplate\fR has multiple series of Xs, the final series is used for the replacement text, unless \fB--suffix\fR specifies otherwise. .sp If \fItemplate\fR has characters after the final set of Xs, then the \fB--suffix\fR option is implied to be all such characters, unless explicitly specified. .RE .SH EXAMPLES .LP \fBExample 1 \fRUsing \fBmktemp\fR .sp .LP The following example illustrates a simple use of \fBmktemp\fR in a \fBsh\fR(1) script. In this example, the script quits if it cannot get a safe temporary file. .sp .in +2 .nf TMPFILE=`mktemp /tmp/example.XXXXXX` if [ -z "$TMPFILE" ]; then exit 1; fi echo "program output" >> $TMPFILE .fi .in -2 .sp .LP \fBExample 2 \fRUsing \fBmktemp\fR to Support \fBTMPDIR\fR .sp .LP The following example uses \fBmktemp\fR to support for a user's \fBTMPDIR\fR environment variable: .sp .in +2 .nf TMPFILE=`mktemp -t example.XXXXXX` if [ -z "$TMPFILE" ]; then exit 1; fi echo "program output" >> $TMPFILE .fi .in -2 .sp .LP \fBExample 3 \fRUsing \fBmktemp\fR Without Specifying the Name of the Temporary File .sp .LP The following example uses \fBmktemp\fR without specifying the name of the temporary file. In this case the \fB-t\fR flag is implied. .sp .in +2 .nf TMPFILE=`mktemp` if [ -z "$TMPFILE" ]; then exit 1; fi echo "program output" >> $TMPFILE .fi .in -2 .sp .LP \fBExample 4 \fRUsing \fBmktemp\fR with a Default Temporary Directory Other than \fB/tmp\fR .sp .LP The following example creates the temporary file in \fB/extra/tmp\fR unless the user's \fBTMPDIR\fR environment variable specifies otherwise: .sp .in +2 .nf TMPFILE=`mktemp -p /extra/tmp example.XXXXX` if [ -z "$TMPFILE" ]; then exit 1; fi echo "program output" >> $TMPFILE .fi .in -2 .sp .LP \fBExample 5 \fRUsing \fBmktemp\fR to Remove a File .sp .LP The following example attempts to create two temporary files. If creation of the second temporary file fails, \fBmktemp\fR removes the first file before exiting: .sp .in +2 .nf TMP1=`mktemp -t example.1.XXXXXX` if [ -z "$TMP1" ]; then exit 1; fi TMP2=`mktemp -t example.2.XXXXXX` if [ -z "$TMP2" ]; then rm -f $TMP1 exit 1 fi .fi .in -2 .sp .LP \fBExample 6 \fRUsing \fBmktemp\fR .sp .LP The following example does not exit if \fBmktemp\fR is unable to create the file. That part of the script has been protected. .sp .in +2 .nf TMPFILE=`mktemp -q -t example.XXXXXX` if [ ! -z "$TMPFILE" ] then # Safe to use $TMPFILE in this block echo data > $TMPFILE ... rm -f $TMPFILE fi .fi .in -2 .sp .LP \fBExample 7 \fRUsing \fBmktemp\fR with Suffix Option .sp .LP The following command illustrates the use of the suffix option. The effect of this command is to create the temporary file \fBex.q5N.SUFF\fR. .sp .in +2 .nf # \fBmktemp --suffix=.SUFF ex.XXXXXX\fR ex.q5Ngid.SUFF .fi .in -2 .sp .LP \fBExample 8 \fRUsing Suffix and Tmpdir Options .sp .LP The following command illustrates the use of the suffix and tmpdir options. .sp .in +2 .nf # \fBmktemp --tmpdir=$HOME --suffix=.bar foo.XXXXXX\fR /root/foo.7ZaO_N.bar .fi .in -2 .sp .LP \fBExample 9 \fRUsing Directory and Suffix Options .sp .LP The following command uses both the directory and suffix options. .sp .in +2 .nf # \fBmktemp --directory --suffix=.bar foo.XXXXXX\fR foo.GSaO3d.bar # \fBls -l\fR drwx------ 2 root staff 512 Mar 19 2012 foo.GSaO.bar .fi .in -2 .sp .LP \fBExample 10 \fRSupporting a Template with Non-Trailing Xs .sp .LP The following command shows the use of the directory option with non-trailing X characters. In this command, the \fB--suffix\fR=\fIsuff\fR option is implied, where \fBbar\fR is used as the suffix. .sp .in +2 .nf # \fBmktemp XXfooXXXXXXbar\fR XXfooaFY0N6bar .fi .in -2 .sp .LP \fBExample 11 \fRUsing the Quiet and Tmpdir Options .sp .LP The following command illustrates the use of the quiet and tmpdir options. .sp .in +2 .nf # \fBmktemp --quiet --tmpdir=/tmp foo\fR [\fBNo diagnostic message is returned\fR] .fi .in -2 .sp .LP \fBExample 12 \fRUsing \fBmktemp\fR with Multiple Options .sp .LP The following command combines the use of the dry-run, tmpdir, and suffix options. .sp .in +2 .nf # \fBmktemp --dry-run --tmpdir=$HOME --suffix=SUFF\fR /root/tmp.qdaGcOSUFF # \fBls -l /root/tmp.qdaGcOSUFF\fR /root/tmp.qdaGcOSUFF: No such file or directory .fi .in -2 .sp .SH ENVIRONMENT VARIABLES .sp .LP See \fBenviron\fR(5) for descriptions of the following environment variables that affect the execution of \fBmktemp\fR with the \fB-t\fR option: \fBTMPDIR.\fR .sp .ne 2 .mk .na \fB\fBTMPDIR\fR\fR .ad .RS 10n .rt Name a directory used for creating temporary files to override system default; used by \fBmktemp\fR. .RE .SH EXIT STATUS .sp .LP The following exit values are returned: .sp .ne 2 .mk .na \fB\fB0\fR\fR .ad .RS 5n .rt Successful completion. .RE .sp .ne 2 .mk .na \fB\fB1\fR\fR .ad .RS 5n .rt An error occurred. .RE .SH ATTRIBUTES .sp .LP See \fBattributes\fR(5) for descriptions of the following attributes: .sp .sp .TS tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) . ATTRIBUTE TYPEATTRIBUTE VALUE _ Availabilitysystem/core-os _ Interface StabilityCommitted .TE .SH SEE ALSO .sp .LP \fBsh\fR(1), \fBmkdtemp\fR(3C), \fBmkstemp\fR(3C), \fBmkstemps\fR(3C), \fBmktemp\fR(3C), \fBattributes\fR(5), \fBenviron\fR(5) .SH NOTES .sp .LP The \fBmktemp\fR utility appeared in OpenBSD 2.1. The Solaris implementation uses only as many `Xs' as are significant for \fBmktemp\fR(3C), \fBmkstemp\fR(3C), and \fBmkstemps\fR(3C).