Using systemd-tmpfiles to manage temporary files and directories
Over the last few years I've found that I needed to have a more structured directory hierarchy for my personal and professional projects, under the
This, in conjunction with a
wksp shell function to easily move around, has been beneficial and allows me to structure things in terms of:
adventofcode cv cucumber /common /cucumber-jvm hacktoberfest jvt.me tmp
However, there are cases where I don't want to create a persistent workspace in this hierarchy, for instance a drive-by Open Source contribution. Because it was for temporary usage, I'd call it
tmp and every so often clean it out.
In 2016, I discovered
systemd-tmpfiles(8) which can manage this automated clean up for us, as well as doing a lot of other powerful stuff like creating temporary files, BTRFS volumes, pipes, symlinks, or copy files around, which can be found documented in
The configuration I've got makes sure that it creates the directory, sets permissions to just my user, and clear files out after a week of existing:
D /home/jamie/workspaces/tmp 0700 jamie jamie 1w -
Something I have noticed is that this gets cleared every time I boot - which is fine, because I'm happy treating it as a scratch space - but it's likely that I need to turn the
D to a
Update 2021-12-13: I've since changed it to a
d and the files persist across reboots, and are deleted after a week of being present - awesome!
Having a "scratch" space to work is really useful, and it's even better when it's truly ephemeral and you don't need to worry about cleaning it out, or that others may be able to see what you're doing, as is possible if using
/tmp on a shared system.