2

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!

3
  • 1
    Please edit your question and show us the output of ls -l $dir/releases/ . Also explain if this needs to be recursive or if all directories will be direct children of $dir/releases.
    – terdon
    Jul 26, 2017 at 16:20
  • nice eye, I did a mistype of copy paste it is dir=/opt/myfiles
    – pr0logas
    Jul 27, 2017 at 7:59
  • The theoretical way to go: You can find the two youngest releases an exclude them with the -prune option. find [path] [conditions to prune] -prune -o [your usual conditions] [actions to perform]
    – Peter
    Jul 27, 2017 at 8:46

1 Answer 1

1

Here's one way:

#!/bin/bash

targetDir=/opt/bla/myfiles;
## declare 'releases' as an associative array
declare -A releases
cd "$targetDir"
## Iterate over all directories in $targetDir. 
for dir in */; do
        ## remove the trailing slash
        dir="${dir%/}"
        ## Extract the version string
        ver="${dir%%-*}"

        ## Use the version as the key for the associative array 
        releases["$ver"]="$dir";
done
## Get the newest version; sort -h understands version numbers
newestVersion=$( printf '%s\n' "${!releases[@]}" | sort -h | tail -n1)
## This is probably not needed as extended globbing should be on by default
shopt -s extglob
## Delete the rest. The '$targetDir/' isn't necessary but it's safer
## just in case we're not actually where we think we are. 
rm -rf  $targetDir/!("${releases[$newestVersion]}")

Caveats:

  1. This assumes you only have directories in /opt/bla/myfiles.
  2. It will delete everything but the directory of the newest version.
1
  • hey, thanks for the answer, I will try to test ASAP. That array solution looks promising.
    – pr0logas
    Jul 27, 2017 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .