Course contentsShow
← Linux
Lesson 271 of 1,8193. The Filesystem Hierarchy and POSIX StandardsFree lesson

Temporary Data Lifecycle Management

Implement policies for cleaning and managing temporary files in /tmp and /var/tmp.

In this lesson, you will learn how to distinguish between the two primary temporary storage directories and how to enforce policies to keep them from cluttering your system.

Linux provides two standard locations for transient data: /tmp and /var/tmp. While they both hold files that aren't meant to last forever, they serve different roles based on their persistence policy.

The Lifecycle Difference

The /tmp directory is designed for "short-term" data. By convention, it is often cleared every time your system reboots, or via a service that deletes files older than a specific age (like 10 days). Because this directory is often backed by memory (a tmpfs), writing to it is extremely fast, but everything inside is considered highly volatile.

In contrast, /var/tmp is designed for "long-term" temporary files. Data placed here is meant to persist across reboots. If a program needs to save a large cache file or a backup that might be needed after a crash or restart, it goes here. It is generally only cleared by manual intervention or more aggressive cleanup scripts.

Managing the Lifecycle

As a system administrator, you manage these lifecycles to prevent storage exhaustion. You can inspect your system’s current cleanup policy by looking at the configuration files for systemd-tmpfiles.

For example, a typical configuration file might contain a line like this:

#Type Path        Mode User Group Age  Argument
d     /tmp        1777 root root  10d
d     /var/tmp    1777 root root  30d

The Age field defines the lifecycle policy: the system will automatically remove files that haven't been accessed for the specified duration (10 days for /tmp and 30 days for /var/tmp).

You can manually trigger a cleanup of these files without a reboot by using the systemd-tmpfiles command:

# Apply cleanup rules immediately
systemd-tmpfiles --clean

By enforcing these policies, you ensure that your root partition remains healthy and that your temporary storage doesn't become a "junk drawer" that consumes disk space indefinitely.

Key Takeaway: /tmp is volatile and cleared frequently, while /var/tmp is persistent and intended for data that must survive across system reboots.