I've got written a script:
#!/bin/bash
dir=/opt/bla/myfiles
# Check disk usage usep=$(df -H | grep /dev/sda3 | awk '{print $5}' | cut -d '%' -f1) if [ $usep -ge 90 ]; then
echo "$(date) Running out of space in /dev/sda3 with $usep percent - so deleting action is taking!" >> /var/log/messages &&
find $dir/releases/* -mtime +3 -exec rm {} \; else
echo "$(date) Disk space is $usep percent - no action required!" /var/log/messages fi
which works great. But I need more advanced method now. As you can see it deletes all the files in dir older than 3 days. I've got a lot of releases, for example: 1.31.1 1.31.2 1.31.3 ...... 1.31.150 1.32.1 1.32.2
and so one. I want to delete all releases except the last one from major build 1.31/1.32. How to? It can't be a static name, because one day it would be 2.32.150
ls -l
total 520
drwxr-xr-x 2 jenkins jenkins 4096 Jun 23 15:45 0.0.31-SNAPSHOT
drwxr-xr-x 2 jenkins jenkins 4096 Jun 23 15:45 1.33.0.100-RELEASE
drwxr-xr-x 2 jenkins jenkins 4096 Jun 23 15:45 1.33.0.101-RELEASE
drwxr-xr-x 2 jenkins jenkins 4096 Jun 8 11:00 1.33.0.58-RELEASE
drwxr-xr-x 2 jenkins jenkins 4096 Jun 8 11:00 1.33.0.59-RELEASE
drwxr-xr-x 2 jenkins jenkins 4096 Jun 8 11:00 1.33.0.64-RELEASE
drwxr-xr-x 2 jenkins jenkins 4096 Jun 8 11:00 1.33.0.66-RELEASE
Any ideas, thanks!
ls -l $dir/releases/
. Also explain if this needs to be recursive or if all directories will be direct children of$dir/releases
.-prune
option.find [path] [conditions to prune] -prune -o [your usual conditions] [actions to perform]