PDA

View Full Version : Recursive Renaming


soup4you2
May 29th, 2003, 12:21
If anybody could help me with this i used to know this but seems i'm getting older and loosing some information here...

or it could be all the beer

but say i have a directory with files like

0001fl_.svm
0002fl_.svm
0003fl_.svm
0004fl_.svm
0005fl_.svm

what is a simple way to rename all these to just

0001.svm
0002.svm
0003.svm
0004.svm
0005.svm

Much appriciate any help here..

frisco
May 29th, 2003, 12:52
for oldfile in *fl_.svm; do
newfile=`echo $oldfile | sed 's/fl_//'`
mv $oldfile $newfile
done

soup4you2
May 29th, 2003, 13:11
thank you verry much... thank god for cygwin that just saved me hours of work... which i'll be dammed if i'm going to rename all those by hand...

frisco
June 6th, 2003, 13:28
I was clicking through www.daemonnews.org when i happened upon http://ezine.daemonnews.org/200306/answerman.html#q6 which refers to the mmv port. This utility would also do what you asked as well as a whole lot more (personally i still prefer writing a quick for loop - let's me customize to my heart's content).

soup4you2
June 6th, 2003, 16:52
i'll stick with the option you mentioned above it gives me more control over what i'm going to be doing.... the base of this little script i've been working on is now pretty much done... now it's time to start on the web front end for it... and here is where the fun begins... :)

bsdjunkie
June 6th, 2003, 17:24
perl solution: 8)


#!/usr/bin/perl -w
$op = shift or die "Usage: rename expr [files]\n";
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was,$_) unless $was eq $_;
}



example of using it:

rename 's/fl_//' *fl_svm
rename 'tr/A-Z/a-z/' *

soup4you2
June 6th, 2003, 19:46
i really need to learn perl someday.....


just too many things to learn.....