Systemd-tmpfiles
systemd-tmpfiles is a utility within systemd that automates the creation, cleanup, and deletion of volatile and temporary files and directories based on configuration files. It helps manage commonly used paths such as /tmp, /run, /var/tmp, /sys, and /proc, ensuring proper setup during boot and clean-up during uptime.
Configuration Files: tmpfiles.d
Configuration files live in directories such as:
/etc/tmpfiles.d/*.conf/run/tmpfiles.d/*.conf/usr/local/lib/tmpfiles.d/*.conf/usr/lib/tmpfiles.d/*.conf~/.config/user-tmpfiles.d/*.conf$XDG_RUNTIME_DIR/user-tmpfiles.d/*.conf~/.local/share/user-tmpfiles.d/*.conf
Format
Each config line consists of:
Type Path Mode User Group Age Argument
Syntax Overview
Types
Here are some key line types:
| Type | Description |
|---|---|
f, f+ | Create (or truncate with +) a file; write content if provided (f+ always). |
w, w+ | Write to an existing file; + appends. Supports globs. |
d, D | d: create directory; D: same, but contents removed with --remove. |
e | Adjust existing directory (mode/owner), optionally clean contents based on age. |
p, p+ | Create FIFO; p+ replaces any existing file. |
L, L+ | Create symlink (with forced replacement if +). |
Age Field
Used for cleanup decisions, it expresses time offsets using suffixes like s, m/min, h, d, w, etc. Defaults to seconds if no suffix is given. Combine multiple units like 1d2h.
An Age prefixed with ~ applies cleanup only one level deep. Omitting or using - disables automatic cleanup.
Using age = 0 means unconditional cleanup upon every run.
Mode, User, Group
Mode,User,Group: adjust permissions and ownership.- Numeric IDs or names accepted.
-means use the invoking user (for user-mode runs). - Prefixing user/group with
:only affects inode creation, not modifications to existing entries.([Freedesktop][2])
Usage
# Create defined files & dirs
sudo systemd-tmpfiles --create
# Clean based on age
sudo systemd-tmpfiles --clean
# Remove items flagged for deletion
sudo systemd-tmpfiles --remove
# Combine remove and create (common during boot)
sudo systemd-tmpfiles --remove --create
# User mode (no sudo)
systemd-tmpfiles --user --createExamples
Creating and Cleaning a Directory
Add to /etc/tmpfiles.d/my.conf:
d /tmp/myapp 0755 myuser mygroup 1h -