LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   quick rename many files (https://www.linuxquestions.org/questions/programming-9/quick-rename-many-files-270992/)

cadj 12-27-2004 10:12 PM

quick rename many files
 
ive wrote a script to convert all my *.wma into *.mp3, heres what it looks like

#-----------

#!/bin/bash

for i in *.wma; do mv "$i" `echo $i | tr ' ' '_'`; done

for i in *.[Ww][Mm][Aa]; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done

for i in *.wma ; do mplayer -vo null -vc dummy -af resample=44100 -ao pcm -waveheader $i && lame -m s audiodump.wav -o /home/cody/$i.mp3; done

#--------------

this works well, however now i have

song1.wma.mp3
song2.wma.mp3

what i want to do now is rename all the *.wma.*

i tried this

#---------
for i in *.wma.* ; do mv "$i" `echo $i | tr '.wma.' '.'`; done
# ---------

but all w's m's and a's are replaces with .

can anyone tell me what ive done wrong?

wapcaplet 12-27-2004 10:19 PM

Try:

Code:

for i in *.wma.* ; do mv "$i" `echo $i | sed -e "s/\.wma$/.mp3/"`; done

cadj 12-27-2004 10:38 PM

sorry but no go

this is my code now

#----
#!/bin/bash

for i in *.wma; do mv "$i" `echo $i | tr ' ' '_'`; done

for i in *.[Ww][Mm][Aa]; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done

for i in *.wma ; do mplayer -vo null -vc dummy -af resample=44100 -ao pcm -waveheader $i && lame -m s audiodump.wav -o /home/cody/$i.mp3; done

for i in *.wma.* ; do mv "$i" `echo $i | sed -e "s/\.wma$/.mp3/"`; done
#------

in the end im still left with song1.wma.mp3

an error is put to the console though, here it is


mv: `01_falling_down.wma.mp3' and `01_falling_down.wma.mp3' are the same file
cody@blue:~$

Dark_Helmet 12-27-2004 11:42 PM

Two options:

script code:
Code:

for file in *.wma.* ; do mv ${file} $( echo ${file} | sed 's@.wma@@' )  ; done
or command-based:
Code:

rename ".wma" "" *.wma.*
Do a man rename for more details about the rename command

cadj 12-28-2004 12:09 AM

ok ive got it, thanks

can i just ask something else quick, how in the script can i put my current directory into a variable?

eg, i have just gone to /home/cody/music/band

i want to run the script that is now in /bin, but hhave it work in my current path

Dark_Helmet 12-28-2004 12:22 AM

You can store the current directory like so:
Code:

current_directory=$( pwd )
The script starts in the directory it was launched from (not where the script resides). For instance, if you launched it from /home/cody/music/band and the script was located in /bin, the script would start execution within /home/cody/music/band. After that, the script can change directories with explicit 'cd' commands.

If you want the script to execute from some other location (not where you executed it from the command line), then you'll need to give it command line arguments and modify your script accordingly.

Hope that's useful.

cadj 12-28-2004 12:42 AM

ahh ok thanks a lot

bigearsbilly 12-29-2004 04:14 AM

the current directory is usually stored in a variable called
$PWD

try also ( a bit neater )
Code:

for file in *.wma;do
new_file=${file%.wma}.mp3
echo mv $file $new_file
done

billy


All times are GMT -5. The time now is 01:51 PM.