LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices


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

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


Hi,
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
ranger_nemo
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...
Code:
#!/usr/bin/python

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
ranger_nemo
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 www.gnu.org or www.fsf.org.

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
bluej
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.

cheers

#!/bin/sh
#

for oldname in $@; do
newname=`echo $oldname | tr 'A-Z ' 'a-z_'`
test "$newname" != "$oldname" && mv -v $i $newname
done
 
Old 07-10-2004, 05:02 AM   #5
rob19
Member
 
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:

Code:
#!/bin/sh
#

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

Incase anybody else (newbies) come across this post by searching the archives like I did.
 
Old 05-16-2005, 02:15 PM   #6
grimse
Member
 
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?
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:22 PM.

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
Open Source Consulting | Domain Registration