1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
.\" $Id: mktemp.man,v 1.9 2001/11/12 19:47:06 millert Exp $
.\"
.\" Copyright (c) 1996, 2000, 2001 Todd C. Miller <Todd.Miller@courtesan.com>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. The name of the author may not be used to endorse or promote products
.\" derived from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.TH MKTEMP 1 "30 September 2001"
.SH NAME
\fBmktemp\fP \- make temporary filename (unique)
.SH SYNOPSIS
\fBmktemp\fP [\fB\-V\fP] | [\fB\-dqtu\fP] [\fB\-p\fP \fIdirectory\fP] [\fItemplate\fP]
.SH DESCRIPTION
The
.B mktemp
utility takes the given filename
.I template
and overwrites a portion of it to create a unique filename.
The
.I template
may be any filename with six (6)
`Xs' appended to it, for example
.I /tmp/tfile.XXXXXX.
If no
.I template
is specified a default of
.I tmp.XXXXXX
is used and the
.B \-t
flag is implied (see below).
.PP
The trailing `Xs' are replaced with a combination
of the current process number and random letters.
.PP
If
.B mktemp
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
.B \-u
flag is given) and the filename is printed to standard output.
.PP
.B mktemp
is provided to allow shell scripts to safely use temporary
files. Traditionally, many shell scripts take the name of the program with
the PID as a suffix and use 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 does allow one to guarantee that a temporary file will not be
subverted, it still allows a simple denial of service attack.
For these reasons it is suggested that
.B mktemp
be used instead.
.PP
The options are as follows:
.TP
.B \-V
Print the version and exit.
.TP
.B \-d
Make a directory instead of a file.
.TP
.BI "\-p " directory
Use the specified
.I directory
as a prefix when generating the temporary filename.
The
.I directory
will be overridden by the user's
.SM TMPDIR
environment variable if it is set.
This option implies the
.B \-t
flag (see below).
.TP
.B \-q
Fail silently if an error occurs.
This is useful if
a script does not want error output to go to standard error.
.TP
.B \-t
Generate a path rooted in a temporary directory.
This directory is chosen as follows:
.RS
.IP \(bu
If the user's
.SM TMPDIR
environment variable is set, the directory contained therein is used.
.IP \(bu
Otherwise, if the
.B \-p
flag was given the specified directory is used.
.IP \(bu
If none of the above apply,
.I /tmp
is used.
.RE
.PP
In this mode, the
.I template
(if specified) should be a directory component (as opposed to a full path)
and thus should not contain any forward slashes.
.TP
.B \-u
Operate in ``unsafe'' mode.
The temp file will be unlinked before
.B mktemp
exits. This is slightly better than mktemp(3)
but still introduces a race condition. Use of this
option is not encouraged.
.PP
The
.B mktemp
utility
exits with a value of 0 on success or 1 on failure.
.Pp
Debian packages using
.Nm
in maintainer scripts must depend on debianutils >= 1.7.
.SH EXAMPLES
The following sh(1)
fragment illustrates a simple use of
.B mktemp
where the script should quit if it cannot get a safe
temporary file.
.RS
.nf
TMPFILE=`mktemp /tmp/example.XXXXXX` || exit 1
echo "program output" >> $TMPFILE
.fi
.RE
The same fragment with support for a user's
.SM TMPDIR
environment variable can be written as follows.
.RS
.nf
TMPFILE=`mktemp \-t example.XXXXXX` || exit 1
echo "program output" >> $TMPFILE
.fi
.RE
This can be further simplified if we don't care about the actual name of
the temporary file. In this case the
.B \-t
flag is implied.
.RS
.nf
TMPFILE=`mktemp` || exit 1
echo "program output" >> $TMPFILE
.fi
.RE
In some cases, it may be desirable to use a default temporary directory
other than
.I /tmp.
In this example the temporary file will be created in
.I /extra/tmp
unless the user's
.SM TMPDIR
environment variable specifies otherwise.
.RS
.nf
TMPFILE=`mktemp \-p /extra/tmp example.XXXXXX` || exit 1
echo "program output" >> $TMPFILE
.fi
.RE
In some cases, we want the script to catch the error.
For instance, if we attempt to create two temporary files and
the second one fails we need to remove the first before exiting.
.RS
.nf
TMP1=`mktemp \-t example.1.XXXXXX` || exit 1
TMP2=`mktemp \-t example.2.XXXXXX`
if [ $? \-ne 0 ]; then
rm \-f $TMP1
exit 1
fi
.fi
.RE
Or perhaps you don't want to exit if
.B mktemp
is unable to create the file.
In this case you can protect that part of the script thusly.
.RS
.nf
TMPFILE=`mktemp \-t example.XXXXXX` && {
# Safe to use $TMPFILE in this block
echo data > $TMPFILE
...
rm \-f $TMPFILE
}
.fi
.RE
.SH ENVIRONMENT
.IP TMPDIR 8
directory in which to place the temporary file when in
.B \-t
mode
.SH SEE ALSO
.BR mkdtemp (3),
.BR mkstemp (3),
.BR mktemp (3)
.SH HISTORY
The
.B mktemp
utility appeared in OpenBSD 2.1.
|