Does Linux have a standard temporary directory for general use, like Windows's C:\Temp
folder? If so, where is it located? I found an SO question about finding a tmp
directory programmatically, but I want to set a temp location in an XML config file ahead of time.
5 Answers
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.
systemd has a say on this too:
/tmp/
and/var/tmp/
are two world-writable directories Linux systems provide for temporary files. The former is typically on tmpfs and thus backed by RAM/swap, and flushed out on each reboot. The latter is typically a proper, persistent file system, and thus backed by physical storage. This means:
/tmp/
should be used for smaller, size-bounded files only;/var/tmp/
should be used for everything else.Data that shall survive a boot cycle shouldn’t be placed in
/tmp/
.If the
$TMPDIR
environment variable is set, use that path, and neither use/tmp/
nor/var/tmp/
directly.See file-hierarchy(7) for details about these two (and most other) directories of a Linux system.
The paths can be queried with systemd-path:
$ systemd-path temporary
/tmp
$ systemd-path temporary-large
/var/tmp
FreeBSD, NetBSD, OpenBSD and DragonFly have something similar described in the hier(7) man page.
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
- keeping files private - This is a
tmpfs
mount (aka RAM drive) with great performance - It gets cleaned up automatically
The downside of tmpfs
is that it should only be used to store small files (PID files, lock files, sockets etc).
-
On current systemd based Linux distributions,
/var/run
is a compatibility symlink pointing to/run
. Commented Jan 11, 2023 at 15:49
You cannot choose a single temporary directory name ahead of time that will work for any Linux system. In fact, you can't do that on Windows either. According to Wikipedia's article on temporary folders, the temporary directory on Windows is determined by the environment variable TEMP. If you were simply using c:\Temp
as a temporary directory on a Windows system that set TEMP to something else, then any program using your XML file to choose a temporary directory would fail.
In short, the system temporary directory is determined by the environment on all modern operating systems that I know of, including both Windows and any UNIX-like system. Setting a single static path as your temporary directory will only work as long as the defaults have not been changed.
Anyway, the standard temporary directory in a typical Linux system is /tmp
. It is the equivalent of C:\Temp
in the sense that it is only the default temporary directory, not universal. Even if /tmp
is available, if a user (or the system) has set the TEMP environment variable, the value of that variable should be used instead.
You could try choosing a temporary directory relative to the user's home directory, which you can create.
-
Some programs like bash, gcc, inkscape or lynx use the
TMPDIR
environment variable. Commented Sep 7, 2011 at 0:28
The initial question was: Does Linux have a standard temporary directory. If so, where is it located? The tmp folder(s) are found at: File System/tmp & File System/var/tmp The /tmp contents could be deleted - if you know which files/folders are required/not required by various apps on an "as required" basis. Unrequired files are usually those downloaded by an app to facilitate the app itself at that point only- therefore not further required after the app has performed. The /var/tmp folder should not be touched at all.
-
5This answer does not provide more information than the accepted answer, so it is quite useless.– zx485Commented Jul 27, 2019 at 17:33
/tmp
, but they were ambiguous about whether that's standard for Linux or just a common convention.C:\Temp
is most certainly not standard. Windows 95 already had%TEMP%
pointing toC:\WINDOWS\Temp
.sys_get_temp_dir();