LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Linux issue renaming files - BASH script (https://www.linuxquestions.org/questions/programming-9/linux-issue-renaming-files-bash-script-908634/)

giantanakim 10-17-2011 02:20 PM

Linux issue renaming files - BASH script
 
Hi Everyone, I hope that you can help. I have several thousand wav files which are named 1112223333 xx xxxxxxx xxxxxx.wav (yes those are spaces in the filename). I need to rename them so that each file is named 1112223333.wav. If there is a duplicate, I will need to add a _1 or _2 before the extension. All of these will be in the same directory. I have been searching the internet trying to find a way to do this with no luck.

crts 10-17-2011 02:58 PM

Hi and welcome to LQ,

try this:
Code:

for file in *; do cnt=1;newfile="${file%% *}"; while [[ -f "${newfile}.wav" ]];do newfile="${file%% *}_$((cnt++))";done;mv "${file}" "${newfile}.wav";done

giantanakim 10-17-2011 03:30 PM

HI CRTS:

This is almost perfect. This truncates the file name perfectly, but adds the _1 to all recordings. If that creates a duplicate, it adds a _2 beyond that.

I really do appreciate your help with this.

grail 10-17-2011 07:48 PM

Quote:

but adds the _1 to all recordings
According to the code provided this would only happen if the file already exists, as per the -f test in the while loop.

Can you confirm that you do not also have the original file with no _1?

David the H. 10-18-2011 01:28 AM

Not to be rude, but you know, if you had bothered to search the forum or the web first, you would have found literally hundreds of previous discussions on how to script the renaming of files.

Not to mention that there are at least half-a-dozen ready-made bulk rename programs already out there, meaning you don't have to reinvent the wheel if you don't want to.

giantanakim 10-18-2011 07:42 AM

CRTS:

Here are the result from the folder that I ran this in:

2032332597_1.wav
2035657813_1.wav
2036458257_1.wav
2038688232_1.wav
2083011585_1_1.wav
2083011585_1.wav
2085681311_1.wav
2085975441_1_1.wav
2085975441_1_2.wav
2085975441_1.wav

grail 10-18-2011 08:06 AM

Can show the original data prior to running the script? (I assume you have a backup)

giantanakim 10-18-2011 08:18 AM

Hi Grail:

I appreciate you looking at this. I do have all of these files backed up. Just for example, this is how the original file names look.

2033028167 by xxxxxx@xxxxxx @ 4_32_36 PM.wav (i xx'ed out the original email address)

With this naming convention, there are no duplicates as the time stamp will change
(i.e. 2033028167 by xxxxxx@xxxxxx @ 4_36_54 PM.wav)

grail 10-18-2011 09:49 AM

Well I just ran the script using the 2 examples in your post and ended up with:
Code:

2033028167_1.wav  2033028167.wav

crts 10-18-2011 10:28 AM

Quote:

Originally Posted by grail (Post 4501517)
Well I just ran the script using the 2 examples in your post and ended up with:
Code:

2033028167_1.wav  2033028167.wav

Yep, those are the same results that I got.
@OP: Did you make a typo while entering the line into the CLI? During the "development" the script was a bit buggy and it yielded similar results to what you posted. However, the version I supplied in my previous post worked. Did you run the script twice on the same set of files? In this case the names will get a bit weird. Yet still not the way you posted.

giantanakim 10-21-2011 09:49 AM

Thank you everyone for your help. After a little tweaking I was able to make this successfully work. I still get an error that says Cannot find [[: or something like that, but it does what it is supposed to do, so I am not worried about that at the moment.

Thanks again CRTS and grail!

crts 10-21-2011 10:00 AM

Quote:

Originally Posted by giantanakim (Post 4504391)
Thank you everyone for your help. After a little tweaking I was able to make this successfully work. I still get an error that says Cannot find [[: or something like that, but it does what it is supposed to do, so I am not worried about that at the moment.

Thanks again CRTS and grail!

Can you post the exact code that you actually typed into the CLI? Are you using bash or some other shell? Generally, the error you mention is a cause for concern.

giantanakim 10-24-2011 03:05 PM

Good afternoon CRTS:

Here is the code that I have been using:

for file in *.wav;
do cnt=1;
newfile="${file%% *}";
while [[ -f "${newfile}.wav" ]];
do newfile="${file%% *}_$((cnt++))";
done;
mv "${file}" "${newfile}.wav";
done

Like I said, it has the effect that I wanted, but it comes up with the error Cannot find [[: on every line.

One thing that I am not sure on, if there are files that get hit with this script more than once, it adds another .wav to the end, so the file name looks like 1112223333.wav.wav. Is there a way to fix that as well?

grail 10-25-2011 12:22 AM

Just to confirm ... you are using bash as your shell?

Also, the code you are using is entered at the command line or in a script?

giantanakim 10-25-2011 09:07 AM

You are correct that I have been using BASH and this is entered into a shell script.


All times are GMT -5. The time now is 03:12 PM.