Rename files extensions

Hello !

i have a few files like ...

setup.001
setup.002
setup.003
setup.004
// to
setup.095

and i would like to rename those files to ...

setup.r01
setup.r02
setup.r03
setup.r04
// to
setup.r95

how can i do that ???

thanks in advance !! :wink:

hope this will guide you..

echo "setup.001"|sed 's/.0/.r/g'

wow thanks a lot !! it worked almost flawlessly !

i did ...

for file in setup.* ; do mv $file `echo $file | sed 's/.0/.r/g'` ; done

everything went right except i lost every "ten"

i got

setup.r01
//to
setup.r09
//but somehow i lost setup.r10

setup.r11
//to
setup.19
//but somehow i lost setup.r20

etc ...

i would need some regex to apply to tens

if i make

echo "setup.010"|sed 's/.0/.r/g'

i got ..

setup.r.r

unfortunately i don't know much about regex :frowning:

Simple, use the rename command as

rename s/\.[0-9]/.r/ *

i don't have the rename command in my shell :frowning:

sed 's/.0/.r/'
#!/bin/ksh
# or bash

# split filename using dot to the two variable
ls setup.* | while IFS="." read start end 
do

        oldname="$start.$end"
        # substring starting from second char and give length nothing = rest of data
        end="r${end:1}"
        newname="$start.$end"
        [ "$newname" = "$oldname" ] && continue
        # remove echo if looks good
        echo mv "$oldname" "$newname"
done

hey thanks all for your useful help ! i did the job perfectly :slight_smile:

you may close the thread if you want :b: