194

On most FHS systems, there is a /tmp folder as well as a /var/tmp folder. What is the functional difference between the two?

Elijah Lynn
  • 1,045
Wesley
  • 14,263

4 Answers4

172

/tmp is meant as fast (possibly small) storage with a short lifetime. Many systems clean /tmp very fast - on some systems it is even mounted as RAM-disk. /var/tmp is normally located on a physical disk, is larger and can hold temporary files for a longer time. Some systems also clean /var/tmp, but less often.

Also note that /var/tmp might not be available in the early boot-process, as /var and/or /var/tmp may be mountpoints. Thus it is a little bit comparable to the difference between /bin and /usr/bin. The first is available during early boot - the latter after the system has mounted everything. So most boot-scripts will use /tmp and not /var/tmp for temporary files.

Another (upcoming) location on Linux for temporary files is /dev/shm.

dr_
  • 29,602
Nils
  • 18,492
  • 6
    I find it a bit ironic that /bin and /usr/bin are actually the same these days (symlinks). ;-) – Felix Schwarz Apr 05 '16 at 14:28
  • 3
    @Felix Not on all distributions yet. But Linux is moving that way. Propably because disks get bigger and bigger. – Nils May 05 '16 at 21:34
  • https://serverfault.com/questions/220000/difference-and-correct-usage-for-tmp-and-var-tmp – 8c6b5df0d16ade6c Aug 08 '22 at 21:35
  • 1
    So, How should /tmp and /var/tmp should be sized considering /tmp is available before /var/tmp be mounted? – Israel Oct 24 '22 at 01:44
  • @Israel 4 GB should be enough for each. But there has to be a policy not to use tmp-directies for long lasting storage. – Nils Oct 24 '22 at 07:57
44

/tmp may be, and sometimes is, cleaned on reboot. /var/tmp is preserved between reboots.

See the Wikipedia article on the FHS.

Kevin
  • 40,767
njsg
  • 13,495
31

POSIX Base Specifications, Issue 7 on /tmp:

The following directory shall exist on conforming systems and shall be used as described:

/tmp

A directory made available for applications that need a place to create temporary files. Applications shall be allowed to create files in this directory, but shall not assume that such files are preserved between invocations of the application.

The File Hierarchy Standard 2.3 on /tmp:

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 P1003.2 (POSIX, part 2) makes requirements that are 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.

POSIX does not specify /var/tmp. The FHS does though:

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.

osvein
  • 199
16

They have the same purpose and functionality. Every version of UNIX/Linux will handle these directories differently. Historically, before the advent of RAM/swap based filesystems, you had disk-less systems where the / and /usr filesystems would be read-only and /var (variable) would be read-write. The /tmp name would be a symbolic link to /var/tmp. Later, disk-less systems fell out of style, disk space became cheaper (to have larger root filesytems) and technology allowed for filesystems mounted from memory instead of disk. The /var/tmp directory fell out of style, but is still used by some programs.

These days, more security are set up by default on /tmp, like g+s,+t permissions, but not on /var/tmp. Additionally, /var/tmp is rarely mounted from RAM or swap.

Arcege
  • 22,536