com zsh
:
# all *.<number>.py files in "n"umerical order
files=(*-<->.py(n))
# associative array whose key is the part before the last "-"
typeset -A latest
for f ($files) latest[${f%-*}]=$f
# plain array with the values of the associative array
keep=($latest)
# array disjunction:
echo rm -- ${files:|keep}
(remova echo
se estiver feliz).
Isso não faz nenhuma suposição sobre quais caracteres o resto dos nomes de arquivo pode conter. Com bash
(ou zsh
ou ksh
) e ferramentas GNU:
xargs -r0a <(printf '%s# all *.<number>.py files in "n"umerical order
files=(*-<->.py(n))
# associative array whose key is the part before the last "-"
typeset -A latest
for f ($files) latest[${f%-*}]=$f
# plain array with the values of the associative array
keep=($latest)
# array disjunction:
echo rm -- ${files:|keep}
' *-*.py |
grep -zEe '-[[:digit:]]+\.py$' |
sort -zrV |
awk -vRS='xargs -r0a <(printf '%s%pre%' *-*.py |
grep -zEe '-[[:digit:]]+\.py$' |
sort -zrV |
awk -vRS='%pre%' -vORS='%pre%' '
{key = $0; sub(/-[^-]*$/, "", key)}
seen[key]++') echo rm --
' -vORS='%pre%' '
{key = $0; sub(/-[^-]*$/, "", key)}
seen[key]++') echo rm --