1 .\" $NetBSD: mktemp.1,v 1.18 2009/08/15 20:02:28 christos 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)
52 utility takes each of the given file name templates and overwrites a
53 portion of it to create a file name.
54 This file name is unique and suitable for use by the application.
55 The template may be any file name with some number of
57 appended to it, for example
61 are replaced with the current process number and/or a
62 unique letter combination.
63 The number of unique file names
65 can return depends on the number of
71 testing roughly 26 ** 6 combinations.
75 can successfully generate a unique file name, the file
76 is created with mode 0600 (unless the
78 flag is given) and the filename is printed to standard output.
84 will generate a template string based on the
88 environment variable, if set.
89 The default location if
93 The default location of the temporary directory can be overridden with the
96 The template string created will consist of the
100 and an eight character unique letter combination.
104 string will be treated as literal.
107 argument is passed, a second file will be created.
108 Care should be taken to ensure that it is appropriate to use an
109 environment variable potentially supplied by the user.
111 Any number of temporary files may be created in a single invocation
114 arguments, also a single one based on the internal template with the
116 option value as filename prefix.
122 option must be present.
125 is provided to allow shell scripts to safely use temporary files.
126 Traditionally, many shell scripts take the name of the program with
127 the pid as a suffix and use that as a temporary file name.
128 This kind of naming scheme is predictable and the race condition
129 it creates is easy for an attacker to win.
130 A safer, though still inferior, approach
131 is to make a temporary directory using the same naming scheme.
132 While this does allow one to guarantee that a temporary file will
133 not be subverted, it still allows a simple denial of service attack.
134 For these reasons it is suggested that
138 The available options are as follows:
139 .Bl -tag -width indent
141 Make a directory instead of a file.
143 Fail silently if an error occurs.
145 a script does not want error output to go to standard error.
147 Generate a template (using the supplied
151 if set) to create a filename template.
164 The temp file will be unlinked before
167 This is slightly better than
169 but still introduces a race condition.
170 Use of this option is not encouraged.
175 utility exits with a value of 0 on success, and 1 on any failure.
179 fragment illustrates a simple use of
181 where the script should quit if it cannot get a safe
183 .Bd -literal -offset indent
184 TMPFILE=`mktemp /tmp/${0##*/}.XXXXXX` || exit 1
185 echo "program output" \*[Gt]\*[Gt] $TMPFILE
188 To allow the use of $TMPDIR:
189 .Bd -literal -offset indent
190 TMPFILE=`mktemp -t ${0##*/}` || exit 1
191 echo "program output" \*[Gt]\*[Gt] $TMPFILE
194 In this case, we want the script to catch the error itself.
195 .Bd -literal -offset indent
196 TMPFILE=`mktemp -q /tmp/${0##*/}.XXXXXX`
197 if [ $? -ne 0 ]; then
198 echo "$0: Can't create temp file, exiting..."
212 It has been imported from
214 the idea and the manual page were taken from