3

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

5
  • Anywhere the user has access to, e. g. in his home directory which is not cleaned up by any system tool. Commented Nov 9, 2021 at 9:31
  • Why not use mktemp and leave the details for the implementation to decide? Commented Nov 9, 2021 at 9:46
  • 2
    Why should /tmp ideally be reserved for the system? Commented Nov 9, 2021 at 10:22
  • should be reserved for the system - this is false. /tmp was created exactly with multi user environment in mind. Commented Nov 9, 2021 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. Commented Nov 9, 2021 at 11:08

1 Answer 1

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.