Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 879
user3668129 Avatar asked Mar 07 '26 20:03

user3668129


1 Answers

Using version sort:

find -name '*.txt' | sort -V | tail -1
like image 103
perreal Avatar answered Mar 10 '26 16:03

perreal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!