LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 10-15-2008, 10:41 AM   #1
Steve W
Member
 
Registered: Mar 2007
Distribution: Ubuntu 12.04, Mint 12 LXDE
Posts: 431

Rep: Reputation: 36
Shell script for changing filename case


I'm looking for a script that will take all files in either the current, or a named, directory, and convert them all to lowercase.

I did a search of LinuxQuestions and came up with what I think is a similar solution. However, could someone confirm this for me, as I am naturally cautious about running any script that carries out file commands without knowing exactly how far it's going to go! Could someone confirm that the following will rename all files in the current directory ONLY to lowercase? I can understand (thanks to the script's comments) what "tr" does, but what's the meaning of the first line?

for oldname in `ls`; do
# tr translates characters from the first set specified to the corresponding character in the second set
#in this case uppercase to lowercase
newname=`echo $oldname | tr [:upper:] [:lower:]`
mv $oldname $newname

Steve
 
Old 10-15-2008, 12:55 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,264
Blog Entries: 54

Rep: Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852
The 'ls' as used here will use any aliases if defined first, so you should 'unalias ls' first or supply full path or prefix with a backslash. Then spaces in filenames will be handled "better" using say "find -print0|xargs -0" and why not "mv "${oldname}" $(echo "${oldname}" | tr [:upper:] [:lower:])" directly?..
If unsure you could test things either by using some shell flag like Bash "-n" or prefix "mv" with "echo" to just print the result without modifying anything.
 
Old 10-16-2008, 03:01 AM   #3
Steve W
Member
 
Registered: Mar 2007
Distribution: Ubuntu 12.04, Mint 12 LXDE
Posts: 431

Original Poster
Rep: Reputation: 36
Okay, thanks. And that's a handy tip about using "echo" to see the results of any command before doing it.
 
Old 10-16-2008, 03:12 AM   #4
Steve W
Member
 
Registered: Mar 2007
Distribution: Ubuntu 12.04, Mint 12 LXDE
Posts: 431

Original Poster
Rep: Reputation: 36
Actually, I just tried both the original script and your suggested one-line replacement, and neither works. On your one I get the error: mv: missing destination file operand after `'

Can you suggest what is wrong with the command?
 
Old 10-16-2008, 03:17 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Put the '[[:upper:]]' and '[[:lower:]]' in single quotes.
 
Old 10-16-2008, 03:31 AM   #6
Steve W
Member
 
Registered: Mar 2007
Distribution: Ubuntu 12.04, Mint 12 LXDE
Posts: 431

Original Poster
Rep: Reputation: 36
Okay thanks for the prompt reply. But upon Googling for this problem further, I found that there is a Rename command in Linux - and to do this it is "rename 'y/A-Z/a-z/' *", which in my case has worked fine.

Hmm - I didn't know Linux had a rename command. I thought you just used mv to rename files. Dunno where I got that from...
 
Old 10-16-2008, 05:59 AM   #7
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,087

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Rename is not a Linux command, it is a Perl script which happens to be part of most Linux distros. Your script was more "Linux pure". I discovered the existence of renema after writing my own script as well.

Do: less $(which rename)

BTW, when scripting it is not uncommon to start a script file like this:

$CP=/usr/bin/cp
$MV=/usr/bin/mv

and in you script use:
$MV $oldname $newname

In that way it is very easy to change the value of $CP for /bin/echo as to make all calls to cp echos.

If you ever write a script involving 'rm' it is even very unwise not to do so.

Furthermore by specifying the full path name you make your script independent from the environment. It is a very common problem that even carefully tested scripts won't run from cron -- unless you have all these full path names.

jlinkels
 
Old 10-16-2008, 06:08 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by Steve W View Post
I'm looking for a script that will take all files in either the current, or a named, directory, and convert them all to lowercase.

I did a search of LinuxQuestions and came up with what I think is a similar solution. However, could someone confirm this for me, as I am naturally cautious about running any script that carries out file commands without knowing exactly how far it's going to go! Could someone confirm that the following will rename all files in the current directory ONLY to lowercase? I can understand (thanks to the script's comments) what "tr" does, but what's the meaning of the first line?

for oldname in `ls`; do
# tr translates characters from the first set specified to the corresponding character in the second set
#in this case uppercase to lowercase
newname=`echo $oldname | tr [:upper:] [:lower:]`
mv $oldname $newname

Steve
if you have Python, here's a script you can use
eg usage:
Code:
# ls -1
A-file.txt
C-FILE.TXT
b-FILE.txt

# filerenamer.py -p ".*" -e "[a-z]" -l "*"
==>>>>  [ /home/C-FILE.TXT ]==>[ /home/c-file.txt ]
==>>>>  [ /home/b-FILE.txt ]==>[ /home/b-file.txt ]
==>>>>  [ /home/A-file.txt ]==>[ /home/a-file.txt ]

# filerenamer.py -p ".*" -e "[a-z]"  "*" #remove -l to commit
/home/C-FILE.TXT  is renamed to  /home/c-file.txt
/home/b-FILE.txt  is renamed to  /home/b-file.txt
/home/A-file.txt  is renamed to  /home/a-file.txt

# ls -1
a-file.txt
b-file.txt
c-file.txt
 
Old 10-17-2008, 03:15 AM   #9
Steve W
Member
 
Registered: Mar 2007
Distribution: Ubuntu 12.04, Mint 12 LXDE
Posts: 431

Original Poster
Rep: Reputation: 36
Okay thanks for the input - plenty of ideas there.
 
  


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
Shell script to read part of filename fatgoth Linux - Newbie 10 02-26-2012 08:21 AM
case esac in shell script vadirajcs Programming 16 08-10-2011 07:43 AM
changing filename using Bash Script tmbigrigg Programming 10 11-10-2007 07:02 AM
shell script:filename containing space ! Dr_Death_UAE Programming 3 12-16-2006 06:46 PM
help changing case on arguments to bourne shell script Maldain Programming 2 05-03-2005 10:18 AM


All times are GMT -5. The time now is 01:11 PM.

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