Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 05-12-2004, 09:25 AM   #1
Registered: Feb 2003
Location: Germany
Distribution: Slackware 9.1
Posts: 77

Rep: Reputation: 15
how to remove capital letters and blanks?

at the moment I am moving my music and documents from the windows partition to the linux partition.
There are many files containing blanks and capitals which can cause problems with the bash.

Is it possible to convert them automatically into something linux-friendly via script?
Old 05-12-2004, 09:46 AM   #2
Senior Member
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
I've had one for a few years...

import os, string, re

for fname in os.listdir(os.curdir):
  newname = string.lower(fname)
  newname = re.sub("\s", "_", newname)
  newname = re.sub("_-_", "_", newname)
  newname = re.sub("\'", "-", newname)
  os.rename(fname, newname)
Make a file in ~/bin , put the above code in it, then "chmod u+x filename". To run it, cd to the directory with all the MP3s, and run "filename". It worked, and I never used it too often, so I never updated it to be able to add a directory to the command... "filename ~/mp3s/".

<< EDIT >>

If you want to change it a bit...

The string.lower changes the filename to all lowercase characters.

First re.sub replaces spaces with underscores.
Second replaces a underscore-hyphen-underscore with a single underscore.
Third replaces single-quotes with a hypen.

Last edited by ranger_nemo; 05-12-2004 at 09:50 AM.
Old 05-12-2004, 08:30 PM   #3
Senior Member
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
Oops... I forgot to add...

Copyright (C) 2004 -- Ranger Nemo

The above script is free software... You can use / modify / distribute it under the terms of the GNU General Public License. You can find a copy of the GPL on either or

The above script was written with the hopes that it will be useful, but has no warranty... Not even an implied warranty of fitness for use. Use it at your own risk.

Old 05-13-2004, 04:36 AM   #4
LQ Newbie
Registered: Mar 2004
Location: japan
Distribution: debian
Posts: 24

Rep: Reputation: 15
below is a shell script that does the job.
it converts all uppercase letter to lowercase, and replaces spaces with underscores.
parameter of the script are the files you want to rename.



for oldname in $@; do
newname=`echo $oldname | tr 'A-Z ' 'a-z_'`
test "$newname" != "$oldname" && mv -v $i $newname
Old 07-10-2004, 05:02 AM   #5
Registered: Mar 2003
Location: New Zealand
Distribution: Red Hat Linux 8 & 9
Posts: 32

Rep: Reputation: 15
I know this is an older post now but I tried the shell script from bluej and got an error. I had to replace $i with $oldname as follows:


for oldname in $@; do
newname=`echo $oldname | tr 'A-Z ' 'a-z_'`
test "$newname" != "$oldname" && mv -v $oldname $newname

Incase anybody else (newbies) come across this post by searching the archives like I did.
Old 05-16-2005, 02:15 PM   #6
Registered: Oct 2004
Location: Oldenburg, Germany
Distribution: FC4
Posts: 42

Rep: Reputation: 15
hello erverybody.
if i try to convert data located under my mounted fat32 partition i always get the message "file x and file y are the same", but there`s no renamed file in the folder. converting files within / works fine.
has anyone a idea what`s my error?


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to preserve capital letters? kosa Linux - General 1 07-14-2005 01:44 PM
Capital i shows up as [ {O_o} Linux - Newbie 0 10-06-2004 08:33 PM
adduser fails with capital letters in a RH 9 and no in other RH 9 michi Linux - Newbie 1 06-11-2004 09:56 AM
usernames with capital letters in RH9 orange400 Linux - General 2 04-27-2004 05:57 AM
capital letters at the console alim Slackware 5 04-25-2002 01:32 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:07 AM.

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