mktemp(1) 맨 페이지 - 윈디하나의 솔라나라

개요

섹션
맨 페이지 이름
검색(S)

mktemp(1)

mktemp(1)                        User Commands                       mktemp(1)

NAME
       mktemp - make temporary filename

SYNOPSIS
       mktemp [-dtqu] [--directory] [--quiet] [--dry-run] [-p directory]
            [--suffix=suff] [--tmpdir[=dir]] [template]

DESCRIPTION
       The mktemp utility makes a temporary filename. To do this, mktemp takes
       the  specified filename template and overwrites a portion of it to cre‐
       ate a unique filename. See OPERANDS.


       The template  is  converted  to  a  path  name  using  the  mktemp(3C),
       mkdtemp(3C), mkstemp(3C), and mkstemps(3C) library functions.


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


       mktemp allows shell scripts to safely use temporary  files.  Tradition‐
       ally, many shell scripts take the name of the program with the PID as a
       suffix  and  used  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 guarantees
       that a temporary file is not subverted, it still allows a simple denial
       of service attack. Use mktemp instead.

OPTIONS
       The following options are supported:

       -d, --directory

           Make a directory instead of a file.


       -p directory

           Use the specified directory as a prefix when generating the  tempo‐
           rary filename. The directory is overridden by the user's TMPDIR en‐
           vironment variable if it is set. This option implies the -t flag.


       -q, --quiet

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


       --suffix=suff

           Append suff to the template. suff must not  contain  a  slash  (/).
           This  option  is implied when the template ends in characters other
           than replaceable X characters, and does not remove the  requirement
           to include such X characters.


       --tmpdir[=dir]

           ´template'  is relative to dir. If dir is not specified, the user's
           environment variable TMPDIR is used if set, else /tmp is used. This
           option does not support a template with an absolute name and unlike
           with the use of -t, a template may contain slashes.


       -t

           Generate a path rooted in a temporary directory. This directory  is
           chosen  as  follows:  If  the user's TMPDIR environment variable is
           set, the directory contained therein is used. Otherwise, if the  -p
           flag  was  given  the  specified  directory is used. If none of the
           above apply, /tmp is used. In this mode, the  template  (if  speci‐
           fied)  should  be a directory component (as opposed to a full path)
           and thus should not contain any forward slashes.


       -u, --dry-run

           Operate in unsafe mode. The temp file is unlinked before mktemp ex‐
           its. This is slightly better than mktemp(3C), but still  introduces
           a race condition. Use of this option is discouraged.


OPERANDS
       The following operands are supported:

       template    template  can be any filename with three to six Xs included
                   in it, for example /tmp/tfile.XXXXXX.

                   If template is not specified, a default  of  tmp.XXXXXX  is
                   used and the -t flag is implied.

                   If  template has multiple series of Xs, the final series is
                   used for the replacement text,  unless  --suffix  specifies
                   otherwise.

                   If  template has characters after the final set of Xs, then
                   the --suffix option is implied to be all  such  characters,
                   unless explicitly specified.


EXAMPLES
       Example 1 Using mktemp



       The  following  example  illustrates  a simple use of mktemp in a sh(1)
       script. In this example, the script quits if it cannot get a safe  tem‐
       porary file.


         TMPFILE=`mktemp /tmp/example.XXXXXX`
         if [ -z "$TMPFILE" ]; then exit 1; fi
         echo "program output" >> $TMPFILE


       Example 2 Using mktemp to Support TMPDIR



       The  following example uses mktemp to support for a user's TMPDIR envi‐
       ronment variable:


         TMPFILE=`mktemp -t example.XXXXXX`
         if [ -z "$TMPFILE" ]; then exit 1; fi
         echo "program output" >> $TMPFILE


       Example 3 Using mktemp Without Specifying the  Name  of  the  Temporary
       File



       The  following  example  uses mktemp without specifying the name of the
       temporary file. In this case the -t flag is implied.


         TMPFILE=`mktemp`
         if [ -z "$TMPFILE" ]; then exit 1; fi
         echo "program output" >> $TMPFILE


       Example 4 Using mktemp with a Default Temporary  Directory  Other  than
       /tmp



       The  following  example creates the temporary file in /extra/tmp unless
       the user's TMPDIR environment variable specifies otherwise:


         TMPFILE=`mktemp -p /extra/tmp example.XXXXX`
         if [ -z "$TMPFILE" ]; then exit 1; fi
         echo "program output" >> $TMPFILE


       Example 5 Using mktemp to Remove a File



       The following example attempts to create two temporary files.  If  cre‐
       ation of the second temporary file fails, mktemp removes the first file
       before exiting:


         TMP1=`mktemp -t example.1.XXXXXX`
         if [ -z "$TMP1" ]; then exit 1; fi
         TMP2=`mktemp -t example.2.XXXXXX`
         if [ -z "$TMP2" ]; then
                 rm -f $TMP1
                 exit 1
         fi


       Example 6 Using mktemp



       The  following  example does not exit if mktemp is unable to create the
       file. That part of the script has been protected.


         TMPFILE=`mktemp -q -t example.XXXXXX`
         if [ ! -z "$TMPFILE" ]
         then
                 # Safe to use $TMPFILE in this block
                 echo data > $TMPFILE
                 ...
                 rm -f $TMPFILE
         fi


       Example 7 Using mktemp with Suffix Option



       The following command illustrates the use of the suffix option. The ef‐
       fect of this command is to create the temporary file ex.q5N.SUFF.


         # mktemp --suffix=.SUFF ex.XXXXXX
         ex.q5Ngid.SUFF


       Example 8 Using Suffix and Tmpdir Options



       The following command illustrates the use of the suffix and tmpdir  op‐
       tions.


         # mktemp --tmpdir=$HOME --suffix=.bar foo.XXXXXX
         /root/foo.7ZaO_N.bar


       Example 9 Using Directory and Suffix Options



       The following command uses both the directory and suffix options.


         # mktemp --directory --suffix=.bar foo.XXXXXX
         foo.GSaO3d.bar
         # ls -l
         drwx------   2 root     staff        512 Mar 19  2012 foo.GSaO.bar


       Example 10 Supporting a Template with Non-Trailing Xs



       The  following  command shows the use of the directory option with non-
       trailing X characters. In this command, the --suffix=suff option is im‐
       plied, where bar is used as the suffix.


         # mktemp XXfooXXXXXXbar
         XXfooaFY0N6bar


       Example 11 Using the Quiet and Tmpdir Options



       The following command illustrates the use of the quiet and  tmpdir  op‐
       tions.


         # mktemp --quiet --tmpdir=/tmp foo
         [No diagnostic message is returned]


       Example 12 Using mktemp with Multiple Options



       The following command combines the use of the dry-run, tmpdir, and suf‐
       fix options.


         # mktemp --dry-run --tmpdir=$HOME --suffix=SUFF
         /root/tmp.qdaGcOSUFF
         # ls -l /root/tmp.qdaGcOSUFF
         /root/tmp.qdaGcOSUFF: No such file or directory


ENVIRONMENT VARIABLES
       See  environ(7) for descriptions of the following environment variables
       that affect the execution of mktemp with the -t option: TMPDIR.

       TMPDIR    Name a directory used for creating temporary files  to  over‐
                 ride system default; used by mktemp.


EXIT STATUS
       The following exit values are returned:

       0    Successful completion.


       1    An error occurred.


ATTRIBUTES
       See attributes(7) for descriptions of the following attributes:

       tab()  box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) ATTRIBUTE TYPEAT‐
       TRIBUTE VALUE _ Availabilitysystem/core-os _ Interface StabilityCommit‐
       ted


SEE ALSO
       sh(1),  mkdtemp(3C),  mkstemp(3C),  mkstemps(3C),  mktemp(3C),  attrib‐
       utes(7), environ(7)

NOTES
       The  mktemp utility appeared in OpenBSD 2.1. The Solaris implementation
       uses only as many 'X's as are significant for mktemp(3C),  mkstemp(3C),
       and mkstemps(3C).

Oracle Solaris 11.4               23 Jul 2012                        mktemp(1)
맨 페이지 내용의 저작권은 맨 페이지 작성자에게 있습니다.
RSS ATOM XHTML 5 CSS3