-   Programming (
-   -   bash help renaming files (

kahn 06-12-2005 09:55 AM

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

dub.wav 06-12-2005 10:50 AM

I don't get the point, but...

If you're using KDE, you can try krename. If not, a freshmeat search 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>

carl.waldbieser 06-12-2005 12:02 PM

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


for name in *; do
  mv "$name" $(echo -n $name | tr [A-Z' '] [a-z_])

perfect_circle 06-14-2005 05:12 AM

in order to do this recursively and change only the mp3 that have those problems try:

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"

if your files contain more that one space continuously, instead of having something like this _____ in the filenames, try this in the above script:

new_name=$(echo -n "${name%/*}/";echo -n "${name##*/}"|tr -s " "|tr ' [A-Z]' '_[a-z]');

kahn 06-16-2005 07:04 AM

cani add this to work in multiple directories?

kahn 06-16-2005 07:14 AM

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.

perfect_circle 06-16-2005 07:15 AM


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.

All times are GMT -5. The time now is 07:26 PM.