Renaming many files in Mac OS X, batch processing

4,929

Solution 1

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

Solution 2

With Homebrew, a package manager for OS X:

brew install rename 

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

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
4,929

Related videos on Youtube

luigi
Author by

luigi

Updated on September 17, 2022

Comments

  • luigi
    luigi almost 2 years

    I'm developing an algorithm based on Skyline queries using C++, using a RTree for storing my data. The algorithm works fine if I process up to 5 points then if I try 6 points it gives a segmentation fault. Using gdb I have discovered that the problem is here:

    Program received signal SIGSEGV, Segmentation fault.
    std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string (
        this=0x7fffffffd7c0, __str=
        <error reading variable: Cannot access memory at address 0xffffffffffffffe8>)
        at /usr/src/debug/gcc-4.7.2-20120921/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.tcc:175
    175           __str.get_allocator())
    

    Can anybody help me to understand where the error is or ar

    • Paul R
      Paul R about 14 years
      The following article explains how to install rename on Mac OS X: macosxhints.com/article.php?story=20050630022203488
    • Paul R
      Paul R about 14 years
      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.
    • nilay
      nilay about 14 years
      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/…
    • Mitch Wheat
      Mitch Wheat over 11 years
      You have a bug in your code. If you want a more specific answer, post the relevant code.
    • luigi
      luigi over 11 years
      Mitch the code is quite long to post, more over is part of my final year project and my university might interpret that as cheating. Anyway I can post maybe a part of it
    • Mitch Wheat
      Mitch Wheat over 11 years
      Try using Valgrind....
    • Nicol Bolas
      Nicol Bolas over 11 years
      @clapclash: We are incapable of clairvoyance. We cannot know what your code is going just from a random error. We cannot magically intuit where some kind of problem may have occurred. If you want our help, you need to show us enough of the code that the problem is reproduced.
    • voidstate
      voidstate about 10 years
      I would vote up nilay's answer if it wasn't a comment...
  • quack quixote
    quack quixote about 14 years
    the perl rename is what this question shows as an example.
  • math
    math about 14 years
    but does that also allow regex replacement? This seems to be just some kind of enhanced shell globbing.
  • Chris Johnsen
    Chris Johnsen about 14 years
    @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.
  • juanpablo
    juanpablo about 14 years
    this package don't have the "rename" command.
  • lajuette
    lajuette about 14 years
    i didn't say that it does. qmv looks like it does the job.
  • holms
    holms over 13 years
    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
  • sapht
    sapht almost 12 years
    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
  • Nicol Bolas
    Nicol Bolas over 11 years
    "Literally all you need to do is to build your program" You also have to be running it on Linux (or MacOSX). Which is not always a thing you can do.
  • Ed S.
    Ed S. over 11 years
    @NicolBolas: True, but the OP is using GDB. I'd say it's a safe bet that this code is running under a *nix OS.
  • Rapptz
    Rapptz over 11 years
    @EdS. not really since MinGW comes with GDB.
  • John Zwinck
    John Zwinck over 11 years
    @NicolBolas: I guess, but given that the OP mentions /usr/src/debug/gcc-4.7.2-20120921/obj-x86_64-redhat-linux/x8‌​6_64-redhat-linux/li‌​bstdc++-v3/include/b‌​its/basic_string.tcc‌​:175...I think valgrind is a pretty safe bet.
  • Ed S.
    Ed S. over 11 years
    @Rapptz: True... still a safe bet. Far more people using it on *nix than there are MinGW users on Windows. Yeah, and that ^ above.
  • Hay
    Hay almost 11 years
    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.
  • user56reinstatemonica8
    user56reinstatemonica8 almost 10 years
    +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
  • Miquel Adrover
    Miquel Adrover almost 10 years
    You can use MacPorts package as well: port install p5-file-rename
  • mems
    mems over 8 years
    @MiquelAdrover: But with port you need to use rename-5.22 (where 22 is the installed version) instead of rename
  • Ako
    Ako about 5 years
    You can rename the symbolic link sudo mv /opt/local/bin/rename-XXX /opt/local/bin/rename