You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Unix "rename" Terminal command available in OS X?

Hello,

I have a couple of different cameras/phones that deliver their photos as

DSC00001.jpg
DSC00002.jpg
DSC00003.jpg
etc.

I want to be able to rename these files such that "DSC" is replaced by the camera or phone that took the shot. The "rename" command in Unix (and CMD prompt in XP) do exactly what I need. I just run

rename DSC*.jpg W580i*.jpg and all of file in the folder are renamed.

When I try and run the rename command in a Terminal in OS X, I get this error

mr-mbp:Pix_Xfer MR$ rename
-bash: rename: command not found

Is there a way to enable the rename command in OS X?

I found a nice script called "Replace Text in Item Names" that does what is needed however the command seems so much easier...

THx!

/MR

MBP 2.6, Mac OS X (10.5.5)

Posted on Oct 20, 2008 4:43 PM

Reply
12 replies

Mar 12, 2009 10:20 PM in response to Topher Kessler

If I were renaming a single file mv would be perfect, but I need to do many files at once. The practicum is that I have a set of files that have an underscore in the file name. I wrote a perl script that automatically generates LaTeX code to make a book for the pictures. However, the \includegraphics command in LaTeX does not care for underscores in filenames so I want to transliterate the underscores into something more LaTeX friendly, like dashes. With rename it's as simple as this:

rename _ - *.jpg

and all of my images that had underscores in their names are replaced with dashes. mv just isn't practical when I'm looking at hundreds (or more) of images. That's why I was so interested in the build of rename from linux -- I don't care what the source is, I just need the functionality.

Now, the rename.c file does have a shell script at the top that is a very decent substitute:

#!/bin/sh
if [ $# -le 2 ]; then
echo call: rename from to files; exit;
fi
FROM="$1"
TO="$2"
shift
shift
for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done

but the compiled C program does fix some potential issues, and it would also satisfy the purist in me that loves having a native compiled rendition of rename around.

Oct 20, 2008 5:41 PM in response to ReidRik_Von

rename is not a standard Unix command.

You could go over to <http://versiontracker.com> and search for rename. You will find Mac OS X utilities such as *A Better Finder Rename*, FileRenamer, Renamer4Mac, AispireRenamer, *File Renamer*, etc...

If you really want to do this with the terminal, you could try

#!/bin/sh
# DSC*.jpg W580i*.jpg
for file in DCS*.jpg
do
new=${file/DSC/W580i/}
mv $file $new
done

If you install Fink <http://Fink.sf.net>, there is gwenrename, krename, and ren that can be installed. Not sure how they work, but they might be options.

Mar 12, 2009 9:39 PM in response to ReidRik_Von

The easy solution to this is as "VK" and "nerowolfe" suggest....use "mv".

The "mv" command IS the standard unix "rename" command. Just reference the same location (filepath) with a new filename and you will have a rename. For example:

File location:
/Users/username/textfile.txt

To rename this to "textfile2.txt" you would type:

mv /Users/username/textfile.txt /Users/username/textfile2.txt

Alternately, if you are in the "username" folder, or in any folder containing the file to be renamed, you do not need to reference the full path...as such:

mv textfile.txt textfile2.txt

Some paranoid people may wish to create a hard link of the file before renaming it, and then deleting the old file, since this will create a copy without removing a reference to the file for the brief microsecond that it takes to "move" the file using the "mv" command.

Mar 12, 2009 11:25 PM in response to nwassob

I should preempt advice about performing the file renaming in perl. Certainly that is an option but I prefer separation of duties. My perl script is for generating LaTeX and I don't want it to tread into the realm of file management. There are also many uses for the rename utility -- stripping version numbers from VMS files sprngs to mind. I am working under the assumption that the contributors to rename.c understand file management and renaming issues much more than I do -- elsewise I'd stick with a perl or bash script.

Cheers,
-J.

Mar 14, 2009 3:34 PM in response to nwassob

I was working on some file operations today and I remembered that I actually once wrote a rename utility in perl for working on Windows. I brushed it off and took out all of the Windows wackiness (the command line doesn't expand blobs, etc.) and have another working solution. I'm not savvy enough to tell if it's better than the shell script from rename.c or not, but it works for what I need it for.

Cheers,
-J.


#!/usr/bin/perl -w
#
# Simulate the 'rename' file utility
#

use strict;

my $usage = "usage: $0 FROM_SPEC TO_SPEC filespec1 filespec2 filespec3\n";
$usage .= "Example: $0 _ - *.txt *.log this_file.c\n\n";

my $specFrom = shift || die "[Missing FROM_SPEC.]\n\n$usage";
my $specTo = shift || die "[Missing TO_SPEC.]\n\n$usage";
if ($#ARGV <= 0){
die "[Missing filespec(s).]\n\n$usage";
}

while($#ARGV >= 0){
my $fileSource = shift;
my $fileTarget = $fileSource;
$fileTarget =~ s/$specFrom/$specTo/g;
next if ("$fileSource" eq "$fileTarget");
print "\t[Processing $fileSource => $fileTarget]\n";
if ( -e $fileTarget ){
print "\t\t[$fileTarget aleady exists. Skipping.]\n";
}
else{
rename $fileSource, $fileTarget;
}
}

Unix "rename" Terminal command available in OS X?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.