I have searched lots of forums and couldn't find an answer to this question.
We have a synology diskstation that runs linux and we want to use it as a temporary share disk for our employees.
Here lies the problem since every script that I have found looks at the date from when it was edited, but we need it to look at the date it was added to the temporary share folder.
So in summary: a nas that runs linux needs a script that automaticaly checks every 10 days if there are files in the nas that were added over 10 days ago and delete them if they're older.
if there are any other solutions that would fine too.
We are running the following cleantmp.sh in production environment:
#!/bin/sh
PATH=/usr/bin:/bin:/usr/sbin
export PATH
max_days=30
case $1 in
[0-9]*)
max_days=$1
shift
;;
esac
dirs_to_clean=${@:-/tmp}
owner_to_keep="root nobody"
omit=""
for i in $owner_to_keep
do
omit="$omit ( ! -user $i )"
done
for dir in $dirs_to_clean
do
[ -d $dir ] &&
cd $dir &&
find . -depth \! -type d \( -mtime +$max_days -o -mtime -0 \) \( -atime +$max_days -ctime +5 -o -ctime +$max_days -atime +5 -o -type l \) $omit -exec rm -f {} \; -o -type d -empty $omit -mtime +$max_days -exec rmdir {} \; 2>/dev/null
sleep 1
done
exit 0
# We assume that a full backup (where atime or ctime changes) happens less often than every 5 days.
# We cd first, so there is a good chance that a too long directory path can be accessed
# if -empty is unsupported, replace it by -links 2
# A just emptied directory is deleted immediately or after another max_days
# -mtime -0 detects files with a future time stamp (were extracted from an obscure archive)
A nightly root crontab entry (crontab -e; crontab -l) could be
You can add more /2ndpath/to/clean /3rdpath/to/clean ...
Nightly means, the remaining files are never older than 10+2=12 days.
For safety, frequently read files are not deleted.