Where is the temporary directory in Linux?
The Filesystem Hierarchy Standard version 3.0 says:
/tmp : Temporary files
The
/tmp
directory must be made available for programs that require temporary files.Programs must not assume that any files or directories in
/tmp
are preserved between invocations of the program.Rationale
IEEE standard POSIX.1-2008 lists requirements similar to the above section. Although data stored in
/tmp
may be deleted in a site-specific manner, it is recommended that files and directories located in/tmp
be deleted whenever the system is booted.FHS added this recommendation on the basis of historical precedent and common practice, but did not make it a requirement because system administration is not within the scope of this standard.
/var/tmp : Temporary files preserved between system reboots
The
/var/tmp
directory is made available for programs that require temporary files or directories that are preserved between system reboots. Therefore, data stored in/var/tmp
is more persistent than data in/tmp
.Files and directories located in
/var/tmp
must not be deleted when the system is booted. Although data stored in/var/tmp
is typically deleted in a site-specific manner, it is recommended that deletions occur at a less frequent interval than/tmp
.
Also the The Open Group Base Specifications Issue 7, Environment Variables mentions the following:
TMPDIR
This variable shall represent a pathname of a directory made available for programs that need a place to create temporary files.
This is an old question so today there is another option available. Linux distributions relying on systemd
(which is 90% of them) can now use $XDG_RUNTIME_DIR
directory (XDG Base Directory Specification) to store certain types of temporary files. It is generally located at /run/user/$uid
. This is a per-user directory with 700
permissions which provides better security. This is a tmpfs
mount which provides performance. The downside of tmpfs
is that it should only be used to keep small files and sockets.
I look at it as a marriage of /tmp
and /var/run
.
Yes /tmp is for general use. See here and here On the Filesystem Hierarchy Standard.
/tmp/ Temporary files (see also /var/tmp). Often not preserved between system reboots.
With some more details listed in the PDF.