Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Install the Linux rename utility UNIX
After switching from Linux to OS X about a year ago, I was pleased to find all the CLI utilities I used on Linux were in OS X. That is, all except rename. There are various scripts which perform almost the same function, but nothing quite as easy to use. Finally I got fed up and decided to try and build the Linux rename utility for OS X.

The rest of this hint assumes you have a Gnu build environment and know how to use it. The source for rename is contained in the util-linux package. Download util-linux-2.12q.tar.gz, extract it, and switch to the util-linux-2.12q directory.

Run ./configure.

If you attempt to run make on the entire package, it will fail. However, you don't need to build the entire package to build rename. Switch to the misc-utils directory and run make rename. When it's done, copy rename to /usr/bin and rename.1 to /usr/share/man/man1.

Run man rename for usage instructions.
    •    
  • Currently 3.50 / 5
  You rated: 4 / 5 (6 votes cast)
 
[23,222 views]  

Install the Linux rename utility | 22 comments | Create New Account
Click here to return to the 'Install the Linux rename utility' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Install the Linux rename utility
Authored by: matsur on Jul 12, '05 09:53:52AM

Putting the binary in /usr/bin is a bad idea. That folder is not guarenteed to be preserved across updates, while /usr/local/bin is less likely to be wiped out. Same goes for the man page.



[ Reply to This | # ]
Install the Linux rename utility
Authored by: EddEdmondson on Jul 12, '05 11:47:50AM
There is no single 'Linux rename utility' unfortunately - so be careful that this is the one you expect. The one installed in Debian is a perl script with the syntax
rename [-v] [-n] [-f] perlexpr [files]
The one installed by RedHat (or whatever it's become nowadays) is different - it's a C program, and is the same one referred to in the hint. It's syntax is
rename from to files


[ Reply to This | # ]
Install the Linux rename utility
Authored by: cellocgw on Jul 12, '05 12:53:08PM

OK, dumb question: do any of these utilities handle the resource forks or does the user have to "manually" identify them?



[ Reply to This | # ]
Install the Linux rename utility
Authored by: chris_on_hints on Jul 12, '05 01:24:12PM

not a dumb question.

it will probably not handle resource forks etc, so might end up disconnecting a file from its resource when you rename it, or it might work perfectly well.

i guess you would have to try it to see...



[ Reply to This | # ]
Install the Linux rename utility
Authored by: peterneillewis on Jul 12, '05 10:29:58PM

The unix commands that operate on files (mv and rm for example) have always handled resource forks under Mac OS X. They operate on directory entires, and despite appearances, there is only one directory entry for a Mac OS X file, whether iot has a data fork, resource fork or both.

Tiger (10.4) adds support for resource forks to the standard unix commands that operate on file data (cp for example).



[ Reply to This | # ]
Install the Linux rename utility
Authored by: jonn8n on Jul 12, '05 01:23:30PM
For those that like their rename utility to have a GUI, check out the shareware app Name those Files! It features GREP search and replace, advanced numbering, inclusion of the parent folder name, date insertions, and more.

Jon

[ Reply to This | # ]
Install the Linux rename utility
Authored by: GlowingApple on Jul 12, '05 02:29:10PM

Automator also does a nice job of incrementing file names and doing word and character replacements on file names.

Of course AppleScript can also be very useful here, but that usually requires a lot more programming knowledge and tends to be fairly specific to the naming that needs to be done.

---
Jayson --When Microsoft asks you, "Where do you want to go today?" tell them "Apple."



[ Reply to This | # ]
Install the Linux rename utility
Authored by: wgscott on Jul 12, '05 01:29:31PM

Another option, one that comes with OS X, is to use the z-shell:

1. Fire up the zshell by typing zsh

2. Define this alias:

mmv='noglob zmv -W'

3. Issue the command:

autoload -U zmv


(You can put the commands for 2 and 3 into your .zshrc file).


Then you can issue the command


mmv photo_*.tiff photo_*.tif

and it will change all files of the form

photo_anything.tiff to the form photo_anything.tif

The nice thing is you don't have to install anything. You can call it rename instead of mmv, or whatever else suits you.


zsh has many more really nice features like this.



[ Reply to This | # ]
bundled example AppleScript
Authored by: kahuna on Jul 12, '05 02:55:01PM

"Replace Text in Item Names.scpt" does the same thing too.



[ Reply to This | # ]
Install the Linux rename utility
Authored by: forman on Jul 12, '05 04:39:38PM

I wrote an extremely useful Perl script to rename many files simultaneously using Perl regular expressions as opposed to shell globs. For instance, to rename all files ending in "tiff" to "tif" (as shown above), one would execute:
ren-regexp 's/tiff$/tif/' *tiff
More than one regular expression can be applied serially to each file:
ren-regexp 's/tiff$/tif/' "s/^DCS/Vacation-/i" "s/_/-/g" DCS*tiff
It's vastly more powerful than a glob-based rename function; provides 100% compatibility with Perl regular expressions; prints output (which can be suppressed) showing each regular expression as it's applied; and has a test flag ("-t") to see the result without actually performing the operation. Advanced command-line users and sysadmins will find this particularly useful. I use it myself several times a day.

You can download ren-regexp here. Let me know what you think.

Michael.

[ Reply to This | # ]
Install the Linux rename utility
Authored by: bimtob on Jul 12, '05 07:35:24PM
Here's one that I wrote that has fewer options, but I find it very useful. It accepts full perl regular expressions as arguments.
#!/usr/bin/perl
# remv: Regular Expression mv
# Usage: remv [-t | -r] filter "regular expression"
# -t is to test regex
# -r is to perform the mv operation
# filter is an argument to ls (ex. *.pl)
# regular expression is put in quotes. $s are backslash \escaped
#
# Example: remv -t \*.jpg "s/oldname(\d)(.jpg)/newname\$1\$2/"
#

$mode = shift;
$filter = shift;
$regex = shift;
$str = "\$file =~ $regex";
chomp($filter);
chomp($regex);

@filelist = `ls $filter`;

if ($mode eq "-r") {
	foreach $file (@filelist) {
		chomp($file);
		$oldname = $file;
		eval $str;
		print "renaming: $oldname to $file\n";
		`mv $oldname $file`;
	}
}
elsif ($mode eq "-t") {
	foreach $file (@filelist) {
		chomp($file);
		$oldname = $file;
		eval $str;
		print "$oldname -> $file\n";
	}
}
else { print "-t\ttest regex\n-r\trename files\n";}
So something like:
remv -t \*.jpg "s/oldname(\d)(.jpg)/newname\$1\$2/"
will rename "oldname1.jpg" and "oldname2.jpg" to "newname1.jpg" and "newname2.jpg" repectively.

[ Reply to This | # ]
Use this one...
Authored by: bimtob on Jul 12, '05 07:40:46PM
One more time. It ate my backslashes.

#!/usr/bin/perl
# remv: Regular Expression mv
# Usage: remv [-t | -r] filter "regular expression"
# -t is to test regex
# -r is to perform the mv operation
# filter is an argument to ls (ex. *.pl)
# regular expression is put in quotes. $s are backslash \escaped
#
# Example: remv -t \*.jpg "s/oldname(\d)(.jpg)/newname\$1\$2/"
#

$mode = shift;
$filter = shift;
$regex = shift;
$str = "\$file =~ $regex";
chomp($filter);
chomp($regex);

@filelist = `ls $filter`;

if ($mode eq "-r") {
	foreach $file (@filelist) {
		chomp($file);
		$oldname = $file;
		eval $str;
		print "renaming: $oldname to $file\n";
		`mv $oldname $file`;
	}
}
elsif ($mode eq "-t") {
	foreach $file (@filelist) {
		chomp($file);
		$oldname = $file;
		eval $str;
		print "$oldname -> $file\n";
	}
}
else { print "-t\ttest regex\n-r\trename files\n";}
[\code]


[ Reply to This | # ]
2nd correction
Authored by: bimtob on Jul 12, '05 08:06:23PM
The backslashes got eaten in the first comment. On the command line you should type the command like this:
rgxmv -t "*.jpg" "s/oldname(\d)(.jpg)/newname\$1\$2/"
and it will print:
oldname1.jpg -> newname1.jpg
oldname2.jpg -> newname2.jpg
then to actually rename the files use the "-r" switch instead of "-t"

[ Reply to This | # ]
Install the Linux rename utility
Authored by: caesurae on Jul 12, '05 07:51:03PM
when accessing the ren-regexp link on your page i get the following result:

    Forbidden
    You don't have permission to access /perl/script/ren-regexp on this server.
    Apache/1.3.33 Server at www.Michael-Forman.com Port 80



[ Reply to This | # ]

Install the Linux rename utility
Authored by: forman on Jul 13, '05 03:37:41AM

Fixed! I had a symlink problem that's now been resolved.

Michael.



[ Reply to This | # ]
Code.
Authored by: forman on Jul 13, '05 03:43:34AM
I thought I'd include the code here in the event of a future symlink error. ;)

The latest version of the code can be found here.

Michael.

#!/usr/bin/perl
## $Id: ren-regexp,v 1.6 2003/12/28 08:01:25 forman Exp forman $ 
##
## Author:        Michael Forman <Michael.Forman@Colorado.EDU>
## URL:           http://www.Michael-Forman.com
## Creation Date:  Date: 2000/07/31 02:47:42 GMT
## Last Revision: $Date: 2003/12/28 08:01:25 $ 
## Revision:      $Revision: 1.6 $
##
## Copyright (C) 1999, 2000 Michael Forman.  All rights reserved.
## This program is free software; you can redistribute it
## and/or modify it under the same terms as Perl itself.
## Please see the Perl Artistic License.
##
## Category: File manipulation
## Rating:   5/5
##
## <meta name="title" content="ren-regexp">
## <meta name="description" content="Rename files using regular expressions.">
## <meta name="abstract" content="Rename files using regular expressions.">
## <meta name="keywords" content="Michael Forman perl script ren-regexp CPAN">
#

##
## TODO:
## Handle case insensitive filesystems
##
#
use Getopt::Long;
use File::Copy;

#---------------------------------------------------------------------------
GetOptions(
  "h|help"		=> \$help,
  "d|debug" 		=> \$debug,
  "t|test" 		=> \$test,
  "v|version" 		=> \$vers,
  #---------------------------------
  "c|color"		=> \$color,
  "u|underline"		=> \$under,
  "g|global"		=> \$global,
  "i|insensitive"	=> \$insen,
  "f|force"		=> \$force,
  "q|quiet" 		=> \$quiet,
);
#---------------------------------------------------------------------------
if($help){ exec("perldoc $0"); }
if($vers){ version(); }
if($#ARGV == -1){ usage(); }
#---------------------------------------------------------------------------
## I turned on color and underlining by default.
## You can comment these out or override them
## using "--nocolor" and "--nounderline".
#
if($color eq ""){ $color = 1; }
if($under eq ""){ $under = 1; }
#---------------------------------------------------------------------------

if($under && $color){
  $CG = ansi_color("g","u");
  $CR = ansi_color("r","u");
  $CO = ansi_color("y","u");
  $CL = ansi_color("d","n");
}elsif($under){
  $CG = ansi_color("d","u");
  $CR = ansi_color("d","u");
  $CO = ansi_color("d","u");
  $CL = ansi_color("d","n");
}elsif($color){
  $CG = ansi_color("g","n");
  $CR = ansi_color("r","n");
  $CO = ansi_color("y","n");
  $CL = ansi_color("d","n");
}

##
## Store regular expressions
#
foreach $re (@ARGV){
  ## Take the following forms:
  ##   s/a/b/[ig]	
  ##    /a/b/[ig]	
  ##     a/b/[ig]	
  ##     a/b            
  #
  if(-e $re){ last; }		# if file, end of regexps
  chomp($re);

  $re =~ s/^s?\///;
  ($pa,$tx,$md) = split(/\//,$re); 
  push(@re,$re);

  if($global && (!($md =~ /g/))){ $md.= "g"; }
  if($insen  && (!($md =~ /i/))){ $md.= "i"; }

  $pa{$re} = $pa;	# regexp
  $tx{$re} = $tx;	# replacement
  $md{$re} = $md;	# modifier
  $dl{$re} = $dl; 	# delimiter
}

if($#re == -1){ usage(); }

$nsp = int(($#re+1)/10)+2;
$spc = " " x $nsp;

foreach $re (@re){
  shift(@ARGV);
}


##
## Apply substitutions
#
while($ofile = shift(@ARGV)){
  $nfile = $ofile;
  $pfile = $ofile;
  $nre = 0;
  foreach $re (@re){
    $pa = $pa{$re};
    $tx = $tx{$re};
    $md = $md{$re};
    $dl = $dl{$re};

    eval("\$nfile =~ s/$pa/$tx/$md");
    if($pfile ne $nfile){ 
      if($ofile[$#ofile] ne $ofile){ push(@ofile, $ofile); }
      ##
      ## A regexp worked!
      ## Store begining end and regexp
      #
      $pf{$ofile,$re} = $pfile;
      $nf{$ofile,$re} = $nfile;
      $rn{$ofile,$re} = $nre;
      $re{$ofile}    .= "$re"." __rEgExP__ ";
      $pfile = $nfile;
    }
    $nre++;
  }
  $nf{$ofile} = $nfile;
}


##
## Output and move
#
foreach $ofile (@ofile){
  ##
  ## Output
  #
  $prc = "";
  @re = split(/ __rEgExP__ /,$re{$ofile});

  print("\n");

  for($i=0;$i<=$#re;$i++){
    $re = @re[$i];
    if(!$quiet){
      $pa = $pa{$re};		# pattern
      $tx = $tx{$re};		# trans
      $md = $md{$re};		# mod

      $pp = $pa{$re[$i+1]};	# trans
      $m2 = $md{$re[$i+1]};	# trans

      $of = $ofile;		# old file name
      $pf = $pf{$ofile,$re};	# new file name
      $nf = $nf{$ofile,$re};	# new file name
      $rn = $rn{$ofile,$re};	# regexp number

      if($color||$under){ 
        ($pf, $nf) = color_regexp3($pf, $pa, $tx, $md, $nf, $pp, "", $m2);
      }

      $prn = $rn + 1;
      if($nsp > 2){ $prn =~ s/^(\d)$/0$1/; $spc = "   "; }
      if($i==0){ print("$spc$pf\n"); }
      print("$prn $nf\n"); 
    }
  }
  ##
  ## Move
  #
  $nfile = $nf{$ofile};
  if(!$test){ 
    if((!-e $nfile)||($force)){ move("$ofile","$nfile"); }
    else{ print(STDERR "Cannot move \"$nfile\": file exists.\n"); }
  }else{
    #print("mv \"$ofile\" \"$nfile\"\n"); 
  }
}

if(!$quiet){
  print("\n");
}


#---------------------------------------------------------------------------
 sub color_regexp3{ my($p1, $r1, $t1, $m1, $p2, $r2, $t2, $m2) = @_;
  ##
  ## Uses three colors to highlight changes in names.
  #
  my($c1, $c2, $c3);
  my(@c3, @r1);
  my($str,$pr);
  my($i);

  $c1 = restcol($p1,$r1,  $t1,$m1,1);
  $c2 = restcol($p2,$r2,"\$1",$m2,2);
  $c3 = stradd($c1, $c2);

  eval("\$p1 =~ s/($r1)/$CR\$1$CL/$m1;"); 

  if($debug){
    print("  $c1  s/$r1/$t1/$m1\n");
    print(" +$c2  /$r2/$m2\n");
    print(" =$c3  sum\n");
  }

  @c3 = split(//, $c3);
  @p2 = split(//, $p2);

  for($i=0;$i<=$#c3;$i++){
    if($c3[$i] != $pr){
      if($c3[$i] == 0){
        $str.= "$CL$p2[$i]";
      }elsif($c3[$i] == 1){
        $str.= "$CL$CG$p2[$i]";
      }elsif($c3[$i] == 2){
        $str.= "$CL$CR$p2[$i]";
      }elsif($c3[$i] == 3){
        $str.= "$CL$CO$p2[$i]";
      }
    }else{
      $str.= "$p2[$i]";
    }
    $pr = $c3[$i];
  }
  $p2 = $str."$CL";

  return($p1,$p2);
}
#---------------------------------------------------------------------------
sub restcol { my($st,$re,$tx,$md,$k) = @_;
  my(@st);
  my($i,$n,$col);

  eval("\$st =~ s/($re)/ __ReGeXp__ $tx __ReGeXp__ /$md;");

  @st = split(/ __ReGeXp__ /,$st);

  for($i=0;$i<=$#st;$i++){
    $n = $i%2*$k;
    $col.= $n x length($st[$i]);
  }

  return($col);
}
#---------------------------------------------------------------------------
sub stradd { my($a, $b) = @_;
  my(@a,@b,@c);
  my($i,$c);
  my($sa) = 1;
  my($sb) = 1;

  if($a =~ s/^-//){ $sa*= -1; }
  if($b =~ s/^-//){ $sb*= -1; }

  @a = split(//,$a);
  @b = split(//,$b);

  for($i=0;$i<=$#a;$i++){
    $c[$i] = $sa * $a[$i] + $sb * $b[$i];
    if($c[$i] < 0){ $c[$i] = 0; }
  }
  $c = join('', @c);

  return($c);
}
#---------------------------------------------------------------------------
sub ansi_color { my($color,$attrb) = @_;
  my($ansi);
  ##
  ## Colors
  ##
  ## Attribute codes:
  ## 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
  ##
  ## Text color codes:
  ## 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
  ##
  ## Background color codes:
  ## 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
  #
  $ansi = "\e[";

     if($color =~ /d(efault)?/i){   $ansi.= "00";  }
  elsif($color =~ /r(ed)?/i){       $ansi.= "31";  }
  elsif($color =~ /g(reen)?/i){     $ansi.= "32";  }
  elsif($color =~ /y(ellow)?/i){    $ansi.= "33";  }
  elsif($color =~ /b(lue)?/i){      $ansi.= "34";  }
  elsif($color =~ /m(agenta)?/i){   $ansi.= "35";  }
  elsif($color =~ /c(yan)?/i){      $ansi.= "36";  }
  elsif($color =~ /w(hite)?/i){     $ansi.= "37";  }

     if($attrb =~ /n(ormal)?/i){    $ansi.= ";00"; }
  elsif($attrb =~ /b(old)?/i){      $ansi.= ";01"; }
  elsif($attrb =~ /u(nderline)?/i){ $ansi.= ";04"; }

  $ansi.= "m";

  return($ansi);
}
#---------------------------------------------------------------------------
sub usage {
  print("\n");
  print("usage: ren-regexp [regexp ...] [file ...]\n");
  print("\n");
  exit;
}

#---------------------------------------------------------------------------
sub version {
  my($date) = "\$Date: 2003/12/28 08:01:25 $_";
  my($rvsn) = "\$Revision: 1.6 $_";
  my($rcsd) = "\$Id: ren-regexp,v 1.6 2003/12/28 08:01:25 forman Exp forman $_";

  $date =~ s/(.*: +)(.*?)(\s*$)/$2/g;
  $rvsn =~ s/(.*: +)(.*?)(\s*$)/$2/g;
  $rcsd =~ s/(.*: +)(.*?)(\s*$)/$2/g;

  print <<EOF;

Program:       ren-regexp v$rvsn

Author:        Michael Forman <Michael.Forman\@Colorado.EDU>
URL:           http://www.Michael-Forman.com
Creation Date: 2000/07/31 02:47:42 GMT
Last Revision: $date GMT
Revision:      $rcsd

Copyright (C) 2002, 2003 Michael Forman.  All rights reserved.
This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.

EOF
  exit;
}
#---------------------------------------------------------------------------
##
## Use "perldoc ren-regexp" to read the man page below.
#
__END__

=head1 NAME

B<ren-regexp> - Rename files by the application of regular expressions

=head1 SYNOPSIS 

B<ren-regexp> 
S<[ B<-dhtv> ]> 
S<[ B<-cfgiqu> ]> 
S<[I<regexp ...>]> 
S<[I<file ...>]>


=head1 DESCRIPTION

B<ren-regexp> applies one or more regular expressions 
to a list of file names.
This provides a method of applying common modifications
to many files that would otherwise require repetitive,
atomic file operations.


=head1 OPTIONS

B<-c --color>

B<-u --underline>

The "B<--color>" and "B<--underline>" options can be used
together or separately to highlight changes in the filename
as the regular expression are applied.

B<-d --debug>

Print additional information useful for debugging.

B<-f --force>

If the new file exists, this will force an overwrite.

B<-g --global>

Apply all regular expressions globally to a filename.
This is equivalent to appending a "I<g>" to the end of each
regular expression as in "I<s/regexp/string/B<g>>".

B<-h --help>

Prints this information.

B<-i --insensitive>

Apply all regular epxression without sensitivity to case.
This is equivalent to appending an "I<i>" to the end of each
regular expression as in "I<s/regexp/string/B<i>>".

B<-q --quiet>

B<ren-regexp> is rather verbose for a unix program.
Consider this a feature to prevent data loss.
To keep things quiet, use this option.

B<-t --test>

Test the application of the regular expressions without renaming the files.
This is highly recommended to prevent the loss of data.


=head1 EXAMPLE

The following example shows standard usage.
The regular expression, "I<s/.mp3/ of 3.mp3/>", is applied to
the three files resulting in files matching the pattern
"I<PI-01 of 3.mp3>".

B<ren-regexp> B<"s/.mp3/ of 3.mp3/"> B<PI-01.mp3 PI-02.mp3 PI-03.mp3>


The following examples all have the same result.
Note that the initial "I<s/>" are both optional
with the final "I</userdefined>" option if there is no modifier.

B<ren-regexp> B<"s/A/B/i"> B<*>

B<ren-regexp> B<-i> B<"s/A/B/"> B<*>

B<ren-regexp> B<"A/B/i"> B<*>

B<ren-regexp> B<-i> B<"A/B"> B<*>

The following example shows three regular expressions
applied in turn on a filename.  
The file progresses from the original of "I<ABCD.txt>"
to "I<abCD.txt>", "I<ABcD.txt>", and finally "I<AcDB.txt>".
The single quote is necessary to prevent the shell from
expanding the regular expression variables, "I<$1>" and "I<$2>".

B<ren-regexp> B<"AB/ab"> B<"abC/ABc"> B<'(B)(cD)/$2$1'> ABCD.txt 

=head1 BUGS

The color ouput doesn't like regular expressions variables (i.e., "I<$1>").

=head1 SEE ALSO

mv    

=head1 AUTHOR AND COPYRIGHT

Michael Forman <Michael.Forman@Colorado.EDU> http://www.Michael-Forman.com

Copyright (C) 2000, 2001, 2002 Michael Forman.  All rights reserved.
This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.
Please see the Perl Artistic License.

=head1 VERSION

Current Revision:  $Revision: 1.6 $
Last Modification: $Date: 2003/12/28 08:01:25 $

=pod SCRIPT CATEGORIES
UNIX/System_administration

=pod OSNAMES
Any



[ Reply to This | # ]
Install the Linux rename utility
Authored by: juanfal on Jul 14, '05 10:38:16AM
An easier of reading, powerful and short is the next one (manage Tcl regexps and admits -recursion, -capitularization....)
#!/usr/bin/env tclsh
# Juan Falgueras, 2004-12-28
#   -- añado capitularizar.. pero falla
# «» regexp -indices {[:alpha:]} 01-pepe.mp3 que
# 1
# «» set que
# 3 3
# «» regexp -indices {[:alpha:]} ./01-pepe.mp3 que
# 1
# «» set que
# 5 5
# Además file rename se niega a cambiar a minúsculas


set Capitularizar 0
set capitularizar 0
set minusculizar 0
set recursivo 0
set pat ""
set reemp ""


if {$argc == 3 && [lindex $argv 0] == "-r"} {
    set recursivo 1
    set pat [lindex $argv 1]
    set reemp [lindex $argv 2]
} elseif {$argc == 1 && [lindex $argv 0] == "-C"} {
    set Capitularizar 1
} elseif {$argc == 1 && [lindex $argv 0] == "-c"} {
    set capitularizar 1
} elseif {$argc == 1 && [lindex $argv 0] == "-m"} {
    set minusculizar 1
} elseif {$argc != 2} {
    puts stderr "USAR: $argv0 '(p)at' '..\1..'"
    return 1
} else {
    set pat [lindex $argv 0]
    set reemp [lindex $argv 1]
}


proc procDir {d} {
    global recursivo pat reemp capitularizar Capitularizar minusculizar

    foreach f [glob "$d/*"] {
        if {$recursivo && [file isdirectory $f]} {
            procDir $f
        }
        if {$Capitularizar} {
            set fichnombre [file tail $f]
            regexp -indices {[:alpha:]} $fichnombre indiceLetra
            set nunom [string totitle $fichnombre [lindex $indiceLetra 0] end]
            file rename -force "[file tail $f]" "$nunom"
        } elseif {$minusculizar} {
            eval exec mv \"$f\" \"[file dirname $f]/[string tolower [file tail $f]]\"
        } elseif [regexp -- "$pat" "[file tail $f]"] {
            regsub -all -- "$pat" "[file tail $f]" "$reemp" nunom
            puts "'$f' --> '[file dirname $f]/$nunom'"
            file rename "$f" "[file dirname $f]/$nunom"
        } 
    }
    return 0
}

procDir .


[ Reply to This | # ]
Install the Linux rename utility
Authored by: lucite on Jul 12, '05 06:52:22PM

Can anyone post the man page for this or a URL to the maintainer page - I'd rather see it's features before installing it.

Thx -



[ Reply to This | # ]
Install the Linux rename utility
Authored by: KenaiTheMacFan on Jul 12, '05 07:39:17PM
Why not use mv??

---
Ian

[ Reply to This | # ]

Install the Linux rename utility
Authored by: wgscott on Jul 13, '05 12:01:07AM

Try it. You will see it won't work for batch-renaming.



[ Reply to This | # ]
How a Rename Program Saves Time
Authored by: forman on Jul 13, '05 04:04:38AM
The reason is that one shouldn't just use "mv" is that it only operates on one file at a time. If you need to rename multiple files in a similar way a rename program or script can save a lot of time. For instance, I was taking screenshots tonight using Apple Remote Desktop. I had to add on a numerical suffix to each snapshot as I took it and later I wanted to clean up the names. What I had was:
Apple Remote Desktop1.tif
Apple Remote Desktop2.tif
...
Apple Remote Desktop14.tif
Apple Remote Desktop15.tif
What I did was the following:
ren-regexp "s/Apple Remote Desktop/2005-07-12-RD-/" *tif
ren-regexp "s/RD-/RD-0/" *tif
ren-regexp 's/RD-\d(\d\d)/RD-$1/' *tif
Actually, my program ren-regexp can take all those regular expressions on a single line:
ren-regexp "s/Apple Remote Desktop/2005-07-12-RD-/" "s/RD-/RD-0/" 's/RD-\d(\d\d)/RD-$1/' *tif
What I was left with was:
2005-07-12-RD-01.tif
2005-07-12-RD-02.tif
...
2005-07-12-RD-14.tif
2005-07-12-RD-15.tif
I typed 94 characters with the above command and it saved me 30 mouse clicks and 270 characters and that's just for 15 files. Imagine how much effort you could save if you had hundreds of images that you need to rename.

Michael.

[ Reply to This | # ]
Install the Linux rename utility
Authored by: tallest on May 23, '08 12:19:47PM

I had some trouble getting configure to run. It would fail with the following error:

checking for blkid_known_fstype in -lblkid... no
configure: error: blkid or volume_id is needed to build util-linux-ng.

Rather than trying to get the configure script working, I built rename manually. First, I had to add the line

#define PACKAGE_STRING "util-linux-ng 2.13.1.1"

to the beginning of the file 'misc-utils/rename.c'. Then I was able to compile it (from the misc-utils directory) with the following command:

gcc -I../include/ rename.c -o rename

This creates an executable that seems to work just fine.



[ Reply to This | # ]