The main difference between a temporary file and a regular file in Linux

files

What is the main difference between a temporary file ("tempfile") and a regular file in Linux?

The only main difference I recon between these two inodes is that in general, a "temporary file" has a much shorter life cycle than a "regular file", from whatever reason, while both are by principle, practically temporary (at least from the fact this universe is heading to a total heat death).

References:

  1. How does a pipe differ from a temporary file?

  2. How does a FIFO (named pipe) differs from a regular pipe (unnamed pipe)?

Best Answer

There is absolutely no difference between a temporary file and a regular file.

A temporary file is a regular file, and a regular file is "regular" as opposed to being a directory, or a device special file, or named pipe etc.

The only difference is, as you point out, the typical use of the file. When a program or script creates a "temporary file", it is typically a regular file that is used to store temporary data that is not needed beyond the lifetime of the process that created it. Such files are often created in $TMPDIR.

POSIX defines:

File: An object that can be written to, or read from, or both. A file has certain attributes, including access permissions and type. File types include regular file, character special file, block special file, FIFO special file, symbolic link, socket, and directory. Other types of files may be supported by the implementation.

Regular file: A file that is a randomly accessible sequence of bytes, with no further structure imposed by the system.

It does not, however, care to say anything about the definition of a "temporary file", because it's not a specific file type (rather, a way of using regular files).

Related Question