Automatically delete empty QubesIncoming subdirectories

I wrote a little script I use to implement the deletion of empty sub directories in my templates (execute as root):

cat <<EOF > /root/delete_empty_incoming.sh
#!/bin/sh
test ! -d /home/user/QubesIncoming || find '/home/user/QubesIncoming/' -mindepth 1 -type d -exec rmdir '{}' \;
EOF
chmod +x /root/delete_empty_incoming.sh
cat <<EOF > /etc/systemd/system/delete_empty_incoming.service
[Unit]
Description=Empty QubesIncoming
[Service]
ExecStart=/root/delete_empty_incoming.sh
[Install]
WantedBy=multi-user.target
EOF
systemctl enable delete_empty_incoming
systemctl start delete_empty_incoming
systemctl status delete_empty_incoming
3 Likes

good idea to use systemd here, you could improve it using systemd to check the condition and run the command instead of using a shell script. This will make deployment easier.

https://www.freedesktop.org/software/systemd/man/latest/systemd.path.html#PathExists=

2 Likes

Note that there is an --ignore-fail-on-non-empty option that makes rmdir silently ignore any non empty folder.
In my own case, since I only want to remove empty top-level folders, I simply use:
rmdir /home/user/QubesIncoming/* --ignore-fail-on-non-empty

3 Likes

This will be an option in 4.3

4 Likes