1.\" $NetBSD: mktemp.1,v 1.21 2012/11/04 10:10:03 apb 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 $ 4.\" 5.\" Copyright (c) 1989, 1991, 1993 6.\" The Regents of the University of California. All rights reserved. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 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. 19.\" 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 30.\" SUCH DAMAGE. 31.\" 32.\" $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $ 33.\" 34.Dd November 4, 2012 35.Dt MKTEMP 1 36.Os 37.Sh NAME 38.Nm mktemp 39.Nd make temporary file name (unique) 40.Sh SYNOPSIS 41.Nm mktemp 42.Op Fl dqu 43.Op Fl p Ar tmpdir 44.Bro 45.Fl t Ar prefix 46.No | 47.Ar template ... 48.Brc 49.Sh DESCRIPTION 50The 51.Nm 52utility 53is provided to allow shell scripts to safely use temporary files. 54It creates temporary files or directories using unique names, 55and prints the names. 56.Pp 57The name of each temporary file or directory is derived from a 58template that includes several trailing 59.Ql X 60characters, such as 61.Pa /tmp/prefix.XXXX . 62The trailing 63.Ql X 64characters in the template are replaced by unique values derived from 65the current process number and additional letters or numbers. 66Any 67.Ql X 68characters other than at the end of the template are taken literally. 69The number of unique file names 70.Nm 71can return depends on the number of trailing 72.Ql X Ns s 73in the template; six 74.Ql X Ns s 75will result in 76.Nm 77testing roughly 26 ** 6 combinations. 78.Pp 79The templates used to create the unique names are derived from the 80.Fl t Ar prefix 81option, or the 82.Ar template 83arguments, possibly modified by other options. 84Any number of temporary files or directories may be created 85in a single invocation using multiple 86.Ar template 87arguments. 88It is possible to specify both a 89.Fl t Ar prefix 90option and one or more 91.Ar template 92arguments, 93but this is not usually done. 94.Pp 95If neither a 96.Fl t Ar prefix 97option, nor any 98.Ar template 99arguments are specified, then the default is equivalent to 100.Fl t Li mktemp . 101.Pp 102If 103.Nm 104can successfully generate a unique file name, the file 105is created with mode 0600 (unless the 106.Fl u 107flag is given) and the filename is printed to standard output. 108.Sh OPTIONS 109The available options are as follows: 110.Bl -tag -width indent 111.It Fl d 112Make a directory instead of a file. 113.It Fl p Ar tmpdir 114Specifies a directory in which temporary files should be created. 115If this option is specified, then it applies to all temporary files, 116including those created as a result of a 117.Fl t Ar prefix 118option, and those created as a result of a 119.Ar template 120argument. 121.Pp 122If the 123.Fl p Ar tmpdir 124option is not specified, then 125temporary files created as a result of a 126.Fl t Ar prefix 127option will use a default temporary directory 128(as described under the 129.Fl t 130option), 131but temporary files created as a result of a 132.Ar template 133argument will not use a default temporary directory 134(so they will be created relative to the current working directory, if the 135.Ar template 136does not begin with 137.Ql \&/ ) . 138.It Fl t Ar prefix 139Generate a template using an appropriate directory name, followed by the 140supplied 141.Ar prefix , 142followed by 143.Ql \&.XXXXXXXX . 144Any 145.Ql X 146characters in the supplied 147.Ar prefix 148are taken literally, but the trailing 149.Ql X 150characters in the appended 151.Ql \&.XXXXXXXX 152are replaced by unique values. 153.Pp 154The directory name used for the template generated by the 155.Fl t Ar prefix 156option is taken from the 157.Fl p Ar tmpdir 158option, or from the 159.Ev TMPDIR 160environment variable, or 161.Pa /tmp 162as a default. 163.Pp 164If one or more 165.Ar template 166arguments are used in addition to the 167.Fl t Ar prefix 168option, then the 169.Ar prefix 170does not apply to the 171.Ar template 172arguments. 173.It Fl q 174Fail silently if an error occurs. 175This is useful if 176a script does not want error output to go to standard error. 177.It Fl u 178Operate in 179.Dq unsafe 180mode. 181The temp file will be unlinked before 182.Nm 183exits. 184This is slightly better than 185.Xr mktemp 3 186but still introduces a race condition. 187Use of this option is not encouraged. 188.El 189.Sh NOTES 190.Nm 191takes care to create the files or directories in a way that is 192safe from race conditions (provided the 193.Fl u 194option is not used). 195.Pp 196Traditionally, without 197.Nm , 198many shell scripts created temporary files 199using the name of the program with 200the pid as a suffix. 201This kind of naming scheme is predictable and creates a race condition that 202allows an attacker to subvert the program by 203creating a different file, directory, or symbolic link 204under the same name. 205A safer, though still inferior, approach 206is to make a temporary directory using the same naming schem 207While this does allow one to guarantee that a temporary file will 208not be subverted, it still allows a simple denial of service attack. 209For these reasons it is recommended that 210.Nm 211should be used instead of simpler schemes. 212.Pp 213Care should be taken to ensure that it is appropriate to use an 214environment variable potentially supplied by the user. 215.Sh EXIT STATUS 216The 217.Nm 218utility exits with a value of 0 on success, and 1 on any failure. 219.Sh EXAMPLES 220The following 221.Xr sh 1 222fragment illustrates a simple use of 223.Nm 224where the script should quit if it cannot get a safe 225temporary file. 226.Bd -literal -offset indent 227TMPFILE=`mktemp /tmp/${0##*/}.XXXXXX` || exit 1 228echo "program output" \*[Gt]\*[Gt] $TMPFILE 229.Ed 230.Pp 231To allow the use of $TMPDIR: 232.Bd -literal -offset indent 233TMPFILE=`mktemp -t ${0##*/}` || exit 1 234echo "program output" \*[Gt]\*[Gt] $TMPFILE 235.Ed 236.Pp 237In this case, we want the script to catch the error itself. 238.Bd -literal -offset indent 239TMPFILE=`mktemp -q /tmp/${0##*/}.XXXXXX` 240if [ $? -ne 0 ]; then 241 echo "$0: Can't create temp file, exiting..." 242 exit 1 243fi 244.Ed 245.Sh SEE ALSO 246.Xr mkdtemp 3 , 247.Xr mkstemp 3 , 248.Xr mktemp 3 , 249.Xr environ 7 250.Sh HISTORY 251The 252.Nm 253utility appeared in 254.Nx 1.5 . 255It has been imported from 256.Fx , 257the idea and the manual page were taken from 258.Ox . 259