views:

145

answers:

3

I have thousands of files that I need to rename with the following format. 2008:09:18:17:45:48-alfanumeric-alfanumeric.wav the first part is a date.

Ex. 2008:09:18:17:45:48-703-s.wav

A want to rename it to:

20080918.174548.703.s.wav

Basically to remove the ':' and to make a more human readable format and easier to split.

I know that rename is what im looking for but the regex is not working. I'm typing:

rename 's/(\d)\:(\d)\:(\d)\:(\d)\:(\d)\:(\d)-(.?)-(.?).wav/$1$2$3.$4$5$6.$7.$8.wav/' ./*

+1  A: 

You need to consider multiplicities:

rename 's/(\d+)\:(\d+)\:(\d+)\:(\d+)\:(\d+)\:(\d+)-(.*)-(.?).wav/$1$2$3.$4$5$6.$7.$8.wav/' ./*
Martin v. Löwis
+2  A: 
rename 's/(\d+):(\d+):(\d+):(\d+):(\d+):(\d+)-([^-]+)-([^.]+).wav/$1$2$3.$4$5$6.$7.$8.wav/' ./*

should be used - in your version you always only match one digit. Plus, no need to escape the :.

Tim Pietzcker
A: 

If this is something you're only going to do once, I'd do it this way:

1) ls the files into a new file: ls datadir > mytmp 2) edit the mytmp with your favorite editor. 2a delete any lines that aren't data files you care about, like "." and ".." 2b edit each line into a shell command to rename the file. I like emacs, and would build a keyboard macro to: clip the file name, insert "mv ", insert the filename, insert space, insert the filename again, edit the inserted filename into the desired format. 2c save 3) run sh < mytmp

Done! Not elegant, tedious if your editor doesn't support macros or similar functionality, but guaranteed to work and git-er-done.

Berry