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 ~/workspaces directory.

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 tmpfiles.d(5).

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 d.

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.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #systemd #linux.

This post was filed under articles.

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.