3

Once /tmp ideally should be reserved for the system, where can a user create temporary files safely?

  • Anywhere the user has access to, e. g. in his home directory which is not cleaned up by any system tool. – eblock Nov 09 '21 at 09:31
  • Why not use mktemp and leave the details for the implementation to decide? – frippe Nov 09 '21 at 09:46
  • 2
    Why should /tmp ideally be reserved for the system? – ilkkachu Nov 09 '21 at 10:22
  • should be reserved for the system - this is false. /tmp was created exactly with multi user environment in mind. – Artem S. Tashkinov Nov 09 '21 at 10:57
  • Back when I started out 30 years ago with the systems I looked after, it used to be /var/tmp was the place for users to store temporary files. That directory also tended to be preserved across reboots, whereas /tmp was wiped, especially since it was a virtual mount. That no longer seems to happen in many places. – Bib Nov 09 '21 at 11:08

1 Answers1

7

The right place to create temporary files¹ is, and has always been, the directory indicated by the TMPDIR environment variable, with /tmp as a fallback. If a system has a location for temporary files other than /tmp, setting TMPDIR is how it advertises this location.

In a shell script, that's ${TMPDIR:-/tmp}, although typically you should call the mktemp command and let it decide.

¹ Of course, I'm not talking about temporary files that need to be in a specific directory, for example because they'll be atomically moved into place once their content is ready.