Seth Woolley's Man Viewer

Manual for mktemp - man 1 mktemp

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

MKTEMP(1)                                                            MKTEMP(1)

       mktemp(1,3) - make temporary filename (unique)

       mktemp(1,3) [-V] | [-dqtu] [-p directory] [template]

       The  mktemp(1,3)  utility takes the given filename template and overwrites a
       portion of it to create a unique filename.  The  template  may  be  any
       filename   with   six   (6)   `Xs'   appended   to   it,   for  example
       /tmp/tfile.XXXXXX.  If no template is specified a default of tmp.XXXXXX
       is used and the -t flag is implied (see below).

       The  trailing  `Xs'  are  replaced  with  a  combination of the current
       process number and random(3,4,6) letters.

       If mktemp(1,3) can successfully generate a unique  filename,  the  file(1,n)  (or
       directory)  is created with file(1,n) permissions such that it is only read-
       able and writable by its owner (unless the -u flag is  given)  and  the
       filename is printed to standard output.

       mktemp(1,3)  is  provided  to  allow  shell  scripts to safely use temporary
       files.  Traditionally, many shell scripts take the name of the  program
       with  the  PID  as a suffix and use that as a temporary filename.  This
       kind of naming scheme is predictable and the race condition it  creates
       is  easy  for  an  attacker  to  win.   A  safer, though still inferior
       approach is to make a temporary directory using the same naming scheme.
       While  this  does allow one to guarantee that a temporary file(1,n) will not
       be subverted, it still allows a simple denial of service  attack.   For
       these reasons it is suggested that mktemp(1,3) be used instead.

       The options are as follows:

       -V     Print the version(1,3,5) and exit.

       -d     Make a directory instead of a file.

       -p directory
              Use the specified directory as a prefix when generating the tem-
              porary filename.  The directory will be overridden by the user's
              TMPDIR  environment  variable if(3,n) it is set.  This option implies
              the -t flag (see below).

       -q     Fail silently if(3,n) an error(8,n) occurs.  This is useful  if(3,n)  a  script
              does not want error(8,n) output to go to standard error.

       -t     Generate a path rooted in(1,8) a temporary directory.  This directory
              is chosen as follows:

                    If the user's TMPDIR environment  variable  is  set(7,n,1 builtins),  the
                     directory contained therein is used.

                    Otherwise,  if(3,n) the -p flag was given the specified direc-
                     tory is used.

                    If none of the above apply, /tmp is used.

       In this mode, the template (if(3,n) specified) should be a directory  compo-
       nent  (as  opposed to a full path) and thus should not contain any for-
       ward slashes.

       -u     Operate in(1,8) ``unsafe'' mode.  The  temp  file(1,n)  will  be  unlinked
              before mktemp(1,3) exits.  This is slightly better than mktemp(1,3)(3) but
              still introduces a race condition.  Use of this  option  is  not

       The  mktemp(1,3) utility exits with a value of 0 on success or 1 on failure.
       Debian packages using in(1,8) maintainer scripts must depend on  debianutils
       >= 1.7.

       The  following  sh(1) fragment illustrates a simple use of mktemp(1,3) where
       the script should quit if(3,n) it cannot get a safe temporary file.

              TMPFILE=`mktemp(1,3) /tmp/example.XXXXXX` || exit(3,n,1 builtins) 1
              echo(1,3x,1 builtins) "program output" >> $TMPFILE

       The same fragment with support for a user's TMPDIR environment variable
       can be written as follows.

              TMPFILE=`mktemp(1,3) -t example.XXXXXX` || exit(3,n,1 builtins) 1
              echo(1,3x,1 builtins) "program output" >> $TMPFILE

       This  can  be further simplified if(3,n) we don't care about the actual name
       of the temporary file.  In this case the -t flag is implied.

              TMPFILE=`mktemp(1,3)` || exit(3,n,1 builtins) 1
              echo(1,3x,1 builtins) "program output" >> $TMPFILE

       In some cases, it may be desirable to use a default temporary directory
       other than /tmp.  In this example the temporary file(1,n) will be created in(1,8)
       /extra/tmp unless the user's TMPDIR environment variable specifies oth-

              TMPFILE=`mktemp(1,3) -p /extra/tmp example.XXXXXX` || exit(3,n,1 builtins) 1
              echo(1,3x,1 builtins) "program output" >> $TMPFILE

       In some cases, we want the script to catch the error.  For instance, if(3,n)
       we attempt to create two temporary files and the second  one  fails  we
       need to remove the first before exiting.

              TMP1=`mktemp(1,3) -t example.1.XXXXXX` || exit(3,n,1 builtins) 1
              TMP2=`mktemp(1,3) -t example.2.XXXXXX`
              if(3,n) [ $? -ne 0 ]; then
                   rm -f $TMP1
                   exit(3,n,1 builtins) 1

       Or  perhaps  you  don't  want to exit(3,n,1 builtins) if(3,n) mktemp(1,3) is unable to create the
       file.  In this case you can protect that part of the script thusly.

              TMPFILE=`mktemp(1,3) -t example.XXXXXX` && {
                   # Safe to use $TMPFILE in(1,8) this block
                   echo(1,3x,1 builtins) data > $TMPFILE
                   rm -f $TMPFILE

       TMPDIR  directory in(1,8) which to place the temporary file(1,n) when in(1,8) -t mode

       mkdtemp(3), mkstemp(3), mktemp(1,3)(3), tempfile(1)

       The mktemp(1,3) utility appeared in(1,8) OpenBSD 2.1.

                                 22 March 2004                       MKTEMP(1)

References for this manual (incoming links)