
El nombre del archivo está estructurado como name$timestamp.extension
donde:
timestamp=`date "+%Y%m%d-%H%M%S"`
Entonces, si hay los siguientes archivos en un directorio:
name161214-082211.gz
name161202-082211.gz
name161220-082211.gz
name161203-082211.gz
name161201-082211.gz
Con la ejecución del código/script de su respuesta, el valor 20
debe almacenarse en la variablehighest_day
highest_day = 20
Respuesta1
Sin utilizar marcas de tiempo de archivos, se vuelve un poco complicado, pero esta es una forma de hacerlo:
#!/usr/bin/env bash
re="name([0-9]{6})-([0-9]{6})\.gz"
re2="([0-9]{2})([0-9]{2})([0-9]{2})"
for file in *.gz
do
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
fi
done
echo $highest_day
Respuesta2
puedes hacer asi..
highest_day= $(for i in *.gz; do echo ${i:8:2}; done | sort -n | tail -1)
Respuesta3
Si la lista de archivos está dentro de un archivo, algo como:
$ cd dir
$ ls -1 * >infile
Esta tubería hace el trabajo:
$ sed 's/[^0-9]*\([0-9]*\)-.*/\1/' infile | sort -r | head -n1 | cut -c 5-6
20