coreutils: mktemp invocation

1 
1 18.4 ‘mktemp’: Create temporary file or directory
1 =================================================
1 
1 ‘mktemp’ manages the creation of temporary files and directories.
1 Synopsis:
1 
1      mktemp [OPTION]... [TEMPLATE]
1 
1    Safely create a temporary file or directory based on TEMPLATE, and
1 print its name.  If given, TEMPLATE must include at least three
1 consecutive ‘X’s in the last component.  If omitted, the template
1 ‘tmp.XXXXXXXXXX’ is used, and option ‘--tmpdir’ is implied.  The final
1 run of ‘X’s in the TEMPLATE will be replaced by alpha-numeric
1 characters; thus, on a case-sensitive file system, and with a TEMPLATE
1 including a run of N instances of ‘X’, there are ‘62**N’ potential file
1 names.
1 
1    Older scripts used to create temporary files by simply joining the
1 name of the program with the process id (‘$$’) as a suffix.  However,
1 that naming scheme is easily predictable, and suffers from a race
1 condition where the attacker can create an appropriately named symbolic
1 link, such that when the script then opens a handle to what it thought
1 was an unused file, it is instead modifying an existing file.  Using the
1 same scheme to create a directory is slightly safer, since the ‘mkdir’
1 will fail if the target already exists, but it is still inferior because
1 it allows for denial of service attacks.  Therefore, modern scripts
1 should use the ‘mktemp’ command to guarantee that the generated name
1 will be unpredictable, and that knowledge of the temporary file name
1 implies that the file was created by the current script and cannot be
1 modified by other users.
1 
1    When creating a file, the resulting file has read and write
1 permissions for the current user, but no permissions for the group or
1 others; these permissions are reduced if the current umask is more
1 restrictive.
1 
1    Here are some examples (although note that if you repeat them, you
1 will most likely get different file names):
1 
1    • Create a temporary file in the current directory.
1           $ mktemp file.XXXX
1           file.H47c
1 
1    • Create a temporary file with a known suffix.
1           $ mktemp --suffix=.txt file-XXXX
1           file-H08W.txt
1           $ mktemp file-XXXX-XXXX.txt
1           file-XXXX-eI9L.txt
1 
1    • Create a secure fifo relative to the user’s choice of ‘TMPDIR’, but
1      falling back to the current directory rather than ‘/tmp’.  Note
1      that ‘mktemp’ does not create fifos, but can create a secure
1      directory in which the fifo can live.  Exit the shell if the
1      directory or fifo could not be created.
1           $ dir=$(mktemp -p "${TMPDIR:-.}" -d dir-XXXX) || exit 1
1           $ fifo=$dir/fifo
1           $ mkfifo "$fifo" || { rmdir "$dir"; exit 1; }
1 
1    • Create and use a temporary file if possible, but ignore failure.
1      The file will reside in the directory named by ‘TMPDIR’, if
1      specified, or else in ‘/tmp’.
1           $ file=$(mktemp -q) && {
1           >   # Safe to use $file only within this block.  Use quotes,
1           >   # since $TMPDIR, and thus $file, may contain whitespace.
1           >   echo ... > "$file"
1           >   rm "$file"
1           > }
1 
1    • Act as a semi-random character generator (it is not fully random,
1      since it is impacted by the contents of the current directory).  To
1      avoid security holes, do not use the resulting names to create a
1      file.
1           $ mktemp -u XXX
1           Gb9
1           $ mktemp -u XXX
1           nzC
1 
11    The program accepts the following options.  Also see ⇒Common
 options.
1 
1 ‘-d’
1 ‘--directory’
1      Create a directory rather than a file.  The directory will have
1      read, write, and search permissions for the current user, but no
1      permissions for the group or others; these permissions are reduced
1      if the current umask is more restrictive.
1 
1 ‘-q’
1 ‘--quiet’
1      Suppress diagnostics about failure to create a file or directory.
1      The exit status will still reflect whether a file was created.
1 
1 ‘-u’
1 ‘--dry-run’
1      Generate a temporary name that does not name an existing file,
1      without changing the file system contents.  Using the output of
1      this command to create a new file is inherently unsafe, as there is
1      a window of time between generating the name and using it where
1      another process can create an object by the same name.
1 
1 ‘-p DIR’
1 ‘--tmpdir[=DIR]’
1      Treat TEMPLATE relative to the directory DIR.  If DIR is not
1      specified (only possible with the long option ‘--tmpdir’) or is the
1      empty string, use the value of ‘TMPDIR’ if available, otherwise use
1      ‘/tmp’.  If this is specified, TEMPLATE must not be absolute.
1      However, TEMPLATE can still contain slashes, although intermediate
1      directories must already exist.
1 
1 ‘--suffix=SUFFIX’
1      Append SUFFIX to the TEMPLATE.  SUFFIX must not contain slash.  If
1      ‘--suffix’ is specified, TEMPLATE must end in ‘X’; if it is not
1      specified, then an appropriate ‘--suffix’ is inferred by finding
1      the last ‘X’ in TEMPLATE.  This option exists for use with the
1      default TEMPLATE and for the creation of a SUFFIX that starts with
1      ‘X’.
1 
1 ‘-t’
1      Treat TEMPLATE as a single file relative to the value of ‘TMPDIR’
1      if available, or to the directory specified by ‘-p’, otherwise to
1      ‘/tmp’.  TEMPLATE must not contain slashes.  This option is
1      deprecated; the use of ‘-p’ without ‘-t’ offers better defaults (by
1      favoring the command line over ‘TMPDIR’) and more flexibility (by
1      allowing intermediate directories).
1 
1    Exit status:
1 
1      0 if the file was created,
1      1 otherwise.
1