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.
tmp=$(mktemp)
dir=$(mktemp -d)
Try it
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.
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
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,
mktempputs 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, butTMPDIRcan contain spaces. - Clean up yourself.
mktempnever 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 useset -e. A failed command substitution leavestmpempty, andrm -rf "$tmp"/*on an empty variable is a classic disaster. -uis a race condition by design; use it only to print an example name.- Suffix and extensions:
mktemp --suffix=.jsonis handy for tools that look at the extension. Not every oldmktemp(BSD, macOS) supports--suffix; there, put the extension inside the template only if the platform allows it. - RAM disks: on many systems
/tmpor/dev/shmis memory-backed, so very large temporary files can use RAM. SetTMPDIRto a disk location for big jobs. - Compare with
$$: a PID is guessable and gets reused;mktempis neither. - Related files:
installcreates files with a given mode;mktempis for scratch space you throw away.
Related
mkdir,install,rm,touch- Lessons: Traps and signals, Creating files and directories