|
|
Install the Linux rename utility
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:
More than one regular expression can be applied serially to each file:
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.
Install the Linux rename utility
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.
Use this one...
One more time. It ate my backslashes.
2nd correction
The backslashes got eaten in the first comment.
On the command line you should type the command like this:
and it will print:
then to actually rename the files use the "-r" switch instead of "-t"
Install the Linux rename utility
when accessing the ren-regexp link on your page i get the following result:
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
Install the Linux rename utility
Fixed! I had a symlink problem that's now been resolved.
Code.
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.
Install the Linux rename utility
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 .
|
SearchFrom our Sponsor...Latest Mountain Lion HintsWhat's New:Hints1 new Hints in the last 24 hoursComments last 2 daysNo new commentsLinks last 2 weeksNo recent new linksWhat's New in the Forums?
Hints by TopicNews from Macworld
From Our Sponsors |
|
Copyright © 2014 IDG Consumer & SMB (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Created this page in 0.12 seconds |
|