Batch Renaming Files using RegEx
And a Command Line Tool Aptly Named ‘Rename’
You can bring a Linux utility called `rename` to OS X by:
brew install rename
My favorite usage pattern is as follows:
rename 's/<expression>/<replacement>/' * -n
- Both <expression> and <replacement> support RegEx
- The * is just to target all files in the directory
- The -n flag makes it a dry run
- If the results look right, run it again without the flag
Example
Imagine we have these files:
foo-1-foo.txt
foo-2-foo.txt
foo-3-foo.txt
To replace only the leading ‘foo’, use:
rename 's/^foo/bar/' *.txt
Result:
bar-1-foo.txt
bar-2-foo.txt
bar-3-foo.txt