Operating System - Linux
1748211 Members
5044 Online
108759 Solutions
New Discussion юеВ

Re: Renaming multiple files.

 
SOLVED
Go to solution
Joakim Brosten
Frequent Advisor

Renaming multiple files.

Hi,

I've about 1000 files named abc-100.200.def, abc-100.300.def, and so on...

I'd like them called abc-100200.def, abc-100300.def, etc instead. (The first "." deleted)

Do you know a nice way of doing this by one (or two) commands instead of 1000... ?

/Joakim
6 REPLIES 6
Steven E. Protter
Exalted Contributor

Re: Renaming multiple files.

Shalom,

How about a single while loop.


make a file list, call it list

while read -r filename
do
mv $filename $filenamenew
done < list

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
~sesh
Esteemed Contributor

Re: Renaming multiple files.

Do you have any commands in your mind? You could put those commands into a script & execute the script.

Better still, you can also get a prepared script (utility) to do the job for you. krename is one of them.

http://www.novell.com/products/linuxpackages/suselinux/krename.html

Hope this helps.
Ivan Ferreira
Honored Contributor
Solution

Re: Renaming multiple files.

for FILE in `ls abc*`
do
NEWNAME=`echo $FILE|awk -F "." '{ print $1$2"."$3}'
mv $FILE $NEWNAME
done
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Stuart Browne
Honored Contributor

Re: Renaming multiple files.

Well, this is the bash way:

for FILE in *.*.def
do
mv $FILE ${FILE/\./}
done

This uses bash's inbuilt parameter expansion stuff. This one does a replace on the first regex match. It matches the first '.', and replaces it with ''.
One long-haired git at your service...
Joakim Brosten
Frequent Advisor

Re: Renaming multiple files.

Thank you guys, this was what I was looking for. I also which you all a Merry Christmas and a Happy New Year.

From Sweden in white snow, /Joakim
Joakim Brosten
Frequent Advisor

Re: Renaming multiple files.

Perfect! Just what I was looking for.