2 .\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved.
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH MKTEMP 1 "Jan 10, 2008"
8 mktemp \- make temporary filename
12 \fBmktemp\fR [\fB-dtqu\fR] [\fB-p\fR \fIdirectory\fR] [\fItemplate\fR]
18 The \fBmktemp\fR utility makes a temporary filename. To do this, \fBmktemp\fR
19 takes the specified filename template and overwrites a portion of it to create
20 a unique filename. See \fBOPERANDS\fR.
23 The template is passed to \fBmkdtemp\fR(3C) for directories or
24 \fBmkstemp\fR(3C) for ordinary files.
27 If \fBmktemp\fR can successfully generate a unique filename, the file (or
28 directory) is created with file permissions such that it is only readable and
29 writable by its owner (unless the \fB-u\fR flag is given) and the filename is
30 printed to standard output.
33 \fBmktemp\fR allows shell scripts to safely use temporary files. Traditionally,
34 many shell scripts take the name of the program with the \fBPID\fR as a suffix
35 and used that as a temporary filename. This kind of naming scheme is
36 predictable and the race condition it creates is easy for an attacker to win. A
37 safer, though still inferior approach is to make a temporary directory using
38 the same naming scheme. While this guarantees that a temporary file is not
39 subverted, it still allows a simple denial of service attack. Use \fBmktemp\fR
44 The following options are supported:
51 Make a directory instead of a file.
57 \fB\fB-p\fR \fIdirectory\fR\fR
60 Use the specified directory as a prefix when generating the temporary filename.
61 The directory is overridden by the user's TMPDIR environment variable if it is
62 set. This option implies the \fB-t\fR flag.
71 Fail silently if an error occurs. This is useful if a script does not want
72 error output to go to standard error.
81 Generate a path rooted in a temporary directory. This directory is chosen as
82 follows: If the user's TMPDIR environment variable is set, the directory
83 contained therein is used. Otherwise, if the \fB-p\fR flag was given the
84 specified directory is used. If none of the above apply, \fB/tmp\fR is used. In
85 this mode, the template (if specified) should be a directory component (as
86 opposed to a full path) and thus should not contain any forward slashes.
95 Operate in unsafe mode. The temp file is unlinked before \fBmktemp\fR exits.
96 This is slightly better than \fBmktemp\fR(3C), but still introduces a race
97 condition. Use of this option is discouraged.
103 The following operands are supported:
110 \fItemplate\fR can be any filename with one or more \fBX\fRs appended to it,
111 for example \fB/tmp/tfile.XXXXXX\fR.
113 If \fItemplate\fR is not specified, a default of \fBtmp.XXXXXX\fR is used and
114 the \fB-t\fR flag is implied.
119 \fBExample 1 \fRUsing \fBmktemp\fR
122 The following example illustrates a simple use of \fBmktemp\fR in a \fBsh\fR(1)
123 script. In this example, the script quits if it cannot get a safe temporary
129 TMPFILE=`mktemp /tmp/example.XXXXXX`
130 if [ -z "$TMPFILE" ]; then exit 1; fi
131 echo "program output" >> $TMPFILE
137 \fBExample 2 \fRUsing \fBmktemp\fR to Support \fBTMPDIR\fR
140 The following example uses \fBmktemp\fR to support for a user's \fBTMPDIR\fR
141 environment variable:
146 TMPFILE=`mktemp -t example.XXXXXX`
147 if [ -z "$TMPFILE" ]; then exit 1; fi
148 echo "program output" >> $TMPFILE
154 \fBExample 3 \fRUsing \fBmktemp\fR Without Specifying the Name of the Temporary
158 The following example uses \fBmktemp\fR without specifying the name of the
159 temporary file. In this case the \fB-t\fR flag is implied.
165 if [ -z "$TMPFILE" ]; then exit 1; fi
166 echo "program output" >> $TMPFILE
172 \fBExample 4 \fRUsing \fBmktemp\fR with a Default Temporary Directory Other
176 The following example creates the temporary file in \fB/extra/tmp\fR unless the
177 user's \fBTMPDIR\fR environment variable specifies otherwise:
182 TMPFILE=`mktemp -p /extra/tmp example.XXXXX`
183 if [ -z "$TMPFILE" ]; then exit 1; fi
184 echo "program output" >> $TMPFILE
190 \fBExample 5 \fRUsing \fBmktemp\fR to Remove a File
193 The following example attempts to create two temporary files. If creation of
194 the second temporary file fails, \fBmktemp\fR removes the first file before
200 TMP1=`mktemp -t example.1.XXXXXX`
201 if [ -z "$TMP1" ]; then exit 1; fi
202 TMP2=`mktemp -t example.2.XXXXXX`
203 if [ -z "$TMP2" ]; then
212 \fBExample 6 \fRUsing \fBmktemp\fR
215 The following example does not exit if \fBmktemp\fR is unable to create the
216 file. That part of the script has been protected.
221 TMPFILE=`mktemp -q -t example.XXXXXX`
222 if [ ! -z "$TMPFILE" ]
224 # Safe to use $TMPFILE in this block
233 .SH ENVIRONMENT VARIABLES
236 See \fBenviron\fR(5) for descriptions of the following environment variables
237 that affect the execution of \fBmktemp\fR with the \fB-t\fR option:
242 The following exit values are returned:
249 Successful completion.
264 See \fBattributes\fR(5) for descriptions of the following attributes:
272 ATTRIBUTE TYPE ATTRIBUTE VALUE
274 Interface Stability Committed
280 \fBsh\fR(1), \fBmkdtemp\fR(3C), \fBmkstemp\fR(3C), \fBattributes\fR(5),
285 The \fBmktemp\fR utility appeared in OpenBSD 2.1. The Solaris implementation
286 uses only as many `Xs' as are significant for \fBmktemp\fR(3C) and