2024-03-03 07:24:45 -08:00
|
|
|
.\" $OpenBSD: mktemp.1,v 1.34 2024/03/03 15:24:45 millert Exp $
|
1996-11-20 23:59:31 -08:00
|
|
|
.\"
|
2024-03-01 13:50:40 -08:00
|
|
|
.\" Copyright (c) 1996, 2000, 2001, 2003, 2010, 2013, 2024
|
2019-01-24 16:19:25 -08:00
|
|
|
.\" Todd C. Miller <millert@openbsd.org>
|
1996-11-20 23:59:31 -08:00
|
|
|
.\"
|
2003-06-02 18:52:39 -07:00
|
|
|
.\" Permission to use, copy, modify, and distribute this software for any
|
|
|
|
.\" purpose with or without fee is hereby granted, provided that the above
|
|
|
|
.\" copyright notice and this permission notice appear in all copies.
|
1996-11-20 23:59:31 -08:00
|
|
|
.\"
|
2003-06-17 14:56:23 -07:00
|
|
|
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1996-11-20 23:59:31 -08:00
|
|
|
.\"
|
2024-03-03 07:24:45 -08:00
|
|
|
.Dd $Mdocdate: March 3 2024 $
|
1996-11-20 23:59:31 -08:00
|
|
|
.Dt MKTEMP 1
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm mktemp
|
2024-03-03 07:24:45 -08:00
|
|
|
.Nd make temporary file or directory (unique)
|
1996-11-20 23:59:31 -08:00
|
|
|
.Sh SYNOPSIS
|
|
|
|
.Nm mktemp
|
2001-10-01 10:08:30 -07:00
|
|
|
.Op Fl dqtu
|
|
|
|
.Op Fl p Ar directory
|
2001-10-10 17:05:55 -07:00
|
|
|
.Op Ar template
|
1996-11-20 23:59:31 -08:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
|
|
|
.Nm mktemp
|
2024-03-03 07:24:45 -08:00
|
|
|
utility takes the specified file name
|
2001-10-10 17:05:55 -07:00
|
|
|
.Ar template
|
2024-03-03 07:24:45 -08:00
|
|
|
and overwrites a portion of it to create a unique file name.
|
2001-10-10 17:05:55 -07:00
|
|
|
The
|
|
|
|
.Ar template
|
2024-03-03 07:24:45 -08:00
|
|
|
may be any file name containing at least six
|
1996-11-20 23:59:31 -08:00
|
|
|
.Ql X Ns s
|
2024-03-03 07:24:45 -08:00
|
|
|
in the last component of the path, for example
|
2024-03-01 13:50:40 -08:00
|
|
|
.Pa /tmp/tfile.XXXXXXXXXX
|
|
|
|
or
|
|
|
|
.Pa /tmp/editor.XXXXXXXXXX.txt .
|
|
|
|
If there is more than one run of
|
|
|
|
.Ql X Ns s
|
|
|
|
in the
|
|
|
|
.Ar template ,
|
2024-03-01 15:37:42 -08:00
|
|
|
only the last one will be considered.
|
2001-10-10 17:05:55 -07:00
|
|
|
If no
|
|
|
|
.Ar template
|
2024-03-03 07:24:45 -08:00
|
|
|
is specified, a default value of
|
2001-10-10 17:05:55 -07:00
|
|
|
.Pa tmp.XXXXXXXXXX
|
|
|
|
is used and the
|
|
|
|
.Fl t
|
|
|
|
flag is implied (see below).
|
2000-03-10 11:07:20 -08:00
|
|
|
.Pp
|
2024-03-01 13:50:40 -08:00
|
|
|
The final
|
1996-11-20 23:59:31 -08:00
|
|
|
.Ql X Ns s
|
2010-12-27 13:18:44 -08:00
|
|
|
are replaced with a unique digit and letter combination.
|
2000-03-10 11:07:20 -08:00
|
|
|
The name chosen depends both on the number of
|
2000-02-06 17:38:11 -08:00
|
|
|
.Ql X Ns s
|
2001-10-10 17:05:55 -07:00
|
|
|
in the
|
|
|
|
.Ar template
|
2024-03-03 07:24:45 -08:00
|
|
|
and the number of collisions with pre-existing entries.
|
|
|
|
The number of unique names
|
1996-11-20 23:59:31 -08:00
|
|
|
.Nm
|
|
|
|
can return depends on the number of
|
|
|
|
.Ql X Ns s
|
2000-02-06 17:38:11 -08:00
|
|
|
provided; ten
|
1996-11-20 23:59:31 -08:00
|
|
|
.Ql X Ns s
|
|
|
|
will
|
|
|
|
result in
|
|
|
|
.Nm
|
2000-02-06 17:38:11 -08:00
|
|
|
testing roughly 26 ** 10 combinations.
|
1996-11-20 23:59:31 -08:00
|
|
|
.Pp
|
|
|
|
If
|
|
|
|
.Nm
|
2024-03-03 07:24:45 -08:00
|
|
|
successfully generates a unique name, the file (or directory)
|
|
|
|
is created with permissions such that it is only readable and writable
|
2000-02-06 17:38:11 -08:00
|
|
|
by its owner (unless the
|
1996-11-20 23:59:31 -08:00
|
|
|
.Fl u
|
2024-03-03 07:24:45 -08:00
|
|
|
flag is given) and the name is printed to the standard output.
|
1998-03-18 22:13:37 -08:00
|
|
|
.Pp
|
1998-09-26 12:54:41 -07:00
|
|
|
.Nm mktemp
|
1998-03-18 22:13:37 -08:00
|
|
|
is provided to allow shell scripts to safely use temporary files.
|
|
|
|
Traditionally, many shell scripts take the name of the program with
|
2024-03-03 07:24:45 -08:00
|
|
|
the PID as a suffix and use that as a temporary file name.
|
2000-03-10 11:07:20 -08:00
|
|
|
This kind of naming scheme is predictable and the race condition it creates
|
|
|
|
is easy for an attacker to win.
|
2024-03-03 07:24:45 -08:00
|
|
|
A safer, though still inferior approach,
|
2000-03-10 11:07:20 -08:00
|
|
|
is to make a temporary directory using the same naming scheme.
|
|
|
|
While this does allow one to guarantee that a temporary file will not be
|
|
|
|
subverted, it still allows a simple denial of service attack.
|
|
|
|
For these reasons it is suggested that
|
1998-03-18 22:13:37 -08:00
|
|
|
.Nm
|
|
|
|
be used instead.
|
2000-03-04 16:28:49 -08:00
|
|
|
.Pp
|
|
|
|
The options are as follows:
|
2000-11-09 09:51:53 -08:00
|
|
|
.Bl -tag -width Ds
|
1997-06-17 08:34:27 -07:00
|
|
|
.It Fl d
|
|
|
|
Make a directory instead of a file.
|
2001-10-01 10:08:30 -07:00
|
|
|
.It Fl p Ar directory
|
|
|
|
Use the specified
|
|
|
|
.Ar directory
|
2024-03-03 07:24:45 -08:00
|
|
|
as a prefix when generating the temporary name.
|
2001-10-01 10:08:30 -07:00
|
|
|
The
|
|
|
|
.Ar directory
|
|
|
|
will be overridden by the user's
|
|
|
|
.Ev TMPDIR
|
|
|
|
environment variable if it is set.
|
|
|
|
This option implies the
|
|
|
|
.Fl t
|
|
|
|
flag (see below).
|
1997-06-17 08:34:27 -07:00
|
|
|
.It Fl q
|
2000-03-10 11:07:20 -08:00
|
|
|
Fail silently if an error occurs.
|
|
|
|
This is useful if
|
1997-06-17 08:34:27 -07:00
|
|
|
a script does not want error output to go to standard error.
|
2001-10-01 10:08:30 -07:00
|
|
|
.It Fl t
|
2001-10-10 17:05:55 -07:00
|
|
|
Generate a path rooted in a temporary directory.
|
|
|
|
This directory is chosen as follows:
|
2001-10-01 10:08:30 -07:00
|
|
|
.Bl -bullet
|
|
|
|
.It
|
|
|
|
If the user's
|
|
|
|
.Ev TMPDIR
|
|
|
|
environment variable is set, the directory contained therein is used.
|
|
|
|
.It
|
|
|
|
Otherwise, if the
|
|
|
|
.Fl p
|
|
|
|
flag was given the specified directory is used.
|
|
|
|
.It
|
|
|
|
If none of the above apply,
|
|
|
|
.Pa /tmp
|
|
|
|
is used.
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
In this mode, the
|
|
|
|
.Ar template
|
2001-10-10 17:05:55 -07:00
|
|
|
(if specified) should be a directory component (as opposed to a full path)
|
|
|
|
and thus should not contain any forward slashes.
|
1996-11-20 23:59:31 -08:00
|
|
|
.It Fl u
|
|
|
|
Operate in
|
|
|
|
.Dq unsafe
|
2000-03-10 11:07:20 -08:00
|
|
|
mode.
|
|
|
|
The temp file will be unlinked before
|
1996-11-20 23:59:31 -08:00
|
|
|
.Nm
|
2000-03-10 11:07:20 -08:00
|
|
|
exits.
|
|
|
|
This is slightly better than
|
2010-12-27 13:18:44 -08:00
|
|
|
.Xr mktemp 3
|
2000-03-10 11:07:20 -08:00
|
|
|
but still introduces a race condition.
|
|
|
|
Use of this option is not encouraged.
|
1997-10-27 20:23:05 -08:00
|
|
|
.El
|
2003-06-10 02:12:09 -07:00
|
|
|
.Sh ENVIRONMENT
|
|
|
|
.Bl -tag -width TMPDIR
|
|
|
|
.It Ev TMPDIR
|
|
|
|
directory in which to place the temporary file when in
|
|
|
|
.Fl t
|
|
|
|
mode
|
|
|
|
.El
|
2018-06-07 04:37:21 -07:00
|
|
|
.Sh EXIT STATUS
|
|
|
|
.Ex -std mktemp
|
1996-11-20 23:59:31 -08:00
|
|
|
.Sh EXAMPLES
|
|
|
|
The following
|
|
|
|
.Xr sh 1
|
|
|
|
fragment illustrates a simple use of
|
|
|
|
.Nm
|
|
|
|
where the script should quit if it cannot get a safe
|
2024-03-03 07:24:45 -08:00
|
|
|
temporary file:
|
1996-11-20 23:59:31 -08:00
|
|
|
.Bd -literal -offset indent
|
2001-10-10 17:05:55 -07:00
|
|
|
TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1
|
1996-11-20 23:59:31 -08:00
|
|
|
echo "program output" >> $TMPFILE
|
|
|
|
.Ed
|
|
|
|
.Pp
|
2001-10-01 10:08:30 -07:00
|
|
|
The same fragment with support for a user's
|
|
|
|
.Ev TMPDIR
|
2024-03-03 07:24:45 -08:00
|
|
|
environment variable can be written as follows:
|
1996-11-20 23:59:31 -08:00
|
|
|
.Bd -literal -offset indent
|
2001-10-10 17:05:55 -07:00
|
|
|
TMPFILE=`mktemp -t example.XXXXXXXXXX` || exit 1
|
|
|
|
echo "program output" >> $TMPFILE
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
This can be further simplified if we don't care about the actual name of
|
2003-06-10 02:12:09 -07:00
|
|
|
the temporary file.
|
|
|
|
In this case the
|
2001-10-10 17:05:55 -07:00
|
|
|
.Fl t
|
2024-03-03 07:24:45 -08:00
|
|
|
flag is implied:
|
2001-10-10 17:05:55 -07:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
TMPFILE=`mktemp` || exit 1
|
2001-10-01 10:08:30 -07:00
|
|
|
echo "program output" >> $TMPFILE
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
In some cases, it may be desirable to use a default temporary directory
|
|
|
|
other than
|
|
|
|
.Pa /tmp .
|
|
|
|
In this example the temporary file will be created in
|
|
|
|
.Pa /extra/tmp
|
|
|
|
unless the user's
|
|
|
|
.Ev TMPDIR
|
2024-03-03 07:24:45 -08:00
|
|
|
environment variable specifies otherwise:
|
2001-10-01 10:08:30 -07:00
|
|
|
.Bd -literal -offset indent
|
2001-10-10 17:05:55 -07:00
|
|
|
TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1
|
2001-10-01 10:08:30 -07:00
|
|
|
echo "program output" >> $TMPFILE
|
|
|
|
.Ed
|
|
|
|
.Pp
|
2013-03-12 08:07:12 -07:00
|
|
|
In other cases, we want the script to catch the error.
|
2001-10-01 10:08:30 -07:00
|
|
|
For instance, if we attempt to create two temporary files and
|
2024-03-03 07:24:45 -08:00
|
|
|
the second one fails we need to remove the first before exiting:
|
2001-10-01 10:08:30 -07:00
|
|
|
.Bd -literal -offset indent
|
2024-03-03 07:24:45 -08:00
|
|
|
TMP1=`mktemp -t example.XXXXXXXXXX.1` || exit 1
|
|
|
|
TMP2=`mktemp -t example.XXXXXXXXXX.2`
|
1997-01-03 17:08:04 -08:00
|
|
|
if [ $? -ne 0 ]; then
|
2001-10-01 10:08:30 -07:00
|
|
|
rm -f $TMP1
|
1996-11-20 23:59:31 -08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
.Ed
|
1999-08-05 02:19:35 -07:00
|
|
|
.Pp
|
|
|
|
Or perhaps you don't want to exit if
|
|
|
|
.Nm
|
2000-03-10 11:07:20 -08:00
|
|
|
is unable to create the file.
|
2024-03-03 07:24:45 -08:00
|
|
|
In this case you can protect that part of the script thusly:
|
1999-08-05 02:19:35 -07:00
|
|
|
.Bd -literal -offset indent
|
2001-10-10 17:05:55 -07:00
|
|
|
TMPFILE=`mktemp -q -t example.XXXXXXXXXX` && {
|
1999-08-05 02:19:35 -07:00
|
|
|
# Safe to use $TMPFILE in this block
|
|
|
|
echo data > $TMPFILE
|
|
|
|
...
|
|
|
|
rm -f $TMPFILE
|
|
|
|
}
|
|
|
|
.Ed
|
2013-03-12 08:07:12 -07:00
|
|
|
.Sh DIAGNOSTICS
|
|
|
|
One of the following error messages may be displayed if
|
|
|
|
.Nm
|
|
|
|
does not succeed and the
|
|
|
|
.Fl q
|
|
|
|
option was not specified:
|
|
|
|
.Bl -tag -width indent
|
|
|
|
.It Li "insufficient number of Xs in template"
|
|
|
|
The specified
|
|
|
|
.Ar template
|
|
|
|
contained fewer than six
|
|
|
|
.Ql X Ns s
|
|
|
|
at the end.
|
|
|
|
.It Li "template must not contain directory separators in -t mode"
|
|
|
|
The
|
|
|
|
.Ar template
|
|
|
|
contained one or more directory components and the
|
|
|
|
.Fl t
|
|
|
|
option was specified.
|
|
|
|
.It Li "cannot make temp dir"
|
|
|
|
.Nm
|
|
|
|
was unable to create the temporary directory for any of the reasons
|
|
|
|
specified by
|
2013-08-06 23:19:36 -07:00
|
|
|
.Xr mkdtemp 3 .
|
2013-03-12 08:07:12 -07:00
|
|
|
.It Li "cannot make temp file"
|
|
|
|
.Nm
|
|
|
|
was unable to create the temporary file for any of the reasons
|
|
|
|
specified by
|
2013-08-06 23:19:36 -07:00
|
|
|
.Xr mkstemp 3 .
|
2013-03-12 08:07:12 -07:00
|
|
|
.It Li "cannot allocate memory"
|
|
|
|
.Nm
|
|
|
|
was unable to allocate memory for any of the reasons specified by
|
|
|
|
.Xr malloc 3 .
|
|
|
|
.El
|
1996-11-20 23:59:31 -08:00
|
|
|
.Sh SEE ALSO
|
1998-09-26 12:54:41 -07:00
|
|
|
.Xr mktemp 3
|
1996-11-20 23:59:31 -08:00
|
|
|
.Sh HISTORY
|
|
|
|
The
|
|
|
|
.Nm
|
2005-12-31 08:52:28 -08:00
|
|
|
utility first appeared in
|
1998-03-07 14:18:05 -08:00
|
|
|
.Ox 2.1 .
|