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