views:

370

answers:

1

Given a group of files with the following naming convention:

datetime_restofname.txt

an example of which would be:

200906290700_somewordorphrase.txt

how could I batch change the mtime of the files to match the date and time in the filename?

+5  A: 
$ for f in *.txt; do touch -t `echo $f | cut -f1 -d _` "$f"; done

This will set the file modtime to the date string before the underscore.

sunny256
Awesome! Thanks so much, sunny256! Sadly, I cannot upvote your excellent reply ("Vote Up requires 15 reputation"), but I deeply appreciate your taking the time to help.
Miles
By the way, it would be awesome if Stack Overflow offered some easy way to shoot you a tip (via PayPal, etc) for your kind help. If you have a PayPal account, please let me know the associated email address - I'd like to beam over some beer money if that's OK.
Miles
Sorry - forgot my email address: miles at tinyapps dot org
Miles
Thanks a lot for the offer, just glad to help. :) I'm also very thankful that we in fact have free software to play around with in the first place, so by sending a small donation to the Free Software Foundation, some organization who works against software patents or something along that line will help keeping it available for free.But it's a good idea, and SO has sort of implemented it with the bounty feature.
sunny256
I slightly modified the script you kindly provided to process files in subdirectories as well:for f in `find -name *.txt`; do touch -t `echo $f | cut -f1 -d _ | tr -d [:alpha:][:punct:]` "$f"; done
Miles
Thanks for the reply - I just donated $20 to the FSF in your name. Also, can someone please point me to a guide for formatting options on SO? Does HTML work? I noticed that underscores around a word italicize...
Miles
Edit: the donation was in your domain name. It would be swell if SO allowed editing of one's own comments...
Miles