Mar 052013
 

I’ve discovered the fantastic yearbox plugin for Dokuwiki, which is superb for my electronic lab notes.  However, implementing it neatly required me to change the filename of every log entry from the last few years.  In my old Dokuwiki system, each log had a filename like

…/pages/log/2013/01/07.txt

I needed to keep the directory structure, but change each filename from two digits to the form “2013-01-07-ljr.txt”.

It turns out to be fairly straightforward with some clever bash tricks.  I changed into the log directory and used this command:

 for i in `find -name '??.txt'` ; do mv $i ${i%/*}/${i:2:4}-${i:7:2}-${i:10:2}-ljr.txt; done

Here the first part finds all files with names of the form “??.txt”.  Then each file is renamed with the “mv” command.  The ${i%/*} gives the file path with everything after the last “/” removed (ie it gives the subdirectory of the file).  Then the rest of the parentheses give extracted parts of the filepath string.

All files stayed in their own directories, and were renamed with additional information from their directory path.  Easy!  Well, not unless you know the details of string manipulation in bash – which I don’t.  I found enough information to do this after looking here and here.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)