# Bash on Synology - Delete recycle bin entries over X days old

Synology NASs have a Task Scheduler that allows you to schedule deletion of files over X days old from recycle bins. This feature isn't working for me–running the task results in no files being deleted. The task works if I schedule a full deletion of the recycle bin, but I'd prefer to only delete a file if it sits in the bin for more than X days. As such, I've created this user script to do so. I want to make sure that it deletes exactly as I want.

• Only entries within the recycle bin of the specific shared folder /volume1/share1/#recycle/
• Only files that are over 60 days old
• Not the recycle bin folder itself
• Delete empty folders

Script:

deletepath="/volume1/share1/#recycle/"
logpath="/volume1/share2/SynoScripts/logs/deleteOlderThanXDays.txt"
errorpath="/volume1/share2/SynoScripts/errors/deleteOlderThanXDays.txt"
now=date "+%Y-%m-%d %H:%M:%S"

echo "" >> $logpath echo$now >> $logpath echo "" >>$errorpath
echo $now >>$errorpath

# Delete files
/usr/bin/find $deletepath -type f -mtime +60 -exec rm -v {} \; >>$logpath 2>>$errorpath # Delete empty folders /usr/bin/find$deletepath -mindepth 1 -type d -empty -exec rmdir {} \; >>$logpath 2>>$errorpath


Does this script appear to satisfy my requirements?

• Are you required to remove non-plain files, such as device nodes, sockets, FIFOs and devices? – Toby Speight Mar 4 at 20:56

If this is intended to be run as a command, I recommend you add a suitable shebang line. Although the question is tagged , there's nothing here that isn't portable POSIX shell, so I recommend

#!/bin/sh


Is it intentional that the paths all share a common initial prefix /volume1 and that the log and error paths share a longer common prefix? If so, encode that for easier re-use:

volume=/volume1
scriptdir=$volume/share2/SynoScripts logpath=$scriptdir/logs/deleteOlderThanXDays.txt
errorpath=$scriptdir/errors/deleteOlderThanXDays.txt  Personally, I'd call those last two logfile and errorfile for clarity. There's no need to quote the values in these assignments, but the values should be quoted when used later, so that we don't break the script when they change to include spaces or other significant characters. Instead of multiple echo commands, consider using a single date with tee: date '+%n%Y-%m-%d %T' | tee -a "$logpath" >>"$errorpath"  After that, we can simply redirect all output and errors: exec >>"$logpath" 2>>"$errorpath"  When using find, prefer to group many arguments into a few commands, using + instead of \;: find "$deletepath" \! -type d -mtime +60 -exec rm -v '{}' +
find "$deletepath" -mindepth 1 -type d -empty -exec rmdir -v '{}' +  I assume you meant to use -v consistently for both commands here. Modified version #!/bin/sh volume=/volume1 scriptdir="$volume/share2/SynoScripts"

deletepath="$volume/share1/#recycle" logpath="$scriptdir/logs/deleteOlderThanXDays.txt"
errorpath="$scriptdir/errors/deleteOlderThanXDays.txt" # redirect all output and errors exec >>"$logpath" 2>>"$errorpath" # log the start time to both logs date '+%n%Y-%m-%d %T' | tee -a "$errorpath"

# delete old non-directory files (including devices, sockets, etc)
find "$deletepath" \! -type d -mtime +60 -exec rm -v '{}' + # delete empty directories, regardless of age find "$deletepath" -mindepth 1 -type d -empty -exec rmdir -v '{}' +