59

I used to rename file in Linux via a rename command:

rename 's/old_pattern/new_pattern/g' *glob

Is there something similar in Mac OS X (Snow Leopard)?

4
  • The following article explains how to install rename on Mac OS X: macosxhints.com/article.php?story=20050630022203488
    – Paul R
    Jun 15, 2010 at 11:05
  • The backticks are not showing up in your comment - you should probably add this as an answer or edit your question to include your solution.
    – Paul R
    Jun 15, 2010 at 12:05
  • 1
    The best quick solution I've ever found has been using the built-in Automator. Check out this article for easy step by step help: tuaw.com/2008/11/11/…
    – nilay
    Jun 20, 2010 at 9:48
  • I would vote up nilay's answer if it wasn't a comment...
    – voidstate
    Jun 22, 2014 at 6:27

11 Answers 11

52

With Homebrew, a package manager for OS X:

brew install rename 

Then you can run the same rename commands as in Linux.

3
  • 3
    You can use MacPorts package as well: port install p5-file-rename
    – madrover
    Aug 13, 2014 at 7:19
  • 2
    @MiquelAdrover: But with port you need to use rename-5.22 (where 22 is the installed version) instead of rename
    – mems
    Jan 27, 2016 at 18:01
  • 1
    You can rename the symbolic link sudo mv /opt/local/bin/rename-XXX /opt/local/bin/rename
    – Ako
    May 13, 2019 at 21:11
29

Use the power of ZSH wisely (type zsh in the terminal if you are one of those poor souls who don't use it by default):

autoload zmv
zmv '(*).htm' '$1.html'

ZMV follows MMV syntax.

4
  • but does that also allow regex replacement? This seems to be just some kind of enhanced shell globbing.
    – math
    Jun 15, 2010 at 15:37
  • 4
    @brubelsabs: Yes, zmv can do regexp replacement. For files that match *user*.html, change the extension to .html and change all occurrences of rc to final: zmv '(*user*).htm' '${1//rc/final}.html' @ghoppe: I think the zmv example in your answer needs -w or parentheses around its wildcard. Jun 15, 2010 at 18:58
  • I really like this suggestion because you don't need to install anything extra on a Mac (like brew), but it does allow you to use the easy mmv like syntax.
    – Hay
    Sep 16, 2013 at 15:15
  • +1 Nice. Couple of differences from regexs I'm used to: 1) use * instead of .* to get all. *? seems to work like non-greedy .*?. 2) for me, ^ and $ for start and end of string seemed to cause it to match nothing Jul 21, 2014 at 14:51
21

Clumsy me:

for i in *.yourfiles; do mv "$i" "`echo $i | sed 's/old/new/g'`"; done 

And if you want to use it like I do often this way:

rename 's/old/new/' *.files

I recommend to use this litte script in ~/bin/rename:

#!/usr/bin/env zsh
SUBSEXPR=$1
shift
for i in $@; do mv $i `echo "$i" | sed $SUBSEXPR`; done
1
  • your script doesn't work for me "rename ACDC AC-DC ACDC*" result-> "ssed: can't read ACDC: No such file or directory" , I installed rename util from linux and now it works anwyay
    – holms
    Jan 30, 2011 at 16:59
5

You can try to install MacPorts and install the renameutils package:

renameutils @0.10.0 (sysutils)

renameutils is a set of programs designed to make renaming files faster and less cumbersome

3
  • this package don't have the "rename" command.
    – juanpablo
    Jun 20, 2010 at 21:38
  • 1
    i didn't say that it does. qmv looks like it does the job.
    – lajuette
    Jun 21, 2010 at 5:43
  • qmv is a great tool, while a bit too much work for simple regex renames, it's fantastic for intelligently naming and moving big numbers of arbitrary files
    – sapht
    Sep 21, 2012 at 16:06
4

There are various version of rename. It looks like you are looking for the Perl-based one.

One version of this utility comes with the File::Rename Perl module. You can install it with something like sudo cpan -i File::Rename.

Or, you could go with the rename from Debian's perl package. It is just a single file to download. Put it where ever you like and chmod it so that it is executable.


An alternative is the zmv tool that comes with zsh. It does not have the same syntax, but it does come with your OS and it can easily take care of many of the common cases.

1
2

On Macs I use Aristotle Pagaltzis's freely available rename, which like Debian's is Perl-based. You can get it here. Or visit here to read it first - always a good idea.

You need to place that somewhere in your $PATH and make it executable (chmod +x rename) and then you're good to go.

2

This shouldn't be difficult but apparently it is. Example, I want to rename all file's extension from aiff to aifc.

find . -iname "*.aiff" -exec bash -c 'mv "$0" "${0%\.aiff}.aifc"' {} \;
0
1

If you are looking for a GUI, try Name Mangler. It has a "preview" feature that shows what will happen if you follow through with the renaming.

1

the equivalent command in renamer (cross-platform) is

$ renamer --regex --find 'old_pattern' --replace 'new_pattern' *glob
0

If you like Sublime Text's multiselect you could use it with qmv:

qmv --editor="/usr/bin/s3 -w" files

0

I just went ahead and found my favorite I've seen referred to as perl-rename giving the rename command where help looks like this:

Usage: rename [-v] [-n] [-f] perlexpr [filenames]

That's how I know I got the one I like.

For Mac, even in Homebrew they have others where I just vaguely remember having trouble. So it's not the rename package, and it's not the nongnu renameutils package either.

Got it from here and just did the install like they mention: https://github.com/subogero/rename

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .