I am facing problem in zsh scripting. I have group of files in a directory such as:001_20160223.txt  /delete001_20161102.txt  /delete001_20161209.txt  /keep005_20160502.txt  /delete 005_20161105.txt  /delete005_20161206.txt  /keep009_20160502.txt  /delete009_20161105.txt  /delete009_20161205.txt  /keepNow, we have to sort files having same starting number for eg. 001 and then we have find latest file based on the date written in file name and delete old files. The same way we have to perform for other files also. So the final output in directory would be following:001_20161209.txt  /keep005_20161206.txt  /keep009_20161205.txt  /keepThis is what I have tried so far:files=($(find . -name ???_*.txt)) | if ((#files > 1)); then rm -- ${files[1,-2]}; fibut it is deleting all files except the last one. I want to create a separate group of files having same starting name and then to delete it.
How to find latest file and delete old files in a group of files in zsh scripting?
zsh