Skip to main content
Temporary files—logs, caches, and leftover application data—accumulate over time. Left unmanaged, they can consume disk space and cause services to fail. On Red Hat Enterprise Linux (and other systemd-based systems), systemd-tmpfiles provides a declarative, automated way to create, manage, and clean these temporary files and directories. systemd-tmpfiles can:
  • Create directories and files at boot or on demand
  • Enforce ownership and permission bits
  • Remove old files according to age-based rules
  • Run at boot and periodically in the background so the system stays clean without manual intervention
A dark presentation slide titled "What Is systemd-tmpfiles?" showing four blue icons and short points explaining that it creates temporary files/directories, sets ownership and permissions, removes old files automatically, and runs at boot and periodically.

Configuration format (tmpfiles.d)

systemd-tmpfiles uses a simple, space-separated configuration format. Each configuration line is a series of fields that define an action and its parameters. Example: create a directory /mydirectory, set permissions and ownership, and remove files older than 30 seconds when cleanup runs:

Common tmpfiles type examples

  • d — create directory (and optionally clean contents)
  • f — create a file with specified content (use f+ to truncate)
  • w / w+ — write or append content to a file
  • D — create a directory and remove it completely when cleaned
  • e — clean up directory contents only
  • v — create subvolumes (Btrfs)
(Refer to the tmpfiles.d manual page for the full list and modifiers.)

Age units

The age field controls time-based cleanup when systemd-tmpfiles --clean runs (manually or via the system timer). For example, 30s means 30 seconds; 30d means 30 days.

Where rules are stored and how to run them

systemd-tmpfiles reads .conf files from standard locations. Administrator-created custom rules belong in /etc/tmpfiles.d/; distribution-provided rules are typically in /usr/lib/tmpfiles.d/. Common commands:
Configuration files must end with the .conf extension to be processed (for example, /etc/tmpfiles.d/mytmp.conf).
For a complete description of line types and modifiers, see the tmpfiles.d manual page excerpt below.
A screenshot of a Unix manual page (tmpfiles.d) showing descriptions of line types like f, f+, w, w+, d, and D with explanatory text. A pager footer reads "Manual page tmpfiles.d(5) line 115 (press h for help or q to quit)".

Demo: create a tmpfiles rule and observe creation and cleanup

  1. Create a configuration file called mytmp.conf in /etc/tmpfiles.d/:
Add this line to create /mydirectory with mode 0755, owned by root:root, and set cleanup age to 30 seconds:
Save and exit (:wq in vim).
  1. Apply creation rules immediately:
  1. Verify the directory exists and has the configured ownership and permissions:
  1. Create a test file and watch cleanup behavior:
Files are removed when systemd-tmpfiles --clean is run (manually or by the system timer). The cleanup action occurs when --clean executes, not precisely at the moment the age threshold is reached.
Be careful when authoring tmpfiles rules—incorrect paths or aggressive age settings may remove important data. Always test new rules in a controlled environment before deploying them to production.

Benefits and use cases

Using systemd-tmpfiles gives you a flexible, declarative approach to:
  • Ensure required runtime directories exist at boot (for services that expect them)
  • Enforce consistent ownership and permission policies
  • Automatically remove stale temporary data to reclaim disk space
  • Combine with at, cron, and systemd timers for broader maintenance automation
A dark-themed presentation slide titled "Summary" with bullet points explaining scheduling tools — the at command for one-time jobs, cron for recurring tasks, and systemd timers for modern scheduling.
Together with scheduling tools like at, cron, and systemd timers, systemd-tmpfiles helps you automate routine maintenance and keep systems clean and reliable.

References

Watch Video

Practice Lab