Saturday, January 28, 2012

BASH Batch Folder Underscore Batch Rename Script

I have a NAS where I backup all my years of photos, but unfortunately I haven't always been consistent with the way I named the folders.
I have always named them year month day and some descriptive name, but the delimiters are what have changed. Sometimes I used underscore "_" sometimes I used dashes "-".
So I spent some time on the interwebs tonight to figure out how to rename them all at once; I wanted to go from underscores to dashes. Here's what I came up with.

for F in *; do if [[ "$F" =  *_* ]]; then mv -v "$F" "`echo "$F" | tr '_' '-'`"; fi; done

This will check all the files or folders in a folder, check to see if it contains an "_" underscore, then rename that file to the same thing but replace the _ with a -.
You can modify this for your own purposes by changing the '_' after tr to 'whateveryouwanttoreplace' and the '-' with 'whateveryouwantthethingreplacedtobe', with all standard disclaimers; YMMV. 
Hope that helps.
If nothing else, now it is somewhere on the internet that I can search for it.

Labels: , , , , , , , , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home