Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Linux mktemp command

Reference for the Linux mktemp command: create unique temporary files and directories safely, use templates, suffixes and TMPDIR, and clean up with trap.

mktemp creates a temporary file or directory with a unique, hard-to-guess name and prints that name. The file is created atomically with mode 0600 (directories 0700), so no other user can read it and no two scripts can pick the same name by accident.

This is the correct replacement for the common but unsafe habit of writing to /tmp/myscript.$$ or a fixed name such as /tmp/out.txt. Predictable names allow collisions and, on shared machines, symlink attacks.

Terminal
tmp=$(mktemp)
dir=$(mktemp -d)

Try it

Bash Files, directories and templates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Cleanup with a trap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
mktemp [-d] [-u] [-q] [--suffix=SUFFIX] [-p DIR | --tmpdir[=DIR]] [TEMPLATE]
Option Description
-d, --directory Create a directory instead of a file.
TEMPLATE A name ending in at least three X characters (six or more is better) that are replaced by random characters. Default: tmp.XXXXXXXXXX.
--suffix=SUFFIX Append SUFFIX after the random part (mktemp --suffix=.log).
-p DIR, --tmpdir[=DIR] Put the item inside DIR; without an argument, use $TMPDIR or /tmp. A template given with -p must not contain a slash.
-t Old option: interpret the template relative to the temporary directory. Prefer --tmpdir.
-u, --dry-run Print a name without creating anything. Unsafe, since the name may be taken before you use it.
-q, --quiet Suppress error messages.

Returns: the created path on standard output and status 0. On failure (no write permission, template without X), it prints an error and returns a non-zero status.

Usage notes

  • Without a template, mktemp puts the file in $TMPDIR (or /tmp). With a template that contains a slash, the file goes exactly where the template says; a relative template without a slash is created in the current directory.
  • Always quote the result: "$tmp". Temporary paths are usually safe, but TMPDIR can contain spaces.
  • Clean up yourself. mktemp never deletes anything. Register a handler right after creation: trap 'rm -rf "$work"' EXIT. See Traps and signals.
  • Check for failure: tmp=$(mktemp) || exit 1, or use set -e. A failed command substitution leaves tmp empty, and rm -rf "$tmp"/* on an empty variable is a classic disaster.
  • -u is a race condition by design; use it only to print an example name.
  • Suffix and extensions: mktemp --suffix=.json is handy for tools that look at the extension. Not every old mktemp (BSD, macOS) supports --suffix; there, put the extension inside the template only if the platform allows it.
  • RAM disks: on many systems /tmp or /dev/shm is memory-backed, so very large temporary files can use RAM. Set TMPDIR to a disk location for big jobs.
  • Compare with $$: a PID is guessable and gets reused; mktemp is neither.
  • Related files: install creates files with a given mode; mktemp is for scratch space you throw away.