LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-27-2004, 10:12 PM   #1
cadj
Member
 
Registered: Aug 2003
Location: Melbourne Australia
Distribution: LinuxMint Elyssa
Posts: 374

Rep: Reputation: 32
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?
 
Old 12-27-2004, 10:19 PM   #2
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Try:

Code:
for i in *.wma.* ; do mv "$i" `echo $i | sed -e "s/\.wma$/.mp3/"`; done
 
Old 12-27-2004, 10:38 PM   #3
cadj
Member
 
Registered: Aug 2003
Location: Melbourne Australia
Distribution: LinuxMint Elyssa
Posts: 374

Original Poster
Rep: Reputation: 32
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:~$
 
Old 12-27-2004, 11:42 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
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
 
Old 12-28-2004, 12:09 AM   #5
cadj
Member
 
Registered: Aug 2003
Location: Melbourne Australia
Distribution: LinuxMint Elyssa
Posts: 374

Original Poster
Rep: Reputation: 32
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
 
Old 12-28-2004, 12:22 AM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
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.
 
Old 12-28-2004, 12:42 AM   #7
cadj
Member
 
Registered: Aug 2003
Location: Melbourne Australia
Distribution: LinuxMint Elyssa
Posts: 374

Original Poster
Rep: Reputation: 32
ahh ok thanks a lot
 
Old 12-29-2004, 04:14 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,432

Rep: Reputation: 203Reputation: 203Reputation: 203
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
rename files allelopath Linux - General 5 07-05-2005 03:00 AM
Is there a quick way to rename files in xfce4 filemanager? servnov Linux - Newbie 2 11-13-2004 08:54 AM
How do I rename a lot of files at once? suresheva31 Linux - General 5 10-12-2004 02:57 PM
Can not rename files. Maximus2000 Linux - General 0 04-22-2004 01:36 PM
Need script to rename files joe_stevensen Programming 5 12-05-2003 06:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration