Uggh, not
ls again.
Code:
files=( *.ogg )
highest=${files[-1]} #use ${files[@]:(-1)} in older bash versions
number="${highest%.*}" #to simply remove the file extension
re='([0-9]+)' #regex extraction of a number from a longer filename
[[ $highest =~ $re ]]
number=${BASH_REMATCH[1]}
However, this does require that the numbers are all zero-padded so that they automatically sort properly in the shell. Otherwise it gets more complex. I
strongly recommend always using zero-padded numbers in filenames for this reason.
You can find a script I wrote that zero-pads file numbers in this thread:
http://www.linuxquestions.org/questi...rectory-949189
However, also be warned that the shell's arithmetic engine treats numbers with leading zeros as
octal values, which will give you errors and bizarre-seeming outputs if you aren't aware of it. Check out this this page for more on how to handle that:
arithmetic expressions
See here too for more on using zero-padded numbers:
http://mywiki.wooledge.org/BashFAQ/018
And much more on bash's built-in string manipulation features here:
http://mywiki.wooledge.org/BashFAQ/100
Edit: another way to get the largest number, if not zero-padded:
Code:
greatest=0
for fname in *.ogg; do
(( ${fname%.*} > ${greatest%.*} )) && greatest=$fname
done
echo "${greatest%.*}"
re='([0-9]+)' gtnum=0
for fname in *.ogg; do
[[ $fname =~ $re ]] && fnum=${BASH_REMATCH[1]}
(( fnum > gtnum )) && greatest=$fname && gtnum=$num
done
echo "$greatest ; $gtnum"