filename is structured as name$timestamp.extension where:timestamp=`date +%Y%m%d-%H%M%S`So, if there are following files in a directory:name161214-082211.gzname161202-082211.gzname161220-082211.gzname161203-082211.gzname161201-082211.gzWith the execution of the code/script from your answer, the value 20 should be stored in the variable highest_dayhighest_day = 20Thank you for your kind answers in advance!
Extract highest day from time-stamp date within a filename
bash;shell;files;sort;timestamps
Without using file timestamps it gets a little clunky but this is one way it can be done:#!/usr/bin/env bashre=name([0-9]{6})-([0-9]{6})\.gzre2=([0-9]{2})([0-9]{2})([0-9]{2})for file in *.gzdo    if [[ $file =~ $re ]]    then        # BASH_REMATCH[n] on filename where n:        # [1] is date ie. 161202        # [2] is time ie. 082211        date=${BASH_REMATCH[1]}        # BASH_REMATCH[n] on date string where n:        # [1] is year ie. 16        # [2] is month ie. 12        # [3] is day ie. 02        [[ $date =~ $re2 ]] && day=${BASH_REMATCH[3]}        # Keep max day value        [[ $day > $highest_day ]] && highest_day=$day    fidoneecho $highest_day