Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
International Rescue Committe

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents

· Introduction to Operating Systems
· Linux Basics
· Working with the System
· Shells and Utilities
· Editing Files
· Basic Administration
· The Operating System
· The X Windowing System
· The Computer Itself
· Networking
· System Monitoring
· Solving Problems
· Security
· Installing and Upgrading
· Linux and Windows

Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
Copyright Info
Terms of Use
Privacy Info
Masthead / Impressum
Your Account

Private Messages

News Archive
Submit News
User Articles
Web Links


The Web

Who's Online
There are currently, 83 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here




       mktemp [-V] | [-dqtu] [-p directory] [template]


       The  mktemp  utility takes the given filename template and
       overwrites a portion of it to create  a  unique  filename.
       The  template may be any filename with some number of `Xs'
       appended to it, for example /tmp/tfile.XXXXXXXXXX.  If  no
       template  is specified a default of tmp.XXXXXXXXXX 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 letters.  The name cho­
       sen 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.

       If mktemp can successfully generate a unique filename, the
       file  (or directory) is created with file permissions such
       that it is only readable and writable by its owner (unless
       the -u flag is given) and the filename is printed to stan­
       dard output.

       mktemp 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 will  not  be  subverted,  it  still
       allows  a simple denial of service attack.  For these rea­
       sons it is suggested that mktemp be used instead.

       The options are as follows:

       -V     Print the version and exit.

       -d     Make a directory instead of a file.

       -p directory
              Use the specified directory as a prefix when gener­
              ating  the  temporary filename.  The directory will
              be overridden  by  the  user's  TMPDIR  environment
              variable  if it is set.  This option implies the -t
              flag (see below).

       -q     Fail silently if an error occurs.  This  is  useful
              if  a  script  does  not want error output to go to

       In this mode, the template  (if  specified)  should  be  a
       directory  component  (as opposed to a full path) and thus
       should not contain any forward slashes.

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

       The mktemp utility exits with a value of 0 on success or 1
       on failure.


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

              TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1
              echo "program output" >> $TMPFILE

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

              TMPFILE=`mktemp -t example.XXXXXXXXXX` || exit 1
              echo "program output" >> $TMPFILE

       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.

              TMPFILE=`mktemp` || exit 1
              echo "program output" >> $TMPFILE

       In some cases, it may be desirable to use a default tempo­
       rary directory other than /tmp.  In this example the  tem­
       porary  file  will  be  created  in  /extra/tmp unless the
       user's TMPDIR environment variable specifies otherwise.

              TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1
              echo "program output" >> $TMPFILE

       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

              TMP1=`mktemp -t example.1.XXXXXXXXXX` || exit 1
              TMP2=`mktemp -t example.2.XXXXXXXXXX`
              if [ $? -ne 0 ]; then
                   rm -f $TMP1
                   exit 1

       TMPDIR  directory  in  which  to  place the temporary file
               when in -t mode


       mkdtemp(3), mkstemp(3), mktemp(3)


       The mktemp utility appeared in OpenBSD 2.1.

                        30 September 2001               MKTEMP(1)
Show your Support for the Linux Tutorial

Purchase one of the products from our new online shop. For each product you purchase, the Linux Tutorial gets a portion of the proceeds to help keep us going.



Security Code
Security Code
Type Security Code

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!

Amazon Wish List

Did You Know?
The Linux Tutorial welcomes your suggestions and ideas.


Tell a Friend About Us

Bookmark and Share

Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.09 Seconds