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. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" From: $OpenBSD: mktemp.1,v 1.8 1998/03/19 06:13:37 millert Exp $
33 .\" $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.7.2.8 2003/02/24 22:37:42 trhodes Exp $
40 .Nd make temporary file name (unique)
56 utility takes each of the given file name templates and overwrites a
57 portion of it to create a file name. This file name is unique
58 and suitable for use by the application. The template may be
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 testing roughly 26 ** 6 combinations.
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.
96 The default location if
101 be taken to ensure that it is appropriate to use an environment variable
102 potentially supplied by the user.
104 Any number of temporary files may be created in a single invocation,
105 including one based on the internal template resulting from the
111 utility is provided to allow shell scripts to safely use temporary files.
112 Traditionally, many shell scripts take the name of the program with
113 the pid as a suffix and use that as a temporary file name. This
114 kind of naming scheme is predictable and the race condition it creates
115 is easy for an attacker to win. A safer, though still inferior, approach
116 is to make a temporary directory using the same naming scheme. While
117 this does allow one to guarantee that a temporary file will not be
118 subverted, it still allows a simple denial of service attack. For these
119 reasons it is suggested that
123 The available options are as follows:
124 .Bl -tag -width indent
126 Make a directory instead of a file.
128 Fail silently if an error occurs. This is useful if
129 a script does not want error output to go to standard error.
131 Generate a template (using the supplied
135 if set) to create a filename template.
139 mode. The temp file will be unlinked before
141 exits. This is slightly better than
143 but still introduces a race condition. Use of this
144 option is not encouraged.
150 exits 0 on success, and 1 if an error occurs.
154 fragment illustrates a simple use of
156 where the script should quit if it cannot get a safe
158 .Bd -literal -offset indent
159 tempfoo=`basename $0`
160 TMPFILE=`mktemp /tmp/${tempfoo}.XXXXXX` || exit 1
161 echo "program output" >> $TMPFILE
164 To allow the use of $TMPDIR:
165 .Bd -literal -offset indent
166 tempfoo=`basename $0`
167 TMPFILE=`mktemp -t ${tempfoo}` || exit 1
168 echo "program output" >> $TMPFILE
171 In this case, we want the script to catch the error itself.
172 .Bd -literal -offset indent
173 tempfoo=`basename $0`
174 TMPFILE=`mktemp -q /tmp/${tempfoo}.XXXXXX`
175 if [ $? -ne 0 ]; then
176 echo "$0: Can't create temp file, exiting..."
190 This implementation was written independently based on the
195 This man page is taken from