user3668129
user3668129

Reputation: 4820

bash script - getting highest file name number from folder

I tries a lot of whys to solve the following problem without a solution :(

I have a folder which contains files with the following name pattern:

 number_name.txt

for example:

0_test.txt
17_test.txt
39_test.txt
99_test.txt
101_test.txt
17_test.txt

I need to get the file name which contain the max prefix number.

(when I compare the strings, I'm getting that "99_test.txt" is the highest which is not true....)

Thanks

Upvotes: 3

Views: 1983

Answers (1)

perreal
perreal

Reputation: 97948

Using version sort:

find -name '*.txt' | sort -V | tail -1

Upvotes: 5

Related Questions