Alternative 01: (See alternative 02 or @Raffa's comment for shortcoming of alternative 01.)
Lets create our temporary directory, and own it as normal user so that we can write to it as normal user:
sudo mkdir /my_temp_files/
sudo chown $USER:$USER /my_temp_files/
Lets write a script to remove the temporary files at system startup. We will store the script in the following directory:
sudo mkdir -p /opt/remove_my_temp_files/bin/
Create a file named remove_my_temp_files.sh
as:
sudo touch /opt/remove_my_temp_files/bin/remove_my_temp_files.sh
sudo chmod 755 /opt/remove_my_temp_files/bin/remove_my_temp_files.sh
sudo chown root:root /opt/remove_my_temp_files/bin/remove_my_temp_files.sh
Write the folling to this file:
#!/bin/bash
if [[ -d /my_temp_files ]] ; then
rm -fr /my_temp_files/*
rm -fr /my_temp_files/.*
fi
Now, create a file named remove_my_temp_files.service
as:
sudo touch /etc/systemd/system/remove_my_temp_files.service
sudo chmod 644 /etc/systemd/system/remove_my_temp_files.service
sudo chown root:root /etc/systemd/system/remove_my_temp_files.service
And write the following to this file:
[Unit]
Description=Removes My Temporary Files.
[Service]
Type=oneshot
ExecStart=-/bin/bash -c '/opt/remove_my_temp_files/bin/remove_my_temp_files.sh'
[Install]
WantedBy=multi-user.target
Now, finally enable remove_my_temp_files.service
as:
sudo systemctl enable remove_my_temp_files.service
Now, files and directories created within this /my_temp_files/
directory will be deleted on every system startup.
Alternative 02:
As @Raffa suggests in his comments below, my way of rm /path/to/dir/*
is limited by the permitted size of command line, and using system service is bit too much for a task that can be simply accomplished by crontab
as he suggests, here is the alternative answer:
Write the following code in $HOME/bin/remove_user_temp.sh
:
#!/bin/bash
declare MY_TEMP_DIR="$HOME/my_temp_dir/"
if [[ -e "$MY_TEMP_DIR" ]] ; then
rm -rf "$MY_TEMP_DIR"
if [[ ! -e "$MY_TEMP_DIR" ]] ; then
mkdir "$MY_TEMP_DIR"
fi
fi
Make this script executable:
chmod +x $HOME/bin/remove_user_temp.sh
Edit the crontab
with:
crontab -e
Choose the file editor of your choice, if permitted, then begin with editing the cron job. At the end of the file, add:
@reboot $HOME/bin/remove_user_temp.sh
Save your edit, and exit crontab. This will also delete the files in your temporary directory at system reboot.
Alternative 03:
Easiest alternative is to put your temporary files in /tmp/
or /dev/shm/
directory. The files stored in /tmp
will get deleted next time your system boots up, while those in /dev/shm
are stored in computer's memory (rather than on the disk) and will be deleted when the computer shuts down or goes to reboot.
/tmp/
... Put your stuff there and they will be cleaned on reboot. – Raffa Feb 14 '24 at 11:26/tmp
and create/tmp/snap-private-tmp
but, restricts sharing files between applications in it ... However, the snap applications should be able to access/tmp
(on their own) normally unless/tmp
is a VFS mount (tmpfs and external media mounts included) and the latter could be solved by granting the snap application needed permission or by a bind mount of/tmp
in the user's home likemount --bind /tmp/ /home/user/tmp/
... Take that with a grain of salt though "oh snap :-)". – Raffa Feb 15 '24 at 06:37