LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-13-2006, 08:58 PM   #1
asilentmurmur
Member
 
Registered: Mar 2005
Location: Washington DC area
Posts: 214

Rep: Reputation: 30
how do i convert file names to lower case and remove the underscore from the name?


Hey everyone

do any of you know how to convert a set of file names in a directory, to lowercase and remove the underscores from their names? Is there a special shell script i could use or a special command? also how do i go about running it?
 
Old 07-13-2006, 09:04 PM   #2
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
Here you go:

Code:
for i in *; do mv "$i" "$(echo $i | tr '_' '')"; done
for i in *; do mv "$i" "$(echo $i | tr '[A-Z]' '[a-z]')"; done
That will get you started.

Regards,

Alunduil
 
Old 07-13-2006, 09:08 PM   #3
bushidozen
Member
 
Registered: Oct 2004
Posts: 215

Rep: Reputation: 30
To convert underscores to spaces:
If you have python installed on your computer, this script will do it:
Code:
#!/usr/bin/python
# the following script will traverse through all of the files
# within the directory where it is placed and replace each
# underscore within the filename with a space 

import os, sys
if len(sys.argv) == 1:
    filenames = os.listdir(os.curdir)
else:
    filenames = sys.argv[1:]
for filename in filenames:
    if '_' in filename:
        newfilename = filename.replace('_', ' ')
        print "Renaming", filename, "to", newfilename, "..."
        os.rename(filename, newfilename)
just copy and paste the above in a text file and make it executable. Then place it in the directory with the files you wish to change and click it. All of the underscores will convert to spaces. This code is runs faster than a comparable bash shell script and runs on windows (with python installed) too.

I will get back to you for converting to lower case.
 
Old 07-14-2006, 02:41 AM   #4
asilentmurmur
Member
 
Registered: Mar 2005
Location: Washington DC area
Posts: 214

Original Poster
Rep: Reputation: 30
awesome it worked thanks man! Do you know of any scripts that will remove spaces from filenames? I need my set of file names to have no spaces.
 
Old 07-14-2006, 02:59 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
A common way of replacing characters in the shell is to use variable expansion.
For example: "${filename// /_}" will replace all spaces in "$filename" with underscores. The "//" doubleslash will do it globally in the variable, and not the first one.
"${filename// /}" will remove spaces. You can use character classes as well:
"${filename//[ _]/}". This will remove all spaces and underscores.

Combining this with the earlier post:
for i in *; do mv "$i" "$(echo ${i//_/} | tr '[A-Z]' '[a-z]')"; done

Last edited by jschiwal; 07-14-2006 at 03:00 AM.
 
Old 07-14-2006, 07:23 PM   #6
bushidozen
Member
 
Registered: Oct 2004
Posts: 215

Rep: Reputation: 30
You can slightly modify the python code I gave you above:
Code:
#!/usr/bin/python
# the following script will traverse through all of the files
# within the directory where it is placed and replace each
# space within the filename with an underscore

import os, sys
if len(sys.argv) == 1:
    filenames = os.listdir(os.curdir)
else:
    filenames = sys.argv[1:]
for filename in filenames:
    if ' ' in filename:
        newfilename = filename.replace(' ', '_')
        print "Renaming", filename, "to", newfilename, "..."
        os.rename(filename, newfilename)
 
Old 07-15-2006, 12:01 AM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Quote:
newfilename = filename.replace(' ', '_')
I wonder what's the python method for converting upper to lower?
 
Old 07-15-2006, 12:26 AM   #8
bushidozen
Member
 
Registered: Oct 2004
Posts: 215

Rep: Reputation: 30
str.uppper() or str.lower(), where str is a string object. Take a look at this link for quick details.
 
Old 07-15-2006, 01:14 AM   #9
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Do I need two if statements or can it be shortened?
Code:
#!/usr/bin/env python

# Replace upper case with lower case
# Replace spaces with underscores

import os, sys
if len(sys.argv) == 1:
    filenames = os.listdir(os.curdir)
else:
    filenames = sys.argv[1:]
for filename in filenames:
    if filename.upper():
	newfilename = filename.lower()
    if ' ' in newfilename:
        newfilename = newfilename.replace(' ', '_')
        print "Renaming", filename, "to", newfilename, "..."
        os.rename(filename, newfilename)
 
Old 07-15-2006, 01:17 AM   #10
bushidozen
Member
 
Registered: Oct 2004
Posts: 215

Rep: Reputation: 30
It could be shortened, but I think that would make it take longer to run. I think it's fine as it is. It is a little more robust, and is easier to debug or change if necessary.
 
Old 07-15-2006, 02:22 AM   #11
bushidozen
Member
 
Registered: Oct 2004
Posts: 215

Rep: Reputation: 30
I just looked over your code, and I saw a couple of errors, so I rewrote it a little:
Code:
#!/usr/bin/python

# Replace upper case with lower case
# Replace spaces with underscores

import os, sys
if len(sys.argv) == 1:
    filenames = os.listdir(os.curdir)
else:
    filenames = sys.argv[1:]
for filename in filenames:
    newfilename = filename.lower()
    if ' ' in newfilename:
        newfilename = newfilename.replace(' ', '_')
        print "Renaming", filename, "to", newfilename, "..."
    os.rename(filename, newfilename)
This code will attempt to convert all filenames to lowercase (even those that are already lowercase), which could translate into a lot of wasted cycles (ie. if all of the files in the directory are already lowercase), but it actually runs faster than trying to determine its case as in the previous code (by using the if statement). Enjoy.
 
Old 07-15-2006, 06:46 AM   #12
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
That works also! Thanks
 
Old 07-15-2006, 08:32 AM   #13
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Stuck again!
I want to choose the directory and not sure how. This doesn't seem to work for /home/images
Code:
    filenames = os.listdir('images')
 
Old 07-15-2006, 01:14 PM   #14
bushidozen
Member
 
Registered: Oct 2004
Posts: 215

Rep: Reputation: 30
It already does that (almost). Just run the script followed by the directory path and the files in the directory you wish to change.

Example: Let's say you have a folder in you home directory called "folder" in it you have three files: ONE 1.txt, TWO 2.txt, and THREE 3.txt. We'll call your script "myscript", and it is located in your home directory.

To change the file ONE 1.txt, run:
Code:
$ myscript folder/ONE 1.txt
$ ls folder
one_1.txt TWO 2.txt THREE 3.txt
To change all of the files, run:
Code:
$ myscript folder/*
$ ls folder
one_1.txt two_2.txt three_3.txt
Or, you could place the script in /home/bin, and simply cd to a directory and run it.
 
Old 07-15-2006, 01:27 PM   #15
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Quote:
myscript folder/*
Thanks, that works.
I had tried it with just ./myscript images without the /*
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mount DVD While Preserve Upper Case File Names xptools Linux - Software 2 12-12-2005 10:18 AM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 04:09 PM
remove * from file nameS zchoyt Linux - Newbie 1 02-13-2005 04:33 PM
Lower case to upper case letter sudhasmyle Programming 1 12-03-2004 04:15 AM
chaging directory names to lower case andy753421 Linux - General 2 05-11-2004 02:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:52 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
Open Source Consulting | Domain Registration