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)?

Hennes
  • 64,768
  • 7
  • 111
  • 168
math
  • 2,633
  • 8
  • 31
  • 43
  • 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 '10 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: http://www.tuaw.com/2008/11/11/mac-automation-rename-multiple-files-efficiently/ – nilay Jun 20 '10 at 09:48
  • The following article explains how to install `rename` on Mac OS X: http://www.macosxhints.com/article.php?story=20050630022203488 – Paul R Jun 15 '10 at 11:05
  • I would vote up nilay's answer if it wasn't a comment... – voidstate Jun 22 '14 at 06:27

11 Answers11

52

With Homebrew, a package manager for OS X:

brew install rename 

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

slhck
  • 223,558
  • 70
  • 607
  • 592
juanpablo
  • 6,966
  • 11
  • 52
  • 72
  • 3
    You can use MacPorts package as well: port install p5-file-rename – madrover Aug 13 '14 at 07: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 '16 at 18:01
  • 1
    You can rename the symbolic link `sudo mv /opt/local/bin/rename-XXX /opt/local/bin/rename` – Ako May 13 '19 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.

ghoppe
  • 6,460
  • 22
  • 21
  • but does that also allow regex replacement? This seems to be just some kind of enhanced shell globbing. – math Jun 15 '10 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. – Chris Johnsen Jun 15 '10 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 '13 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 – user56reinstatemonica8 Jul 21 '14 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
math
  • 2,633
  • 8
  • 31
  • 43
  • 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 '11 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

lajuette
  • 4,672
  • 1
  • 21
  • 17
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.

Chris Johnsen
  • 39,401
  • 6
  • 111
  • 111
  • the [perl rename](http://git.debian.org/?p=perl/perl.git;a=blob_plain;f=debian/rename;hb=HEAD) is what this question shows as an example. – quack quixote Jun 15 '10 at 12:49
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.

Telemachus
  • 6,845
  • 1
  • 27
  • 33
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"' {} \;
Alex Nolasco
  • 233
  • 2
  • 6
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.

Peter Murray
  • 837
  • 5
  • 10
1

the equivalent command in renamer (cross-platform) is

$ renamer --regex --find 'old_pattern' --replace 'new_pattern' *glob
Lloyd
  • 121
  • 3
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

Pysis
  • 1,080
  • 2
  • 12
  • 25
0

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

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

Gordon Wells
  • 101
  • 1