June 10, 2016

Change timestamp of file based on filename pattern

Suppose you have a list of files which each matches the same pattern include a date/timestamp: > name01_20160610-101002.csv

name01_20160611-091002.csv

name01_20160612-101302.csv

name01_20160613-141202.csv

name01_20160614-121002.csv

ls -1 |\
while read FILE 
do
   DATE="$(echo $FILE | perl -lpe 's/^.*20(......)-(....).*/\1\2/')
   echo touch $FILE -t $DATE
done