Files – Change File Modified Date Using Date from File Name

datefilenamesfiles

Change last modified date of multiple files as per date mentioned in the file's name

Files are

04-01-1981 XXXXXYYYYZZZZ.xml
19-11-1982 XXXXXYYYYZZZZ.xml
25-12-1981 XXXXXYYYYZZZZ.xml

The reason it is needed : I am doing migration of some articles, here each file is an article to be published. And already available custom import utility use file's last modified date as article published date. Hope this helps and clear the reason for it.

Best Answer

POSIXly:

for file in ??-??-????' '*.xml; do
  date=${file%% *}
  year=${date##*-}
  day=${date%%-*}
  month=${date%-*}
  month=${month#*-}
  touch -d "$year-$month-$day 00:00:00" -- "$file"
done

With zsh, you can shorten it to:

for f (??-??-????' '*.xml(N))
  touch -d "$f[7,10]-$f[4,5]-$f[1,2] 00:00:00" -- $f
Related Question