
nome do arquivo é estruturado como name$timestamp.extension
onde:
timestamp=`date "+%Y%m%d-%H%M%S"`
Portanto, se houver os seguintes arquivos em um diretório:
name161214-082211.gz
name161202-082211.gz
name161220-082211.gz
name161203-082211.gz
name161201-082211.gz
Com a execução do código/script da sua resposta, o valor 20
deverá ser armazenado na variávelhighest_day
highest_day = 20
Responder1
Sem usar carimbos de data e hora de arquivo, fica um pouco desajeitado, mas esta é uma maneira de fazer isso:
#!/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
Responder2
você pode fazer assim..
highest_day= $(for i in *.gz; do echo ${i:8:2}; done | sort -n | tail -1)
Responder3
Se a lista de arquivos estiver dentro de um arquivo, algo como:
$ cd dir
$ ls -1 * >infile
Este tubo faz o trabalho:
$ sed 's/[^0-9]*\([0-9]*\)-.*/\1/' infile | sort -r | head -n1 | cut -c 5-6
20