3

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.

3
  • 3
    To the closers, and so that the OP can try and improve the question, what is unclear about that question? Please avoid closing a question as unclear without first leaving a comment as to what is unclear. Commented Jul 25, 2019 at 6:47
  • To inforce on @StéphaneChazelas: We do not know why the OP needs this, but at least archiving files is a typical use case of setting the mtime. touch is the command to go, which is widely misunderstood as being only useful to create an empty file. Commented Jul 25, 2019 at 7:03
  • 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
    – Rv555
    Commented Jul 25, 2019 at 13:45

1 Answer 1

4

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .