[Gllug] error in bash script to rename files in a directory

Tethys tet at accucard.com
Sun Dec 29 19:06:22 UTC 2002


"Liam Delahunty" writes:

>for i in `ls *.php3`; do mv $i `echo $i|tr 'php3' 'php'`; done
>
>with the intention of renaming all the files in a folder from .php3 to .php
>
>Instead, I now have *.phpp

tr doesn't behave in the way you expect. It translitterates characters,
rather than substituting strings. You should be using sed instead:

	for i in *.php3; do mv "$i" `echo "$i" | sed 's/php3$/php/'`; done

Note that you don't need to use ls to get the list of files. The shell's
globbing will do that for you. I've also quoted "$i" where it's referenced,
which ensures it'll still work if you have whitespace in your filenames.

Tet

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list