Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Special Files and Directories

The /tmp and /var/tmp directories

Reference for /tmp and /var/tmp: how they differ, the sticky bit, cleanup rules, safe temporary files with mktemp, and security mistakes such as predictable names.

/tmp and /var/tmp are shared directories where any user or program may create temporary files. They are the standard place for scratch data: sort buffers, downloaded installers, lock files, sockets and test output.

The two differ in lifetime. /tmp is for short-lived files and is cleared at reboot on many systems (often because it is a RAM-backed tmpfs). /var/tmp is for temporary files that should survive a reboot, such as a large build that must be resumable. Both have the sticky bit (mode 1777), so everyone can create files but only the owner can delete or rename them.

Terminal
ls -ld /tmp /var/tmp
f=$(mktemp)           # safe unique file in /tmp
d=$(mktemp -d)        # safe unique directory

Try it

Bash Safe temporary files with mktemp

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 The sticky bit

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] [-p DIR] [TEMPLATE]     # TEMPLATE ends in XXXXXX
Item Description
mktemp Creates an empty file with a random name (mode 600) and prints its path.
mktemp -d Creates a directory (mode 700).
mktemp -p DIR Uses DIR instead of $TMPDIR or /tmp.
$TMPDIR Environment variable that programs should honor for the temporary location.
systemd-tmpfiles Cleans old entries according to /usr/lib/tmpfiles.d and /etc/tmpfiles.d.

Edge cases: temporary space may be small (a RAM-backed /tmp is limited to a fraction of memory), so large intermediate data may fit better in /var/tmp or a work directory.

Usage notes

  • Never invent predictable names such as /tmp/myscript.tmp or /tmp/out.$$. Another user can create a symlink with that name first and trick your script into overwriting a file it owns (a symlink attack). Use mktemp.
  • Sticky bit: without it, a world-writable directory would let users delete each other's files. With it, deletion needs ownership of the file or directory.
  • Clean up: use trap 'rm -rf "$d"' EXIT so errors and Ctrl+C do not leave files behind.
  • Retention: many distributions delete unused files after about 10 days in /tmp and 30 days in /var/tmp; the rules are in tmpfiles configuration. Do not store anything important there.
  • Not secret by default: other users can list directory names, so never put passwords in file names. Mode 600 files protect contents.
  • Private tmp: systemd services can use PrivateTmp=yes, and each gets its own empty /tmp; this is why a file you create is sometimes "missing" for a service.