1mktemp(1) User Commands mktemp(1)
2
3
4
6 mktemp - make temporary filename
7
9 mktemp [-dtqu] [-p directory] [template]
10
11
13 The mktemp utility makes a temporary filename. To do this, mktemp takes
14 the specified filename template and overwrites a portion of it to cre‐
15 ate a unique filename. See OPERANDS.
16
17
18 The template is passed to mkdtemp(3C) for directories or mkstemp(3C)
19 for ordinary files.
20
21
22 If mktemp can successfully generate a unique filename, the file (or
23 directory) is created with file permissions such that it is only read‐
24 able and writable by its owner (unless the -u flag is given) and the
25 filename is printed to standard output.
26
27
28 mktemp allows shell scripts to safely use temporary files. Tradition‐
29 ally, many shell scripts take the name of the program with the PID as a
30 suffix and used that as a temporary filename. This kind of naming
31 scheme is predictable and the race condition it creates is easy for an
32 attacker to win. A safer, though still inferior approach is to make a
33 temporary directory using the same naming scheme. While this guarantees
34 that a temporary file is not subverted, it still allows a simple denial
35 of service attack. Use mktemp instead.
36
38 The following options are supported:
39
40 -d Make a directory instead of a file.
41
42
43 -p directory Use the specified directory as a prefix when generating
44 the temporary filename. The directory is overridden by
45 the user's TMPDIR environment variable if it is set.
46 This option implies the -t flag.
47
48
49 -q Fail silently if an error occurs. This is useful if a
50 script does not want error output to go to standard
51 error.
52
53
54 -t Generate a path rooted in a temporary directory. This
55 directory is chosen as follows: If the user's TMPDIR
56 environment variable is set, the directory contained
57 therein is used. Otherwise, if the -p flag was given
58 the specified directory is used. If none of the above
59 apply, /tmp is used. In this mode, the template (if
60 specified) should be a directory component (as opposed
61 to a full path) and thus should not contain any forward
62 slashes.
63
64
65 -u Operate in unsafe mode. The temp file is unlinked
66 before mktemp exits. This is slightly better than
67 mktemp(3C), but still introduces a race condition. Use
68 of this option is discouraged.
69
70
72 The following operands are supported:
73
74 template template can be any filename with one or more Xs appended
75 to it, for example /tmp/tfile.XXXXXX.
76
77 If template is not specified, a default of tmp.XXXXXX is
78 used and the -t flag is implied.
79
80
82 Example 1 Using mktemp
83
84
85 The following example illustrates a simple use of mktemp in a sh(1)
86 script. In this example, the script quits if it cannot get a safe tem‐
87 porary file.
88
89
90 TMPFILE=`mktemp /tmp/example.XXXXXX`
91 if [ -z "$TMPFILE" ]; then exit 1; fi
92 echo "program output" >> $TMPFILE
93
94
95
96 Example 2 Using mktemp to Support TMPDIR
97
98
99 The following example uses mktemp to support for a user's TMPDIR envi‐
100 ronment variable:
101
102
103 TMPFILE=`mktemp -t example.XXXXXX`
104 if [ -z "$TMPFILE" ]; then exit 1; fi
105 echo "program output" >> $TMPFILE
106
107
108
109 Example 3 Using mktemp Without Specifying the Name of the Temporary
110 File
111
112
113 The following example uses mktemp without specifying the name of the
114 temporary file. In this case the -t flag is implied.
115
116
117 TMPFILE=`mktemp`
118 if [ -z "$TMPFILE" ]; then exit 1; fi
119 echo "program output" >> $TMPFILE
120
121
122
123 Example 4 Using mktemp with a Default Temporary Directory Other than
124 /tmp
125
126
127 The following example creates the temporary file in /extra/tmp unless
128 the user's TMPDIR environment variable specifies otherwise:
129
130
131 TMPFILE=`mktemp -p /extra/tmp example.XXXXX`
132 if [ -z "$TMPFILE" ]; then exit 1; fi
133 echo "program output" >> $TMPFILE
134
135
136
137 Example 5 Using mktemp to Remove a File
138
139
140 The following example attempts to create two temporary files. If cre‐
141 ation of the second temporary file fails, mktemp removes the first file
142 before exiting:
143
144
145 TMP1=`mktemp -t example.1.XXXXXX`
146 if [ -z "$TMP1" ]; then exit 1; fi
147 TMP2=`mktemp -t example.2.XXXXXX`
148 if [ -z "$TMP2" ]; then
149 rm -f $TMP1
150 exit 1
151 fi
152
153
154
155 Example 6 Using mktemp
156
157
158 The following example does not exit if mktemp is unable to create the
159 file. That part of the script has been protected.
160
161
162 TMPFILE=`mktemp -q -t example.XXXXXX`
163 if [ ! -z "$TMPFILE" ]
164 then
165 # Safe to use $TMPFILE in this block
166 echo data > $TMPFILE
167 ...
168 rm -f $TMPFILE
169 fi
170
171
172
174 See environ(5) for descriptions of the following environment variables
175 that affect the execution of mktemp with the -t option: TMPDIR.
176
178 The following exit values are returned:
179
180 0 Successful completion.
181
182
183 1 An error occurred.
184
185
187 See attributes(5) for descriptions of the following attributes:
188
189
190
191
192 ┌─────────────────────────────┬─────────────────────────────┐
193 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
194 ├─────────────────────────────┼─────────────────────────────┤
195 │Availability │SUNWcsu │
196 ├─────────────────────────────┼─────────────────────────────┤
197 │Interface Stability │Committed │
198 └─────────────────────────────┴─────────────────────────────┘
199
201 sh(1), mkdtemp(3C), mkstemp(3C), attributes(5), environ(5)
202
204 The mktemp utility appeared in OpenBSD 2.1. The Solaris implementation
205 uses only as many `Xs' as are significant for mktemp(3C) and
206 mkstemp(3C).
207
208
209
210SunOS 5.11 10 Jan 2008 mktemp(1)