I have a directory with folder structure as follows:
-- DATA -- ABD 1231345 -- 01-08-12 // date in mm-dd-yy format
-- 03-09-12
-- 06-11-12
-- DEF 4859480 -- 02-10-12
-- 05-10-12
-- 07-10-12
I would like to batch rename this DATA folder as follows
-- DATA -- ABD 1231345 -- 2012_01_08 // date in yyyy_mm_dd format with underscore
-- 2012_03_09
-- 2012_06_11
-- DEF 4859480 -- 2012_02_10
-- 2012_05_10
-- 2012_07_10
Do you have a suggestion on how to accomplish using command line on Mac OSX / unix?
You could use a
forloop andawk, parsing each file-name into your specified format and thenmvto rename the original to the new name:This can be executed in folder above
DATA. If you want to execute it directly inDATA, update the first loop to readfor dir in *; doinstead ofDATA/*. It tellsawkto use the-as the delimiter (instead of whitespace), and then reconstructs a string from “mm-dd-yy” to “20yy_mm_dd”.Using
pushdandpopdwill enable the script to change the current directory to each subdirectory insideDATA(pushd) and then, after moving all necessary files will change back to the original (popd). Doing this will save you a lot of parsing-effort trying to save directory paths / etc.