Steven Jones
Steven Jones

Reputation: 5

Call a script on only the highest numbered file in a directory?

So what I need to do is go through lots of directories each containing about 5000 numbered files and run an awk script on only the highest numbered file in each one. The files all have the name g091, g092, g093 and so on up to, for instance, g094678. I only want to run the script on the highest one, and it feels like a giant waste of time to get into the directory, look through the whole thing, and then call it specifically on that file. Is there some command I can use to do this for me?

Upvotes: 0

Views: 43

Answers (1)

karakfa
karakfa

Reputation: 67477

try this

ls -1 | sed 's/^g//' | sort -nr | head -1 | sed 's/^/g/'

since this gets upvotes I think need to add @glenn jackman's better alternative here

ls | sort -k1.2nr | head -1

which uses subfield reference in sort.

Upvotes: 2

Related Questions