1MKTEMP(1) General Commands Manual MKTEMP(1)
2
3
4
6 mktemp - make temporary filename (unique)
7
9 mktemp [-V] | [-dqtu] [-p directory] [template]
10
12 The mktemp utility takes the given filename template and overwrites a
13 portion of it to create a unique filename. The template may be any
14 filename with some number of `Xs' appended to it, for example
15 /tmp/tfile.XXXXXXXXXX. If no template is specified a default of
16 tmp.XXXXXXXXXX is used and the -t flag is implied (see below).
17
18 The trailing `Xs' are replaced with a combination of the current
19 process number and random letters. The name chosen depends both on the
20 number of `Xs' in the template and the number of collisions with
21 pre-existing files. The number of unique filenames mktemp can return
22 depends on the number of `Xs' provided; ten `Xs' will result in mktemp
23 testing roughly 26 ** 10 combinations.
24
25 If mktemp can successfully generate a unique filename, the file (or
26 directory) is created with file permissions such that it is only read‐
27 able and writable by its owner (unless the -u flag is given) and the
28 filename is printed to standard output.
29
30 mktemp is provided to allow shell scripts to safely use temporary
31 files. Traditionally, many shell scripts take the name of the program
32 with the PID as a suffix and use that as a temporary filename. This
33 kind of naming scheme is predictable and the race condition it creates
34 is easy for an attacker to win. A safer, though still inferior
35 approach is to make a temporary directory using the same naming scheme.
36 While this does allow one to guarantee that a temporary file will not
37 be subverted, it still allows a simple denial of service attack. For
38 these reasons it is suggested that mktemp be used instead.
39
40 The options are as follows:
41
42 -V Print the version and exit.
43
44 -d Make a directory instead of a file.
45
46 -p directory
47 Use the specified directory as a prefix when generating the tem‐
48 porary filename. The directory will be overridden by the user's
49 TMPDIR environment variable if it is set. This option implies
50 the -t flag (see below).
51
52 -q Fail silently if an error occurs. This is useful if a script
53 does not want error output to go to standard error.
54
55 -t Generate a path rooted in a temporary directory. This directory
56 is chosen as follows:
57
58 · If the user's TMPDIR environment variable is set, the
59 directory contained therein is used.
60
61 · Otherwise, if the -p flag was given the specified direc‐
62 tory is used.
63
64 · If none of the above apply, /tmp is used.
65
66 In this mode, the template (if specified) should be a directory compo‐
67 nent (as opposed to a full path) and thus should not contain any for‐
68 ward slashes.
69
70 -u Operate in ``unsafe'' mode. The temp file will be unlinked
71 before mktemp exits. This is slightly better than mktemp(3) but
72 still introduces a race condition. Use of this option is not
73 encouraged.
74
75 The mktemp utility exits with a value of 0 on success or 1 on failure.
76
78 The following sh(1) fragment illustrates a simple use of mktemp where
79 the script should quit if it cannot get a safe temporary file.
80
81 TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1
82 echo "program output" >> $TMPFILE
83
84 The same fragment with support for a user's TMPDIR environment variable
85 can be written as follows.
86
87 TMPFILE=`mktemp -t example.XXXXXXXXXX` || exit 1
88 echo "program output" >> $TMPFILE
89
90 This can be further simplified if we don't care about the actual name
91 of the temporary file. In this case the -t flag is implied.
92
93 TMPFILE=`mktemp` || exit 1
94 echo "program output" >> $TMPFILE
95
96 In some cases, it may be desirable to use a default temporary directory
97 other than /tmp. In this example the temporary file will be created in
98 /extra/tmp unless the user's TMPDIR environment variable specifies oth‐
99 erwise.
100
101 TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1
102 echo "program output" >> $TMPFILE
103
104 In some cases, we want the script to catch the error. For instance, if
105 we attempt to create two temporary files and the second one fails we
106 need to remove the first before exiting.
107
108 TMP1=`mktemp -t example.1.XXXXXXXXXX` || exit 1
109 TMP2=`mktemp -t example.2.XXXXXXXXXX`
110 if [ $? -ne 0 ]; then
111 rm -f $TMP1
112 exit 1
113 fi
114
115 Or perhaps you don't want to exit if mktemp is unable to create the
116 file. In this case you can protect that part of the script thusly.
117
118 TMPFILE=`mktemp -t example.XXXXXXXXXX` && {
119 # Safe to use $TMPFILE in this block
120 echo data > $TMPFILE
121 ...
122 rm -f $TMPFILE
123 }
124
125
127 TMPDIR directory in which to place the temporary file when in -t mode
128
130 mkdtemp(3), mkstemp(3), mktemp(3)
131
133 The mktemp utility appeared in OpenBSD 2.1.
134
135
136
137 30 September 2001 MKTEMP(1)