Question

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.

Was it helpful?

Solution

ls checkin.* | sort -t. -n | tail -1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top