Session and utilities

The mktemp command

Creates a temporary file or folder with a unique, safe name that cannot clash.

Syntax

mktemp [opciones] [plantilla]Try it in the terminal →

Most useful options

-d, --directoryCreates a folder instead of a file
-u, --dry-runOnly prints a name without creating anything. It is unsafe: another process can create that name first
-q, --quietDoes not show a message when it cannot create the file; it only exits with status 1
--suffix=SUFFAppends SUFF to the end of the name; it must not contain a slash. If the template does not end in X, whatever follows the Xs already counts as the suffix
-p, --tmpdir <carpeta>Creates the name inside the given folder. With --tmpdir and no value, it uses $TMPDIR or /tmp
-tTreats the template as a single name inside $TMPDIR, the -p folder or /tmp. It is deprecated

Copy-ready examples

$ mktemptemporary file with a unique name, in /tmp or in $TMPDIR if it is set.

Example output

/tmp/tmp.aB3xK9pQ2r
$ mktemp -dtemporary folder instead of a file.

Example output

/tmp/tmp.Qw8zLx0mHd
$ mktemp --suffix=.txttemporary file with an extension, for programs that require one.

Example output

/tmp/tmp.4fT1pVz9Ke.txt
$ mktemp -p "$HOME" miapp.XXXXXXname from your own template in your home folder. Each X is replaced by a random character.

Example output

/home/alumno/miapp.k2Q7xR

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
mktemp: too few X's in template 'miapp'The template does not end in at least three consecutive Xs in its last part.End the name with XXX or more, for example miapp.XXXXXX.
mktemp: failed to create file via template '/home/alumno/nueva/tmp.XXXXXXXXXX': No such file or directoryThe folder given with -p does not exist, so the file cannot be created inside it.Create the folder first with mkdir -p, or choose one that already exists.

Typical mistakes

  • The file does not delete itself: when your script ends, you have to remove it, for example with trap 'rm -f "$f"' EXIT.
  • With -u nothing is created, and another process can create that name before you do. In a script, always create the file, without -u.
  • /tmp is shared by all the users of the system: do not keep sensitive data there without protecting it.

Differences from similar commands

  • touch: creates a file with the name you choose and, if it exists, only updates its date. mktemp picks a new, unique name.
  • mkdir: creates a folder with the name you pass. mktemp -d picks the name for you so it does not clash with another folder.

Security

mktemp -u creates nothing: between printing the name and using it, another process can take it. In scripts, create the file with mktemp without -u.

Related commands

FAQ

Why is a fixed name in /tmp not enough?

Two programs or two users can pick the same name and overwrite each other. A predictable name can also be guessed to attack your script. mktemp guarantees a unique name.

How do I store the path in a variable?

With f=$(mktemp). The shell captures the path mktemp prints and stores it in f. For a folder, use f=$(mktemp -d).

What does the X in the template mean?

Each X is replaced by a random character. You need at least three at the end of the name, and the more you use, the harder it is to guess.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course