Select greatest numbered filename

ls(1) sorts files by name, so ls | tail -1 should do.


Obligatory zsh answer:

echo "The highest-numbered file is" filename_*.dat([-1])

This is a glob with the glob qualifier [NUM] to retain only the NUMth match (a negative value counts from the last match). If you have numbers of varying width, add the n qualifier to

% ls
filename_1.dat filename_12.dat filename_17.dat filename_2.dat filename_8.dat
% echo filename_*.dat([-1])
filename_8.dat
% echo filename_*.dat(n[-1])
filename_17.dat

Globbing only happens in a context that looks for a list of words, so if you want to assign the filename to a variable, you need to make it an array which will contain one element:

latest=(filename_*.dat[-1])
echo "The highest-numbered file is $latest"

In any shell, you can set the positional arguments to the full list of matches and keep the last one.

set_latest () {
  eval "latest=\${$#}"
}
set_latest filename_*.dat
echo "The highest-numbered file is $latest"

Keep in mind that this returns the last in alphabetical order, not in numerical order, e.g. filename_10.dat is after filename_09.dat but before filename_9.dat.