Penguin
Annotated edit history of mktemp(1) version 1, including all changes. View license author blame.
Rev Author # Line
1 perry 1 ----
2 __NAME__
3
4
5 mktemp - make temporary filename (unique)
6 __SYNOPSIS__
7
8
9 mktemp [[-dqtu] [[-p directory] [[template]
10 __DESCRIPTION__
11
12
13 The mktemp utility takes the given filename template and
14 overwrites a portion of it to create a unique filename. The
15 template may be any filename with some number of Xs
16 appended to it, for example /tmp/tfile.XXXXXXXXXX.
17 If no template is specified a default of
18 tmp.XXXXXXXXXX is used and the -t flag is implied
19 (see below).
20
21
22 The trailing Xs are replaced with a combination of the cur- rent process number and random letters. The name chosen depends both on the number of Xs in the template and the number of collisions with pre-existing files. The number of unique filenames mktemp can return depends on the number of Xs provided; ten Xs will result in mktemp testing roughly 26 10 combinations.
23
24
25 If mktemp can successfully generate a unique filename, the
26 file (or directory) is created with file permissions such
27 that it is only readable and writable by its owner (unless
28 the -u flag is given) and the filename is printed to stan-
29 dard output.
30
31
32 mktemp is provided to allow shell scripts to safely use tem-
33 porary files. Traditionally, many shell scripts take the
34 name of the program with the PID as a suffix and use that as
35 a temporary filename. This kind of naming scheme is pre-
36 dictable and the race condition it creates is easy for an
37 attacker to win. A safer, though still inferior approach is
38 to make a temporary directory using the same naming scheme.
39 While this does allow one to guarantee that a temporary file
40 will not be subverted, it still allows a simple denial of
41 service attack. For these reasons it is suggested that
42 mktemp be used instead.
43
44
45 The options are as follows:
46 -d Make a directory instead of a file.
47
48
49 -p directoryUse the specified directory as a prefix when generat-ing the temporary filename. The directory will beoverridden by the user's TMPDIR environment variable if it is set. This option implies the -t flag (see below).
50
51
52 -q
53 Fail silently if an error occurs. This is useful if a script
54 does not want error output to go to standard
55 error.
56
57
58 -t
59 Generate a path rooted in a temporary directory. This
60 directory is chosen as follows:
61
62
63 If the user's TMPDIR environment variable is set,
64 the directory contained therein is used.
65
66
67 Otherwise, if the -p flag was given the specified directory
68 is used.
69
70
71 If none of the above apply, /tmp is
72 used.
73
74
75 In this mode, the template (if specified) should be a
76 directory component (as opposed to a full path) and thus
77 should not contain any forward slashes.
78
79
80 -u
81 Operate in ``unsafe'' mode. The temp file will be unlinked
82 before mktemp exits. This is slightly bet- ter than
83 mktemp(3) but still introduces a race condi- tion. Use of
84 this option is not encouraged.
85
86
87 The mktemp utility exits with a value of 0 on success or 1
88 on failure.
89
90
91 Debian packages using mktemp in maintainer scripts must
92 depend on debianutils
93
94
95 __EXAMPLES__
96
97
98 The following sh(1) fragment illustrates a simple
99 use of mktemp where the script should quit if it cannot get
100 a safe temporary file.
101
102
103 TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1
104 echo
105 The same fragment with support for a user's TMPDIR environ- ment variable can be written as follows.
106
107
108 TMPFILE=`mktemp -t example.XXXXXXXXXX` || exit 1
109 echo
110 This can be further simplified if we don't care about the actual name of the temporary file. In this case the -t flag is implied.
111
112
113 TMPFILE=`mktemp` || exit 1
114 echo
115 In some cases, it may be desirable to use a default tempo- rary directory other than /tmp. In this example the tempo- rary file will be created in /extra/tmp unless the user's TMPDIR environment variable specifies otherwise.
116
117
118 TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1
119 echo
120 In some cases, we want the script to catch the error. For instance, if we attempt to create two temporary files and the second one fails we need to remove the first before exiting.
121
122
123 TMP1=`mktemp -t example.1.XXXXXXXXXX` || exit 1
124 TMP2=`mktemp -t example.2.XXXXXXXXXX`
125 if [[ $? -ne 0 ]; then
126 rm -f $TMP1
127 exit 1
128 fi
129 Or perhaps you don't want to exit if mktemp is unable to create the file. In this case you can protect that part of the script thusly.
130
131
132 TMPFILE=`mktemp -q -t example.XXXXXXXXXX`
133 __ENVIRONMENT__
134 TMPDIR
135
136
137 directory in which to place the temporary file when in -t
138 mode
139
140
141 __SEE ALSO__
142
143
144 mkdtemp(3), mkstemp(3),
145 mktemp(3)
146 __HISTORY__
147
148
149 The mktemp utility appeared in OpenBSD
150 2.1.
151
152
153 BSD September 30, 2001 1
154 ----
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.