Shell scripting - how to get all files with a common name, and order those files

162 views Asked by At

So I'm learning how powerful shell scripting is and love it, but am having a really hard time with this...

I have a bunch of files named as: checkin.10 checkin.11 checkin.12

The number after the . will always be a # and goes up sequentially.

I need to find all checkin.* files in a dir, and return the file with the highest # in the filename. So for the above files, I would need to return 'checkin.12'. My logic was to get all checkin.* file names, loop through them and compare the # to the # of the previous file stored in a variable. If the current file's # is higher, I update the variable # to be the current files #, if it's lower, I move onto the next one. This way I'm able to get checkin.12 returned as the highest # and do what I need to do with checkin.12. There has to be a better way to do this, was hoping someone could show me an example of another approach.

1

There are 1 answers

2
Jonathan Leffler On BEST ANSWER
ls checkin.* | sort -t. -n | tail -1