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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to rename files in a folder - please help with script
# 1  
Old 06-01-2011
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
Code:
chp01_00001.wav
chp01_00002.wav
....
chp02_00001.wav
chp02_00002.wav
....

but I want them to have the following names:
Code:
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":
Code:
#!/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:
Code:
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

Last edited by Franklin52; 06-02-2011 at 10:29 AM.. Reason: Please use code tags
# 2  
Old 06-01-2011
Code:
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 :
Code:
# ls -1 *.wav
chp01_00001.wav
chp01_00002.wav
chp02_00001.wav
chp02_00002.wav

If i run the following commands :
Code:
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 :

Code:
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

Last edited by ctsgnb; 06-01-2011 at 01:42 PM..
# 3  
Old 06-01-2011
We would use mv command to move/rename files. Try the below script.
Code:
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


Last edited by michaelrozar17; 06-02-2011 at 04:24 AM..
# 4  
Old 06-01-2011
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!
# 5  
Old 06-01-2011
My code was in ksh
# 6  
Old 06-01-2011
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.
# 7  
Old 06-01-2011
Quote:
Originally Posted by michaelrozar17
We would use mv command to move/rename files. Try the below script.
Code:
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

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 ...
This User Gave Thanks to ctsgnb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to rename all files and folder containing underscore?

I want to rename all files and folder containing underscore in name and replace underscore with hyphen. Currently I am using following code, rename '_' '-' */*/* It was working but now it is showing me "Argument list too long" Please help! (2 Replies)
Discussion started by: opticalpigion
2 Replies

2. Shell Programming and Scripting

Need help in writitng a script to rename file name and copy to other folder

Hi All, My requirement is as follows: A file (say abc) will be having list of the .txt file names. I need to read this abc file line by line and rename the .txt file names inside it and move them to other folder/path. Eg: abc ------- file1.txt file2.txt file3.txt Output (should... (1 Reply)
Discussion started by: pavan.yadalla
1 Replies

3. UNIX for Dummies Questions & Answers

looping through files, doing something, and rename in new folder

Hi, I'm really new at this but have several hundred files that i need to do something with and save with a new name. The files are 26 columns wide. I want to select some for renaming. I've figured out what to do to each file ("file-1.CSV"| grep -v "=" | cut -f 1-4,9,14,15,18,19,20,21,22,24,26... (2 Replies)
Discussion started by: bob101
2 Replies

4. UNIX for Dummies Questions & Answers

Bash script to rename all files within a folder...

Hi. I don't have any experience with making scripts in bash. I need a simple script to rename all files in a folder to the format file1.avi, file2.avi, file3.avi, and so on..... Please note that the original files have different filenames and different extensions. But they all need to be... (2 Replies)
Discussion started by: dranzer
2 Replies

5. UNIX for Dummies Questions & Answers

Using Vim or Ex to rename all files in a folder

Is there a way to use Vim or Ex to rename all files in a folder? For example if i have a folder with Apples1.txt, Apples2.txt, Apples3.txt, could I rename all files so they are Oranges1.txt, Oranges2.txt, Oranges3.txt ? I know with :bufdo %s/apples/oranges/g|update you can do this for inside... (5 Replies)
Discussion started by: VimNewUser
5 Replies

6. Linux

rename files in a folder with date&time stamp

Hi, I want to rename all the files (more than 100 files) in a fodler to another folder with date&time stamp. foe eg, file1.dat file2.dat file3.dat .. to be renamed as file1100629_16_30_15.txt (yy-mon-dd_hh_mi_ss) file1100629_16_30_16.txt .. so on (2 Replies)
Discussion started by: feroz
2 Replies

7. Shell Programming and Scripting

Rename folder/directory after running sort script

Hello, I'm trying to create a script that sorts documents by author (found in file name) and then create a directory for that author. For example, Input: John - Paper_1.txt John - Paper_2.txt Mark - Paper_1.txt Jill - Paper_1.txt Output: dir/John/Paper_1.txt dir/John/Paper_2.txt... (1 Reply)
Discussion started by: jl487
1 Replies

8. Shell Programming and Scripting

Copy files from folder and rename them

hello, I need to build a shell script that receives the folder to copy by parameter and copy all files except thumb.db to another folder and rename them like, file.jpg renamed to file_bb1.jpg. can someone help me Thanks (4 Replies)
Discussion started by: zeker
4 Replies

9. Shell Programming and Scripting

rename all the files in a folder..

Hi Guys, I have 5000 files in a folder. all are .DAT files. I want to rename them as .TXT files. I tried the following command. mv *.DAT *. TXT But it is throwing an error. Can you please tell me what am i doing wrong. Thanks & Regards, Magesh. (11 Replies)
Discussion started by: mac4rfree
11 Replies

10. UNIX for Dummies Questions & Answers

rename files in a folder

i have a folder that contains 100's of files: abc, bca, def, ghi.... i want to rename abc with 1, bca with 2, def with 3, ghi with 4 and so on. my way, i create a file.txt with contents: 1 2 3 4 i use while loop to rename my files. without using file.txt, i just want to rename abc with... (4 Replies)
Discussion started by: tjmannonline
4 Replies
Login or Register to Ask a Question