LinuxQuestions.org
Help answer threads with 0 replies.
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 06-12-2005, 09:55 AM   #1
kahn
Member
 
Registered: Feb 2004
Location: Seattle
Distribution: FC5
Posts: 109

Rep: Reputation: 15
bash help renaming files


Hello everybody...

I have a lot of mp3s from windows... and all of them have spaces and caps...

I want to rename all the files so that empty spaces become _ (underscores) and all the capital letters become lowercase...

Can anybody help me out? Thanks a lot!

Martin Seidl
Fc3/2.6.9-1.667
 
Old 06-12-2005, 10:50 AM   #2
dub.wav
Member
 
Registered: Aug 2003
Location: Norway
Distribution: FC4
Posts: 83

Rep: Reputation: 20
I don't get the point, but...

If you're using KDE, you can try krename. If not, a freshmeat search http://freshmeat.net/rename might help.

No file managers I know of has trouble handling spaces in filename, and they're not really any trouble in bash either, i.e. "Artist<tab>
 
Old 06-12-2005, 12:02 PM   #3
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Here is a simple script that should do it. Run it in the directory you want to convert.

Code:
for name in *; do
   mv "$name" $(echo -n $name | tr [A-Z' '] [a-z_])
done
 
Old 06-14-2005, 05:12 AM   #4
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
in order to do this recursively and change only the mp3 that have those problems try:
Code:
find . -type f -name "*.[Mm][Pp]3" | while read name; do
   new_name=$(echo -n "${name%/*}/";echo -n "${name##*/}"|tr ' [A-Z]' '_[a-z]');
   if [ "$name" != "$new_name" ]; then
      mv "$name" "$new_name"
   fi
done
if your files contain more that one space continuously, instead of having something like this _____ in the filenames, try this in the above script:
Code:
new_name=$(echo -n "${name%/*}/";echo -n "${name##*/}"|tr -s " "|tr ' [A-Z]' '_[a-z]');
 
Old 06-16-2005, 07:04 AM   #5
kahn
Member
 
Registered: Feb 2004
Location: Seattle
Distribution: FC5
Posts: 109

Original Poster
Rep: Reputation: 15
cani add this to work in multiple directories?
 
Old 06-16-2005, 07:14 AM   #6
kahn
Member
 
Registered: Feb 2004
Location: Seattle
Distribution: FC5
Posts: 109

Original Poster
Rep: Reputation: 15
nevermind... hey, thanks a lot for you guys... this was helpful and educational.

you should all be knighted!

oh wait, thats only the ms devil that gets to be nighted.
 
Old 06-16-2005, 07:15 AM   #7
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
Quote:
Originally posted by kahn
cani add this to work in multiple directories?
Since it works recursively, you can search and change all the mp3's in your disk.
 
  


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
bash help, renaming file extensions trey85stang Linux - General 8 07-21-2005 04:51 PM
Batch Renaming in bash xushi Programming 6 07-07-2005 03:24 PM
How do I make a bash sheet for renaming files? Cyberian Linux - Newbie 16 03-16-2005 08:08 AM
Renaming files in one go saurya_s Linux - Software 1 01-12-2004 01:16 PM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM


All times are GMT -5. The time now is 10:50 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration