Help with bash command to change lots of file names

Roger McCalman roger at runcircle.co.uk
Thu May 9 22:21:39 BST 2002


If using ksh then

for f in * ; do n=${f#CHAP}; n=${n%txt} ; echo mv $f ch${n}tex; done

Cheers, Roger

On Thu, May 09, 2002 at 11:25:42PM +0100, greadey wrote:
>     Hi peeps,
> 
> I have a directory with lots of files called CHAPN.TXT where n is a
> number from 1 to 14.  I want to turn them all into latex files and so
> for ease of editing in Vim I want to change them all to chN.tex.  I have
> been trying to write a bash command to do it with a for loop and sed and
> I basically have;
> 
> for f in *
> do
> NEW=`echo $f | sed -e 's/CHAP \( \W+ \. \) /ch \1 tex/ ' ` # I've put
> whitespace in for clarity
> #mv $f $NEW
> echo $NEW
> done
> 
> I have tried different quotes and I believe my regexp is correct but
> when I run this command or any variation I either get an error if I use
> all single quotes instead of backticks and single quotes or the command
> just prints all the original file names.
> 
> Can someone tell me where I am going wrong?
> 
> Thanks in advance
> 
> greadey.
> 
> 
> ------ FreeBSD UK Users' Group  -  Mailing List ------
> http://listserver.uk.freebsd.org/mailman/listinfo/freebsd-users




More information about the Ukfreebsd mailing list