Linux: How to create self deleting files!

In this article i will show you how you can create self deleting files!, files that will be deleted them self after a defined number of…

Linux: How to create self deleting files!
Photo by Sonja Langford on Unsplash

In this article i will show you how you can create self deleting files!, files that will be deleted them self after a defined number of minutes or a specific date! You might ask why you would need this? well in my case is because i wanted to create a mechanism that will not allow a script to sent an email with events from the system if the last email has been sent in the last one hour! but you might find use as well in your occasions!

Creating the command

Linux does not have any built in command to create self — deleting files so we will create one! its much easier than you think :)

Create the following script an save it as sdelete.sh

#!/bin/bash 
 
# Check if the correct number of arguments is provided 
if [ "$#" -ne 2 ]; then 
    echo "Usage: $0 <filename> <time>" 
    echo "Example: $0 /path/to/your/file 'now + 5 minutes'" 
    echo "Example: $0 /path/to/your/file '12:30 PM tomorrow'" 
    exit 1 
fi 
 
# Assign arguments to variables 
filename=$1 
time=$2 
 
# Check if the filename is a full path 
if [[ "$filename" != /* ]]; then 
    echo "Error: Filename must be a full path." 
    exit 1 
fi 
 
# Create the file 
touch "$filename" 
if [ $? -eq 0 ]; then 
    echo "File '$filename' created." 
else 
    echo "Error: Failed to create file '$filename'." 
    exit 1 
fi 
 
# Schedule the file for deletion using 'at' 
echo "rm \"$filename\"" | at "$time" 
if [ $? -eq 0 ]; then 
    echo "File '$filename' is scheduled for deletion at $time." 
else 
    echo "Error: Failed to schedule deletion with 'at' command." 
    exit 1 
fi

Make it executable with chmod

chmod +x sdelete.sh

Testing

Lets make some tests now to verify that the script works

Creating and Deleting a file after an amount of minutes

$ ./sdelete.sh ~/hello.txt 'now +1 minute' 
File '/home/administrator/hello.txt' created. 
warning: commands will be executed using /bin/sh 
job 6 at Thu Jul 25 17:18:00 2024 
File '/home/administrator/hello.txt' is scheduled for deletion at now +1 minute. 
 
$ ls -l | grep -i hello 
-rw-rw-r-- 1 administrator administrator          0 Jul 25 17:17 hello.txt

Checking after one minute we can verify that hello.txt does not exist

Creating and deleting a file in a defined time stamp

Also we can define a timestamp as parameter that the file will be deleted, in this case is 17:50

$ ./sdelete.sh ~/hello.txt '17:50' 
File '/home/administrator/hello.txt' created. 
warning: commands will be executed using /bin/sh 
job 7 at Thu Jul 25 17:50:00 2024 
File '/home/administrator/hello.txt' is scheduled for deletion at 17:50.

How it works?

The script relies in the at command. The at command in Linux is used to schedule commands to be executed at a later time. It reads commands from standard input or from a specified file and executes them at a specified time.

Here are some simple examples about the at command

Schedule a task in the future

echo "ls -l /tmp" | at now + 1 minute

Schedule a command to run at a specific time

echo "echo 'Hello, World!'" | at 10:30 AM

Schedule a Command to Run at a Specific Date and Time

echo "shutdown -h now" | at 11:59 PM 12/31/2024

Conclusion

Well.. self deleting files might not be super useful but i hope you enjoyed this short article!