Utility to extract dates from filenames in Bash?

With a regex and Parameter Expansion:

for i in *; do
  [[ $i =~ [0-9]{8}|[0-9]{4}-[0-9]{2}-[0-9]{2} ]]
  x="${BASH_REMATCH[0]//-/}"
  y="${x:0:4}"
  m="${x:4:2}"
  d="${x:6:2}"
  echo "$y $m $d"
done | sort -n

Output:

2010 12 20
2016 01 01
2016 04 13
2017 01 11

Leave a Comment