Bash – select only one result from a bash glob

bashglobwildcards

I'm trying to write a script for work to automate some reporting on an output. The Log files are (currently, it's being 'standardise' in the future) stored in this sort of path structure:

/<root_path>/<process_one_path>/logs/<time_date_stamp>/<specific_log_file>

/<root_path>/<process_two_path>/logs/<different_time_date_stamp>/<specific_log_file>

Every part of the path is known except the time date stamps, which are always the latest in the folder.

If I try to use a wild card in place of the time date stamp, I get multiple results, e.g.:

> ls /<root_path>/<process_two_path>/logs/* [tab]
20130102-175103
20130118-090859
20130305-213506

I only want it to return the latest one, is this possible with Bash?

NB (I don't have zsh, and as lovely as it sounds I doubt we'll ever get it at work)

Best Answer

The following works in bash 4.2:

list=( /<root_path>/<process_two_path>/logs/* )
echo "${list[-1]}"

If your bash is an older version:

list=( /<root_path>/<process_two_path>/logs/* )
echo "${list[${#list[@]}-1]}"