I have some jobs that run and will fill up disk drives if not pruned.
I put together the simple script below that I can call with a directory and the number of files I want to keep. The oldest files will be deleted. I add it to cron jobs with a command in this pattern:
~/DelOverNumberFiles.sh ~/public_html/FrontDoor/Camera\ 01 200 Note: there are 2 params, the \ escapes the space before the 01
Copy into DelOverNumberFiles.sh
#!/bin/bash
limit=$2
dir=$1
Cnt=0
for line in `ls -t "$1"`
do
if [[ $Cnt -gt $limit ]]
then
rm "$dir/$line"
echo "rm $dir/$line"
fi
Cnt=`expr $Cnt + 1`
done
While I know this is not the most professional way to do this, I wanted something very simple I could play with and should have few dependencies so I could use it on shared servers that I do not have control over.