xref: /freebsd/usr.bin/mktemp/mktemp.1 (revision 6137b5f7b8c183ee8806d79b3f1d8e5e3ddb3df3)
1.\" Copyright (c) 1989, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
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.
15.\"
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
26.\" SUCH DAMAGE.
27.\"
28.\" From: $OpenBSD: mktemp.1,v 1.8 1998/03/19 06:13:37 millert Exp $
29.\"
30.Dd August 4, 2022
31.Dt MKTEMP 1
32.Os
33.Sh NAME
34.Nm mktemp
35.Nd make temporary file name (unique)
36.Sh SYNOPSIS
37.Nm
38.Op Fl d
39.Op Fl p Ar tmpdir
40.Op Fl q
41.Op Fl t Ar prefix
42.Op Fl u
43.Ar template ...
44.Nm
45.Op Fl d
46.Op Fl p Ar tmpdir
47.Op Fl q
48.Op Fl u
49.Fl t Ar prefix
50.Sh DESCRIPTION
51The
52.Nm
53utility takes each of the given file name templates and overwrites a
54portion of it to create a file name.
55This file name is unique
56and suitable for use by the application.
57The template may be
58any file name with some number of
59.Ql X Ns s
60appended
61to it, for example
62.Pa /tmp/temp.XXXX .
63The trailing
64.Ql X Ns s
65are replaced with the current process number and/or a
66unique letter combination.
67The number of unique file names
68.Nm
69can return depends on the number of
70.Ql X Ns s
71provided; six
72.Ql X Ns s
73will
74result in
75.Nm
76selecting 1 of 56800235584 (62 ** 6) possible file names.
77.Pp
78If
79.Nm
80can successfully generate a unique file name, the file
81is created with mode 0600 (unless the
82.Fl u
83flag is given) and the filename is printed
84to standard output.
85.Pp
86If the
87.Fl t Ar prefix
88option is given,
89.Nm
90will generate a template string based on the
91.Ar prefix
92and the
93.Ev TMPDIR
94environment variable if set.
95If the
96.Fl p
97option is set, then the given
98.Ar tmpdir
99will be used if the
100.Ev TMPDIR
101environment variable is not set.
102Finally,
103.Pa /tmp
104will be used if neither
105.Ev TMPDIR
106or
107.Fl p
108are set and used.
109Care should
110be taken to ensure that it is appropriate to use an environment variable
111potentially supplied by the user.
112.Pp
113If no arguments are passed or if only the
114.Fl d
115flag is passed
116.Nm
117behaves as if
118.Fl t Li tmp
119was supplied.
120.Pp
121Any number of temporary files may be created in a single invocation,
122including one based on the internal template resulting from the
123.Fl t
124flag.
125.Pp
126The
127.Nm
128utility is provided to allow shell scripts to safely use temporary files.
129Traditionally, many shell scripts take the name of the program with
130the pid as a suffix and use that as a temporary file name.
131This
132kind of naming scheme is predictable and the race condition it creates
133is easy for an attacker to win.
134A safer, though still inferior, approach
135is to make a temporary directory using the same naming scheme.
136While
137this does allow one to guarantee that a temporary file will not be
138subverted, it still allows a simple denial of service attack.
139For these
140reasons it is suggested that
141.Nm
142be used instead.
143.Sh OPTIONS
144The available options are as follows:
145.Bl -tag -width indent
146.It Fl d , Fl -directory
147Make a directory instead of a file.
148.It Fl p Ar tmpdir , Fl -tmpdir Ns Oo = Ns Ar tmpdir Oc
149Use
150.Ar tmpdir
151for the
152.Fl t
153flag if the
154.Ev TMPDIR
155environment variable is not set.
156Additionally, any provided
157.Ar template
158arguments will be interpreted relative to the path specified as
159.Ar tmpdir .
160If
161.Ar tmpdir
162is either empty or omitted, then the
163.Ev TMPDIR
164environment variable will be used.
165.It Fl q , Fl -quiet
166Fail silently if an error occurs.
167This is useful if
168a script does not want error output to go to standard error.
169.It Fl t Ar prefix
170Generate a template (using the supplied
171.Ar prefix
172and
173.Ev TMPDIR
174if set) to create a filename template.
175.It Fl u , Fl -dry-run
176Operate in
177.Dq unsafe
178mode.
179The temp file will be unlinked before
180.Nm
181exits.
182This is slightly better than
183.Xr mktemp 3
184but still introduces a race condition.
185Use of this
186option is not encouraged.
187.El
188.Sh EXIT STATUS
189.Ex -std
190.Sh EXAMPLES
191The following
192.Xr sh 1
193fragment illustrates a simple use of
194.Nm
195where the script should quit if it cannot get a safe
196temporary file.
197.Bd -literal -offset indent
198tempfoo=`basename $0`
199TMPFILE=`mktemp /tmp/${tempfoo}.XXXXXX` || exit 1
200echo "program output" >> $TMPFILE
201.Ed
202.Pp
203To allow the use of $TMPDIR:
204.Bd -literal -offset indent
205tempfoo=`basename $0`
206TMPFILE=`mktemp -t ${tempfoo}` || exit 1
207echo "program output" >> $TMPFILE
208.Ed
209.Pp
210In this case, we want the script to catch the error itself.
211.Bd -literal -offset indent
212tempfoo=`basename $0`
213TMPFILE=`mktemp -q /tmp/${tempfoo}.XXXXXX`
214if [ $? -ne 0 ]; then
215	echo "$0: Can't create temp file, exiting..."
216	exit 1
217fi
218.Ed
219.Sh SEE ALSO
220.Xr mkdtemp 3 ,
221.Xr mkstemp 3 ,
222.Xr mktemp 3 ,
223.Xr environ 7
224.Sh HISTORY
225A
226.Nm
227utility appeared in
228.Ox 2.1 .
229This implementation was written independently based on the
230.Ox
231man page, and
232first appeared in
233.Fx 2.2.7 .
234This man page is taken from
235.Ox .
236