1 .\" $NetBSD: mktemp.1,v 1.22 2014/11/10 07:33:31 snj Exp $
2 .\" From: $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $
3 .\" From: $OpenBSD: mktemp.1,v 1.8 1998/03/19 06:13:37 millert Exp $
5 .\" Copyright (c) 1989, 1991, 1993
6 .\" The Regents of the University of California. All rights reserved.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. 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 .\" $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $
39 .Nd make temporary file name (unique)
53 is provided to allow shell scripts to safely use temporary files.
54 It creates temporary files or directories using unique names,
57 The name of each temporary file or directory is derived from a
58 template that includes several trailing
61 .Pa /tmp/prefix.XXXX .
64 characters in the template are replaced by unique values derived from
65 the current process number and additional letters or numbers.
68 characters other than at the end of the template are taken literally.
69 The number of unique file names
71 can return depends on the number of trailing
77 testing roughly 26 ** 6 combinations.
79 The templates used to create the unique names are derived from the
83 arguments, possibly modified by other options.
84 Any number of temporary files or directories may be created
85 in a single invocation using multiple
88 It is possible to specify both a
90 option and one or more
93 but this is not usually done.
99 arguments are specified, then the default is equivalent to
104 can successfully generate a unique file name, the file
105 is created with mode 0600 (unless the
107 flag is given) and the filename is printed to standard output.
109 The available options are as follows:
110 .Bl -tag -width indent
112 Make a directory instead of a file.
114 Specifies a directory in which temporary files should be created.
115 If this option is specified, then it applies to all temporary files,
116 including those created as a result of a
118 option, and those created as a result of a
124 option is not specified, then
125 temporary files created as a result of a
127 option will use a default temporary directory
128 (as described under the
131 but temporary files created as a result of a
133 argument will not use a default temporary directory
134 (so they will be created relative to the current working directory, if the
139 Generate a template using an appropriate directory name, followed by the
146 characters in the supplied
148 are taken literally, but the trailing
150 characters in the appended
152 are replaced by unique values.
154 The directory name used for the template generated by the
156 option is taken from the
160 environment variable, or
166 arguments are used in addition to the
170 does not apply to the
174 Fail silently if an error occurs.
176 a script does not want error output to go to standard error.
181 The temp file will be unlinked before
184 This is slightly better than
186 but still introduces a race condition.
187 Use of this option is not encouraged.
191 takes care to create the files or directories in a way that is
192 safe from race conditions (provided the
196 Traditionally, without
198 many shell scripts created temporary files
199 using the name of the program with
201 This kind of naming scheme is predictable and creates a race condition that
202 allows an attacker to subvert the program by
203 creating a different file, directory, or symbolic link
205 A safer, though still inferior, approach
206 is to make a temporary directory using the same naming scheme
207 While this does allow one to guarantee that a temporary file will
208 not be subverted, it still allows a simple denial of service attack.
209 For these reasons it is recommended that
211 be used instead of simpler schemes.
213 Care should be taken to ensure that it is appropriate to use an
214 environment variable potentially supplied by the user.
218 utility exits with a value of 0 on success, and 1 on any failure.
222 fragment illustrates a simple use of
224 where the script should quit if it cannot get a safe
226 .Bd -literal -offset indent
227 TMPFILE=`mktemp /tmp/${0##*/}.XXXXXX` || exit 1
228 echo "program output" \*[Gt]\*[Gt] $TMPFILE
231 To allow the use of $TMPDIR:
232 .Bd -literal -offset indent
233 TMPFILE=`mktemp -t ${0##*/}` || exit 1
234 echo "program output" \*[Gt]\*[Gt] $TMPFILE
237 In this case, we want the script to catch the error itself.
238 .Bd -literal -offset indent
239 TMPFILE=`mktemp -q /tmp/${0##*/}.XXXXXX`
240 if [ $? -ne 0 ]; then
241 echo "$0: Can't create temp file, exiting..."
257 and the idea and the manual page were taken from