Operating System - Linux
1832854 Members
3060 Online
110047 Solutions
New Discussion

Re: Script to find any capital letter and change it to lower case?

 
SOLVED
Go to solution
Coolmar
Esteemed Contributor

Script to find any capital letter and change it to lower case?

Is there a way to find all files with a capital letter...any capital letter...in the filename and change it to lowercase? It would have to be with the find command as the subdirectories are VERY deep in some cases.

Thanks!
11 REPLIES 11
Aussan
Respected Contributor

Re: Script to find any capital letter and change it to lower case?

i created a test directory and touched a few files that had upper case letters in the name

this worked for that directory

for file in `ls`
do
mv $file `echo $file | tr '[:upper:]' '[:lower:]'`

done


i would add * after ls if you want it to do subdirectories also
The tongue weighs practically nothing, but so few people can hold it
Bernd Reize
Trusted Contributor

Re: Script to find any capital letter and change it to lower case?

find | while read fname; do
lname=$(echo $fname | tr "[:upper:]" "[:lower:]")
[ $lname != $fname ] && mv $fname $lname
done

Beware that this script may run a very long time (depending on amount of directories) since it finds every file and tests for capital letters.
Coolmar
Esteemed Contributor

Re: Script to find any capital letter and change it to lower case?

Thanks Aussan, but I would need to do a "find" of all files with a capital letter and change it to a lower case. An "ls" will not work as the subdirectories go very very deep in some cases.
spex
Honored Contributor

Re: Script to find any capital letter and change it to lower case?

Hi Coolmar,

#!/usr/bin/sh
FPATH=/foo/bar
find ${FPATH} -type f -print > /tmp/flist.${$}.out
while read L
do
{
DIR=${L%/*}
F1=${L##*/}
F2=$(echo ${F1} | tr '[:upper:]' '[:lower:]')
if [ ${F1} != ${F2} ]
then
mv ${DIR}/${F1} ${DIR}/${F2}
fi
} done < /tmp/flist.${$}.out
rm -f /tmp/flist.${$}.out
exit 0

PCS
James R. Ferguson
Acclaimed Contributor
Solution

Re: Script to find any capital letter and change it to lower case?

Hi:

Perl makes this easy. This will lowercase the names of all files with any uppercase letters. This will be done recursively in the directory (or directories) that you pass as an argument. Only files (not directory names) will be changed.

The _current_working_ directory will be used if you run the script without any argument!

# cat /.rename
#!/usr/bin/perl
#@(#)rename $ Find files with uppercase letters and lowercase - JRF $
use strict;
use warnings;
use File::Find;

my @path = @ARGV ? @ARGV : getcwd() =~ m{^/$} ? ('/') : ('.');
my @files = ();
my ( $oldname, $newname );

find( sub { push( @files, $File::Find::name ) if m/[A-Z]+/ && -f $_ }, @path );

for $oldname (@files) {
($newname = $oldname) =~ tr [A-Z] [a-z];
rename( $oldname, $newname );
}
1;

...run as:

# ./rename /path

Regards!

...JRF...
Sandman!
Honored Contributor

Re: Script to find any capital letter and change it to lower case?

The command pipeline below will do what you're looking for so give it a try:

# find . -type f | awk '{f=tolower($0);system("mv "$0" "f)}'

~hope it helps
A. Clay Stephenson
Acclaimed Contributor

Re: Script to find any capital letter and change it to lower case?

One danger in these scripts is not testing for the existence of a file before moving. For example,
suppose that you have files "myfile" (the good) and "MyfilE" (the bad). Unless a test is done first the "mv MyfilE myfile" will overwrite the existing file.
If it ain't broke, I can fix that.
spex
Honored Contributor

Re: Script to find any capital letter and change it to lower case?

@Sandman!,

Passing the entire $0 to tolower() causes directory names containing uppercase characters to also be converted to lowercase, resulting in "mv: ... No such file or directory".

PCS
Coolmar
Esteemed Contributor

Re: Script to find any capital letter and change it to lower case?

Thanks folks.
James R. Ferguson
Acclaimed Contributor

Re: Script to find any capital letter and change it to lower case?

Hi (again):

Clay makes an excellent point! We can easily handle this and cleanup the problem PCS noted:

# cat ./rename
#!/usr/bin/perl
#@(#)rename $ Find files with uppercase letters and lowercase - JRF $
use strict;
use warnings;
use Cwd;
use File::Basename;
use File::Find;

my @path = @ARGV ? @ARGV : getcwd() =~ m{^/$} ? ('/') : ('.');
my @files = ();
my ( $dirname, $basename, $oldname, $newname );

find( sub { push( @files, $File::Find::name ) if -f $_ && m/[A-Z]+/ },
@path );

for $oldname (@files) {
$dirname = dirname($oldname);
$basename = basename($oldname);
( $newname = $basename ) =~ tr [A-Z] [a-z];
$newname = $dirname . '/' . $newname;
if ( -f $newname ) {
print STDERR "'$oldname' not renamed; '$newname' exists!\n";
}
else {
rename( $oldname, $newname )
or print STDERR "'$oldname' not changed\n";
}
}
1;

Regards!

...JRF...
Hunki
Super Advisor

Re: Script to find any capital letter and change it to lower case?

Slightly modifying Aussan's script :

#!/bin/ksh
for file in `ls`
do
mv $file `echo $file |tr "[:lower:]" "[:upper:]"`
done