File Cleaner
This script allows for automatic deletion of files a set number of days old from the date of creation. For example, your download client downloads something on the 18th of the month and your script is set for 3 days the file will not be deleted until the 22nd which causes it to pass the threshold and be deleted.
The number of days can be changed for any number of custom paths by simply copying the template and adjusting the marked values to fit your needs. We will use the ~/archive
in this example with a timer of 2 days. You can use this as a template to create your own deletion paths and timers.
Create Script
- Create a
~/scripts
directory if it does not already exist.
mkdir ~/scripts
- Navigate into the directory.
cd ~/scripts
- Next, create and open the script in the nano editor.
nano file-cleaner.sh
- Paste the following lines into it:
- Make sure to replace
username
with your Ultra username.
#!/bin/bash
find /home/username/archive/* -type d -ctime +2 -exec rm -rf {} \\;
- Save and exit the editor by pressing
Ctrl + X
thenY
thenENTER
.
Usage Instructions
- Now before we activate the script we will break it down into its parts that are changeable.
/home/ultradocs/"archive"/*
- This can be any path you wish but must include the
*
symbol at the end or else you risk removing the containing directory itself, in this case "archive". - The quotations around archive are just to tell Linux that the space in the middle is part of the directory’s name and only needs including if the directory you are targeting has spaces within the name.
ctime +2
- This tells the
find
command to pass any file two days older than its creation date to therm
(remove) command which can obviously be adjusted to any time frame you wish. - The rest of the commands in the set should not be messed with as it could have unintended results. Just to be sure we highlight another example of the line before we activate the automation and give it a manual test.
Test Script
- First, navigate to the
~/scripts
folder.
cd ~/scripts
- Next, we need to make the script executable.
chmod +x file-cleaner.sh
- The script is now ready to be executed, which can be done with the following command.
./file-cleaner.sh
- You may get output like this:
find: ‘/home/username/archive/*’: No such file or directory
- This is totally fine as either no files match your filter OR its deleted the files that do match your filter and cannot find anymore.
- We’d recommend you test this on a separate directory first with a value of
-ctime +0
to confirm all works if the previous test didn’t make it clear. This will delete all files inside a folder so be careful not to put in a pathway you care about.
Automate Script
Once you have concluded the script to be running successfully, you can schedule it to be automatically executed via crontab.
- Open crontab with the following command:
crontab -e
- Inside the crontab add the following line at the bottom of the crontab file.
- Make sure to replace
username
with your Ultra username. - The script will now run every 4 hours checking for files that have crossed the day threshold you have set and remove them.
0 */4 * * * /home/username/scripts/file-cleaner.sh
- Save and exit crontab by pressing
Ctrl+X
thenY
andENTER
.
If you require further assistance, you can open a support ticket here!
Feedback and suggestions regarding this article are welcome on our Discord server