I have a large collection of files that look like this:
Some Name da-1234567-1.py
Some Name da-1234567-2.py
Some Name da-1234567-4.py
Other Name di-5678912-3.py
Other Name di-5678912-4.py
Other Name di-5678912-5.py
I would like to remove all versions and only keep:
Some Name da-1234567-4.py
Other Name di-5678912-5.py
I realise my question is similar to Find latest folder/file version and remove - no time stamp data . However its answer does not work for me and my knowledge of bash and/or regular expression is insufficient to change the solution to cater my needs. Unfortunately I do not have the reputation score to comment thus I do not see another option than to post a new question.
I tried the following from above question:
for file in *.*
do
[[ -d "$file" || $file =~ _[[:digit:]]{3}\. ]] && continue
echo -n "Considering $file: " >&2
extn="${file/*.}"
versions=("$file")
keep="$file"
# Look at matching files
for version in "${file%.$extn}"_???."$extn"
do
[[ -f "$version" ]] || continue
# Save every one. Identify the current last
versions+=("$version")
keep="$version"
echo -n "$version " >&2
done
echo "==> keep $keep" >&2
# Delete them all except the last
for version in "${versions[@]}"
do
[[ "$version" != "$keep" ]] && echo rm -f -- "$version"
done
[[ "$keep" != "$file" ]] && echo mv -f -- "$keep" "$file"
done
I realise that it probably goes wrong in this part:
$file =~ _[[:digit:]]{3}\.
, since opposed to the above question my file ending is -n and not _nnn, but I do not see how to fix it.
-
, a single digit,.py
? If so just change the_
to a-
, the3
to a1
and you should be good to go. You could also delete the{3}
part because1
is default.