Back to the command reference

mktemp

FilesAdvanced In simulator In real Linux

Create a temporary file or directory safely

Name: mktemp = make temporary

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

mktemp safely creates a temporary file (or with -d a directory) with a random, unique name and prints its path. Scripts should use it instead of fixed names like /tmp/xxx to avoid clashes and security issues.

Syntax

mktemp [OPTION]... [TEMPLATE]

Common options

-d / --directoryCreate a directory
-p / --tmpdir DIRCreate it in DIR ($TMPDIR or /tmp by default)
--suffix SUFFIXAdd SUFFIX to the name, e.g. --suffix=.txt
-u / --dry-runPrint a name without creating it (unsafe: someone may take it first)
XXXXXXX characters in the template become random

Examples

  • $ tmp=$(mktemp)

    Create a temp file in a script and keep its path

    Explain
  • $ mktemp -d /tmp/build.XXXXXX

    Create a temporary directory

    Explain

Related