1

I'm looking for a way to batch rename almost 1,000 log files created by an Eggdrop bot. A few years ago, I had to setup my bot from scratch, and neglected to set the log format properly, so all of these files now have the format:

channelname.log.%d%b%Y (channelname.log.14Jan2014)

I want to rename all those files to match all my old log files, which are in the format of:

channelname.log.%Y%m%d (channelname.log.20140101)

I've already made the change in my eggdrop.conf file, but I would like to rename all the newer log files to match the format of the old ones.

This is on a Linux shell, so some sort of bash command would be ideal. Thanks!

fog
  • 43
  • 6

1 Answers1

2
find . -type f -name '*.log.*[^0-9-]*' -print0 | while read -d '' -r logfile; do
    mv "${logfile}"  "${logfile/.log.*/.log.`date -d ${logfile#*.log.} +%Y-%m-%d`}"
done

Assuming it's in a locale date knows how to handle.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • That command gave me output of "date: invalid date '%Y-%m-%d'" equal to the number of files. After it finished running, all that was left in the directory was one file name "channelname.log." . (of course, I backed up all the logs first before attempting). I'll see if I can figure out where it broke down. – fog Nov 18 '14 at 21:13
  • Got it, looks like it was a minor typo where you had "${i#*.log.}" it should have been "${logfile#*.log.}". Thanks! – fog Nov 18 '14 at 21:21
  • Ah, sorry, and luckily you backed up ;) Yeah, I usually test with a `for i in ...` construct, that was a leftover of me substituting a proper variable name, but apparently not fully, apologies! – Wrikken Nov 18 '14 at 21:46