1 .\" Copyright (c) 1989, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" From: $OpenBSD: mktemp.1,v 1.8 1998/03/19 06:13:37 millert Exp $
36 .Nd make temporary file name (unique)
54 utility takes each of the given file name templates and overwrites a
55 portion of it to create a file name.
56 This file name is unique
57 and suitable for use by the application.
59 any file name with some number of
66 are replaced with the current process number and/or a
67 unique letter combination.
68 The number of unique file names
70 can return depends on the number of
77 selecting 1 of 56800235584 (62 ** 6) possible file names.
81 can successfully generate a unique file name, the file
82 is created with mode 0600 (unless the
84 flag is given) and the filename is printed
91 will generate a template string based on the
95 environment variable if set.
98 option is set, then the given
102 environment variable is not set.
105 will be used if neither
111 be taken to ensure that it is appropriate to use an environment variable
112 potentially supplied by the user.
114 If no arguments are passed or if only the
122 Any number of temporary files may be created in a single invocation,
123 including one based on the internal template resulting from the
129 utility is provided to allow shell scripts to safely use temporary files.
130 Traditionally, many shell scripts take the name of the program with
131 the pid as a suffix and use that as a temporary file name.
133 kind of naming scheme is predictable and the race condition it creates
134 is easy for an attacker to win.
135 A safer, though still inferior, approach
136 is to make a temporary directory using the same naming scheme.
138 this does allow one to guarantee that a temporary file will not be
139 subverted, it still allows a simple denial of service attack.
141 reasons it is suggested that
145 The available options are as follows:
146 .Bl -tag -width indent
147 .It Fl d , Fl -directory
148 Make a directory instead of a file.
149 .It Fl p Ar tmpdir , Fl -tmpdir Ns Oo = Ns Ar tmpdir Oc
156 environment variable is not set.
157 Additionally, any provided
159 arguments will be interpreted relative to the path specified as
163 is either empty or omitted, then the
165 environment variable will be used.
167 Fail silently if an error occurs.
169 a script does not want error output to go to standard error.
171 Generate a template (using the supplied
175 if set) to create a filename template.
176 .It Fl u , Fl -dry-run
180 The temp file will be unlinked before
183 This is slightly better than
185 but still introduces a race condition.
187 option is not encouraged.
194 fragment illustrates a simple use of
196 where the script should quit if it cannot get a safe
198 .Bd -literal -offset indent
199 tempfoo=`basename $0`
200 TMPFILE=`mktemp /tmp/${tempfoo}.XXXXXX` || exit 1
201 echo "program output" >> $TMPFILE
204 To allow the use of $TMPDIR:
205 .Bd -literal -offset indent
206 tempfoo=`basename $0`
207 TMPFILE=`mktemp -t ${tempfoo}` || exit 1
208 echo "program output" >> $TMPFILE
211 In this case, we want the script to catch the error itself.
212 .Bd -literal -offset indent
213 tempfoo=`basename $0`
214 TMPFILE=`mktemp -q /tmp/${tempfoo}.XXXXXX`
215 if [ $? -ne 0 ]; then
216 echo "$0: Can't create temp file, exiting..."
230 This implementation was written independently based on the
235 This man page is taken from