2

I'm using Ubuntu 22.04LTS. I would like to create a folder "temp" where during my session I save the files that are, as the name suggest, temporary. How can I define a scritp that at shutdown automatically deletes the contents of this folder?

Ex: I download few documents that I need for some tasks but at the end of the day I don't need to keep them, thus avoiding to accumulate useless files.

Iano
  • 23

3 Answers3

4

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.

rusty
  • 16,917
3

There is a way built into Linux, called tmpfs, that can achieve exactly this. To use it, make a folder tmpfs, and everything inside the folder after you made it tmpfs will evaporate when it is no longer tmpfs or when the computer shuts down. The downsides are that files in tmpfs folders can only reside in RAM or swap.

# mount -t tmpfs tmpfs temp

The filesystem don't store if a folder is tmpfs. To permanently mark a folder as tmpfs, you should edit /etc/fstab:

tmpfs   /tmp    tmpfs   defaults        0       2

In addition to tmpfs, Ubuntu's services autoclean the /tmp directory on startup (thanks raj), so you can place any temporary files there.

mcendu
  • 313
0

Here is a much simpler version of @rusty's answer. I will use the official systemd-tmpfiles mechanism. Note that just like that answer, my answer removes things at power on not power off. Just paste the following into /etc/tmpfiles.d/my-temp-files-1503475-1004020.conf :

D /my_temp_dir 0755 root root

Change root to $USER if necessary. For the user-only alternative, you can use ~/.config/user-tmpfiles.d/ after systemctl --user enable systemd-tmpfiles-setup. This uses systemd-tmpfiles like @muru's answer but it uses systemd-tmpfiles-setup not systemd-tmpfiles-clean. There is no need to use a mix of bash scripts in /opt, enableing and installing .services, manual rm, chmod, chown, /opt, or cron for such a basic task.

Daniel T
  • 5,339