View Single Post
Old 4th January 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Default

I found J65nkos way of doing it interesting, but rather involved from my point of view; and several other posts also were interested. Being rather lazy, if I wanted to generate a string of mv commands to rename a group of files based on a simple pattern search & replace, at a unix shell, I would do it with a simple for loop and sed:

Code:
for FN in `ls *-*`; do 
        mv "$FN" `echo "$FN" | sed 's/-/_/g'`
done
whitespace and proper quoting added for legibility; since I would normally do it in one line at my $USER@`hostname`$ prompt. I also would admit the "quotes" around $FN if possible. Why the for loop? Because my mind thinks of the problem like this: for each file that I want to move, move the files with 'this' part of the name changed to 'this'.


It is not an efficient way of doing it, but it is fast enough when the number of files is not huge; if they were, I would probably have fun in C (or assembly if it was truly a huge set of files...). Without the limitation of using mv, I would probably use this if not in a bourne shell:

Code:
Terry@vectra$ perl -e 'map { $on=$_; s/-/_/; rename($on, $_) or warn $!; } <*>;'
Which says the same thing that my brain does. I usually use the move() function in File::Copy, but that's about 23 more keys to press in this simple case.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote