Renaming multiple files in a folder

March 7, 2011 at 05:56:17
Specs: Macintosh
I have a bunch of files that are named by 20 different person´s names.
What I would like to do is to copy these files to a new destination and rename each file sequentially as sub1, sub2, ...sub20.

ex.
Bob_Tom.ima
joe_marie.ima
....


copy these to another new folder and rename them as

sub1.ima
sub2.ima

where sub1 is bob_tom, and sub2 is joe_marie


Is it possible to do this by scripting? (not using the renaming tool )

thank you in advance


See More: Renaming multiple files in a folder

Report •

#1
March 7, 2011 at 15:32:45
This korn shell script copies all files with a "ima" extention with a sub<no> to a destination directory - /tmp in this case. When it's working the way you want cange the copy command to move, mv:

#!/bin/ksh

dest="/tmp"

i=0
for file in *.ima
do
   ((i+=1))  # bump the filename counter by 1
   subname="sub${i}"
   newfile="${dest}/${subname}.ima" # build 
   cp "$file" "$newfile"
done


Report •

#2
March 8, 2011 at 03:34:57
Thanks a lot,

Would it be possible to apply the same script for renaming the folders as well ?
I´d just tried it didn´t seem to work.

eg. inside "test" folder

there are 4 folders

ana
bob
tom
jack


and then rename them to

sub1
sub2
sub3
sub4


Thank you !


Report •

#3
March 8, 2011 at 06:57:49
I am not a Mac guy, but in every other *nix I have worked on, the name of the folder can not have the same name of a file you are trying to put into that folder.

Maybe you could come up with a different algorithm.


Report •

Related Solutions

#4
March 8, 2011 at 08:28:46
<deleted by nails>

Report •

Ask Question