sed to rename files in a folder - please help with script

Hello,

I am new to shell scripting and stuck on renaming files in a folder. The files have the format

chp01_00001.wav
chp01_00002.wav
....
chp02_00001.wav
chp02_00002.wav
....

but I want them to have the following names:

chp_bloomy_00001.wav
chp_bloomy_00002.wav
chp_bloomy_00003.wav
chp_bloomy_00004.wav

I wrote a loop to delete the numbering from chp and insert "bloomy":

#!/bin/bash
FILES="home/bloomy/path/wav/*"

for f in $FILES
do
    
       sed ''$f's/./bloomy/6' $FILES
       sed ''$f's/chp*/chp/g' $FILES
    
done

But the loop doesn't work and I get the following error message:

sed: -e expression #1, char 2: extra characters after command

I also don't know how to remove the numbering and replace it with consecutive numbering at the end of the file names.

Can someone help please?

Cheers,

Bloomy

ksh
j=0
for i in chp*.wav
do
     let j+=1
     echo mv $i ${i%??_*}_bloomy_$(printf "%05d\n" $j).wav
done

Example if i initially have :

# ls -1 *.wav
chp01_00001.wav
chp01_00002.wav
chp02_00001.wav
chp02_00002.wav

If i run the following commands :

ksh
j=0
for i in chp*.wav
do
    let j+=1
    echo mv $i ${i%??_*}_bloomy_$(printf "%05d\n" $j).wav
done

Then it displays the followin result :

mv chp01_00001.wav chp_bloomy_00001.wav
mv chp01_00002.wav chp_bloomy_00002.wav
mv chp02_00001.wav chp_bloomy_00003.wav
mv chp02_00002.wav chp_bloomy_00004.wav

Remove the "echo" after havin checked it does what you need

We would use mv command to move/rename files. Try the below script.

for file in `ls -1 home/bloomy/path/wav/*` # FYI  ls -one(1) not alphabet L
do
    NEW_FILE=$(echo $file | sed 's/.._/_bloomy_/')
    mv "$file" "$NEW_FILE"
    echo "Moved file: $file"
done

Thanks!

Ctsgnb, your code gave me files of the format

chp*.wav_bloomy_00001

but I needed the _bloomy_00001 before the .wav and didn't know how to change that.

Michaelrozar17, your code worked fine.

Thanks a lot!

---------- Post updated at 09:47 AM ---------- Previous update was at 08:40 AM ----------

I just noticed that with the code from Michaelrozar17, the order of the files in the folder changes and they seem to be numbered arbitrarily.

But I need the files in the same order as before, just with the filenames changed.

Any suggestions?

Cheers!

My code was in ksh

I think that by running the script some of your files got replaced. For example, chp01_00001.wav is renamed to chp_bloomy_00001.wav, and chp02_00001.wav is renamed to chp_bloomy_00001.wav too.

Watch out

The Code provided by Michaelrozar does not change the indexing of the file so that
chp01_00001.wav
and
chp02_00001.wav

will be respectively moved

from chp01_00001.wav to chp_bloomy_00001.wav
and
from chp02_00001.wav to chp_bloomy_00001.wav

... Same target name !!! This will overwrite the initial copy of the chp01_00001.wav ...

1 Like

Thanks, it's working now!

I used the ksh code and everything worked out fine.

Ooops.. My bad. Below is the modified code.

!#/bin/ksh
for file in `ls -1 home/bloomy/path/wav/*` # FYI  ls -one(1) not alphabet L
do
    (( ++i ))
    NEW_FILE=$(echo $file | sed 's/\(.._\)\(00*\)\(.*$\)/_bloom_\2/')
    mv "$file" "${NEW_FILE}${i}.wav"
    echo "Moved file: $file"
done