Batch rename files on OSX/Unix

I had to rename a bunch of files in a directory by just changing a prefix. Perfect for the command line, no?
After a bit of googling (that exists as a verb now, doesn’t it? :-) ) I found this article:Batch File Rename By File Extension in Unix

All you need to do is iterate over all of the files in directory, and then rename by echoing the name & running sed on it, i.e.

for i in *; do mv $i `echo $i | sed 's/search/replace/'`; done

If you need to iterate over a tree of directories or only rename certain files, you can run this command

for f in `find . -type f -name "filename"`; do mv $f `echo $f | sed "s/search/replace/"`; done

If you are using git, you can also exchange mv with git mv.

As always: backups and/or version control are your friends!

About elduderino78

I'm a front-end developer working in the bowels of BBC Worldwide & focusing on POSH (HTML), CSS, JavaScript, Ajax, accessibility & usability
This entry was posted in Development, Tools and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

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=""> <strike> <strong> <pre lang="" line="" escaped="">