Linux Commands Examples

A great documentation place for Linux commands

mktemp

create a temporary file or directory

Synopsis

mktemp [OPTION]... [TEMPLATE]


add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!

examples

0
source
            
mktemp_out_=
mktemp_in_=
mktemp_() {
mktemp_in_=$1; shift
if mktemp_out_=`mktemp $@ -t "$mktemp_in_" 2>/dev/null`; then
echo "$mktemp_out_"
return 0
fi
mktemp $@ $2 /tmp/"$mktemp_in_"
}
mktemp_dir_() {
mktemp_ "$1" -d
0
source
            
TEMPFILE=$(mktemp /tmp/File-XXXXXX)
echo $TEMPFILE
0
source
            
td=$(mktemp -d)
cp $1 $td
cd $td
}
0
source
            
FILE=`mktemp --tmpdir=.`
FILE2=`mktemp --tmpdir=.`
exec touch -r "${FILE}" "${FILE2}"
0
source

How long do temporary files made with mktemp last?

Temp files do not go away on their own. They are called temp files simply because in your script or session or wherever you are making them, you are expected to delete them when you are done. Or leave them laying around if that's your thing. mktemp exists solely to allow you to come up with a file that is named something unique so that it doesn't overwrite some existing file.

description

Create a temporary file or directory, safely, and print its name. TEMPLATE must contain at least 3 consecutive ’X’s in last component. If TEMPLATE is not specified, use tmp.XXXXXXXXXX, and --tmpdir is implied. Files are created u+rw, and directories u+rwx, minus umask restrictions.
-d
, --directory

create a directory, not a file

-u, --dry-run

do not create anything; merely print a name (unsafe)

-q, --quiet

suppress diagnostics about file/dir-creation failure

--suffix=SUFF

append SUFF to TEMPLATE. SUFF must not contain slash. This option is implied if TEMPLATE does not end in X.

--tmpdir[=DIR]

interpret TEMPLATE relative to DIR. If DIR is not specified, use $TMPDIR if set, else /tmp. With this option, TEMPLATE must not be an absolute name. Unlike with -t, TEMPLATE may contain slashes, but mktemp creates only the final component

-p DIR

use DIR as a prefix; implies -t [deprecated]

-t

interpret TEMPLATE as a single file name component, relative to a directory: $TMPDIR, if set; else the directory specified via -p; else /tmp [deprecated]

--help

display this help and exit

--version

output version information and exit

copyright

Copyright © 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

reporting bugs

Report mktemp bugs to bug-coreutils[:at:]gnu[:dot:]org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report mktemp translation bugs to <http://translationproject.org/team/>


see also

mkstemp, mkdtemp, mktemp

The full documentation for mktemp is maintained as a Texinfo manual. If the info and mktemp programs are properly installed at your site, the command

info coreutils 'mktemp invocation'

should give you access to the complete manual.


author

Written by Jim Meyering and Eric Blake.

How can this site be more helpful to YOU ?


give  feedback