40

I always heard that the proper way to find the temporary folder on a UNIX machine was to look at the TMP environment variable. When writing code that worked on Windows as well as Linux, I would check for TEMP and TMP.

Today, I discovered that my Ubuntu install does not have that environment variable at all.

I know it seems you can always count on /tmp being there to put your temporary files in, but I understood that TMP was the way the user could tell you to put the temporary files someplace else.

Is that still the case?

1
  • 2
    How is this too localized? I'm pretty sure more than a couple people use temporary files in Linux.
    – Cascabel
    Commented Mar 12, 2010 at 19:09

6 Answers 6

24

You are probably thinking of TMPDIR.

This variable shall represent a pathname of a directory made available for programs that need a place to create temporary files.

3
  • 13
    My default installations of Ubuntu LTS Server 12.04 und 14.04 don't have any environment variable regarding the temporary directory, neither TMPDIR, nor TMP or TEMP, even if TMPDIR is documented in some places. In the end, this variable seems not reliable (anymore). Commented Mar 21, 2016 at 14:08
  • 2
    With a fallback: TMPDIR="{${TMPDIR:-$(dirname $(mktemp))/}"
    – Deilan
    Commented Jul 18, 2017 at 13:21
  • @Deilan mktemp defaults to using $TMPDIR or /tmp if $TMPDIR is not specified, so that fallback should probably just be "${TMPDIR:-/tmp}"
    – Andria
    Commented Jul 21 at 1:28
14

A good way to create a temporary directory is using mktemp, e.g.

mktemp -d -t

This way, you can even make sure, that your file names won't collide with existing files.

2
  • 6
    It's a good answer, but it's not an answer to the question above. Commented Jul 17, 2013 at 7:11
  • mktemp honors TMPDIR so this is better than using the variable by itsefl Commented Jul 14, 2021 at 22:30
12

POSIX/FHS says that /tmp is the root for temporary files, although some programs may choose to examine $TEMP or $TMP instead.

0
5

Similar to what @Chris Lercher said, I find that this works for me:

dirname $(mktemp -u -t tmp.XXXXXXXXXX)

That won't actually create a temp file (because of the -u flag to mktemp) but it'll give you the directory that temp files would be written to. This snippet works on OSX and Ubuntu (probably other *nix too).

If you want to set it into a variable, do something like this:

TMPDIR=`dirname $(mktemp -u -t tmp.XXXXXXXXXX)`
1
  • 4
    Or simply: dirname $(mktemp -u).
    – ekhumoro
    Commented Dec 28, 2016 at 20:40
2
Global variables command. [ Useful ]
# Let's look at environment variable's
printenv | sort
# search for TMP var
printenv | grep TMP
$TMP is not declared, because it's $TMPDIR [ Answer ]
## [ -d /tmp ] && echo 'is true' 
export TMP='/tmp' # In order to pass variables to a subshell.

Use $TMPDIR , this is the correct var name for Linux. Notice: the /tmp directory contents (files) will be deleted on Shutdown/REBOOT, This should be expected/desired.

0

FYI - ubuntu (and I assume other systemd based distros) does define the XDG_RUNTIME_DIR variable - which is a per-user temp space, so little more secure than just /tmp :

$ echo $XDG_RUNTIME_DIR

/run/user/1000
$ ls -ld $XDG_RUNTIME_DIR

drwx------ 2 ubuntu ubuntu 40 Dec 22 15:18 /run/user/1000

I think XDG_RUNTIME_DIR is maintained by systemd/pam, so it won't be set in Dockers or other non-systemd environments.

You can do something like this in ~/.bashrc if you like:

export TEMP="${XDG_RUNTIME_DIR:-/tmp}"

See: https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html https://www.freedesktop.org/wiki/

Also - there seems to me some caveats with XDG_RUNTIME_DIR and sudo: https://unix.stackexchange.com/questions/346841/why-does-sudo-i-not-set-xdg-runtime-dir-for-the-target-user

Not the answer you're looking for? Browse other questions tagged or ask your own question.