J'ai écrit un 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
qui fonctionne très bien. Mais j'ai besoin d'une méthode plus avancée maintenant. Comme vous pouvez le constater, tous les fichiers du répertoire datant de plus de 3 jours sont supprimés. J'ai beaucoup de communiqués, par exemple: 1.31.1 1.31.2 1.31.3 ...... 1.31.150 1.32.1 1.32.2
et cetera. Je veux supprimer toutes les versions sauf la dernière de la version majeure 1.31/1.32. Comment? Ce ne peut pas être un nom statique, car un jour ce serait 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
Des idées, merci!
Voici un moyen:
#!/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]}")
mises en garde:
/opt/bla/myfiles
.