LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Shell script for changing filename case (https://www.linuxquestions.org/questions/linux-software-2/shell-script-for-changing-filename-case-676526/)

Steve W 10-15-2008 10:41 AM

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

unSpawn 10-15-2008 12:55 PM

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.

Steve W 10-16-2008 03:01 AM

Okay, thanks. And that's a handy tip about using "echo" to see the results of any command before doing it.

Steve W 10-16-2008 03:12 AM

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?

jschiwal 10-16-2008 03:17 AM

Put the '[[:upper:]]' and '[[:lower:]]' in single quotes.

Steve W 10-16-2008 03:31 AM

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

jlinkels 10-16-2008 05:59 AM

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

ghostdog74 10-16-2008 06:08 AM

Quote:

Originally Posted by Steve W (Post 3310901)
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


Steve W 10-17-2008 03:15 AM

Okay thanks for the input - plenty of ideas there.


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