Wednesday, May 24, 2017

File timestamp reset

I had a bunch of old MS Word docs that I wanted to convert to PDFs. I did it in LibreOffice, but the new PDFs were all new - I wanted them to have the same timestamps as the Word docs.

Also, just opening the Word docs in LibreOffice updated their modification times. So what I wanted to do was get the original creation time for each Word doc, and use that to set the creation and modification times for both the Word doc and its corresponding PDF file.

This does that:

IFS=     ; for x in *.doc ; do y=${x%.doc}.pdf ; d=`stat -c %W $x` ; touch -d @$d $x $y ; done

The character after IFS= is a tab, which you can insert by typing ctrl-v TAB.