Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happened to the TMP environment variable?

Tags:

bash

tmp

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?

like image 295
boatcoder Avatar asked Mar 12 '10 18:03

boatcoder


Video Answer


4 Answers

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.

like image 133
Alok Singhal Avatar answered Oct 04 '22 16:10

Alok Singhal


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.

like image 32
Chris Lercher Avatar answered Oct 04 '22 16:10

Chris Lercher


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

like image 38
Ignacio Vazquez-Abrams Avatar answered Oct 04 '22 14:10

Ignacio Vazquez-Abrams


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)`
like image 28
Tom Saleeba Avatar answered Oct 04 '22 14:10

Tom Saleeba