Bhargav Katkam Bhargav Katkam - 4 months ago 8
Linux Question

finding latest version files in each directory

I have files with versions, i.e. 2 digit suffixes for each file


ex: dphy/05E2.01, dphy/05E2.00, pll/05E2.43, pll/05E2.42 ...


I am writing a script to process these files. Could able to list, only versioned files with below command

find /path/ -type f -name '*.[0-9][0-9]'


But I am interested in only latest versions of files from each directory i.e.


dphy/05E2.01, pll/05E2.43


So, how to list only latest versions?

Answer
find /path/ -type f -name '*.[0-9][0-9]' |
awk -F '.' '{k=$0; sub(/\.[^.]+$/, "", k)} $NF>=max[k]{max[k]=$NF} 
      END{for (i in max) print i FS max[i]}'

/path/pll/05E2.43
/path/dphy/05E2.01