tinyapps.org / blog


Batch convert month names to numbers in filenames #

This bash oneliner renames files formatted as Monthyyyy (January2003, March2011, etc) to mm-yyyy (01-2003, 03-2011, etc):
$ for i in * ; do mv $i $(echo $i | sed -e "s/January/01-/" -e "s/February/02-/" -e "s/March/03-/" -e "s/April/04-/" -e "s/May/05-/" -e "s/June/06-/" -e "s/July/07-/" -e "s/August/08-/" -e "s/September/09-/" -e "s/October/10-/" -e "s/November/11-/" -e "s/December/12-/") ; done

"Good, Watson, good! But not, if I may say so, quite good enough!" -- Sherlock Holmes in The Valley of Fear

Indeed, we'd be better off with a date format like yyyymm. Let's swap the position of the month and date, removing the hyphen at the same time:
$ for i in * ; do mv $i $(echo $i | sed "s/\(.*\)-\(.*\)/\2\1/") ; done

Now the files are named 200301, 201103, etc.

RenameWand also makes short work of file renaming operations, especially involving dates. The previous example becomes:
$ java -jar RenameWand.jar "<month>-<year>" "<year><month>"
Files can even be renamed into subdirectories based on dates in their names (e.g., "foo jun 2011" becomes 2011/jun/foo):
$ java -jar RenameWand.jar "<a> <month> <year>" "<year>/<month>/<a>"

/nix | May 26, 2011


Subscribe or visit the archives