Aqui está uma maneira:
#!/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]}")
Advertências :
- Isso pressupõe que você tenha apenas diretórios em
/opt/bla/myfiles
. - Ele excluirá tudo, exceto o diretório da versão mais recente.