Linux Commands Examples

A great documentation place for Linux commands


create a temporary file or directory


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!


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

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.


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.
, --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


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


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]


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


display this help and exit


output version information and exit


Copyright © 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <>.
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: <>
General help using GNU software: <>
Report mktemp translation bugs to <>

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.


Written by Jim Meyering and Eric Blake.

How can this site be more helpful to YOU ?

give  feedback