Computing.Net > Forums > Unix > Simple, rename upper to lower

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Simple, rename upper to lower

Reply to Message Icon

Original Message
Name: paul
Date: December 17, 2001 at 02:27:20 Pacific
Subject: Simple, rename upper to lower
Comment:

Hi, I am new to unix, very new but have a problem with files arriving in my directories from other systems, some in upper and some in lower case. I thought there would be an easy command to convert all file names in a directory from upper to lower or the other way round. But I cannot find a way. Can anyone help please.


Report Offensive Message For Removal


Response Number 1
Name: Brian Racer
Date: December 17, 2001 at 08:07:45 Pacific
Reply: (edit)

#!/usr/bin/perl

my $directory="/directory";
opendir(dir,$directory) || warn "Oh noooo ... $!\n";
foreach ( sort ( grep ( /^[^\.]+/ , readdir(dir) ) ) )
{
next if -d $_;
chomp;
if ( ! /[A-Z]/ ) {
print "file $_ is already in lowercase\n" ; next ; }
print "lets lowercase the file $_\n";
rename "$directory/$_" , $directory."/".eval{ tr/[A-Z]/[a-z]/; $_ };
}
close dir;


Report Offensive Follow Up For Removal

Response Number 2
Name: James Boothe
Date: December 17, 2001 at 08:17:09 Pacific
Reply: (edit)

And if you do not have perl installed, here is a non-perl solution. But there are ramifications to renaming files. If a process wants MYFILE, it will not find myfile. And you can have MYFILE and myfile in the same directory.

rename.sh downshift
will rename upper/mixed to lower case.

rename.sh upshift
will rename lower/mixed to upper case.

#!/bin/sh

if [ $# -ne 1 \
-o "$1" != upshift \
-a "$1" != downshift ]
then
echo '\nUsage: rename.sh [ upshift | downshift ]\n'
exit
fi

action=$1

for fn in *
do

if [ $action = upshift ] ; then
fn2=`echo $fn | tr -A "[:lower:]" "[:upper:]"`
else
fn2=`echo $fn | tr -A "[:upper:]" "[:lower:]"`
fi

if [ $fn = $fn2 -o $fn = rename.sh ] ; then
continue
fi

if [ -f $fn2 ] ; then
echo "Cannot $action $fn ($fn2 exists)"
else
echo "$action $fn ? [y|n] \c"
read ans
if [ "$ans" = y ] ; then
mv -i $fn $fn2
fi
fi
done

(end of script)

In case this script will be in the current directory, it has logic to prevent renaming itself, so if you call it something other than rename.sh, change that line of code.

As coded, the script asks for confirmation for each rename. If it gets a lowercase y, it will rename. If you do not want the confirmation logic, replace the last big if-statement with:

if [ -f $fn2 ] ; then
echo "Renaming $fn to $fn2 ... Cannot (target name exists)"
else
echo "Renaming $fn to $fn2 ..."
mv -i $fn $fn2
fi

The translate commands, instead of using :lower: and :upper: constructs, could also be coded:

fn2=`echo $fn | tr -A "[a-z]" "[A-Z]"`
fn2=`echo $fn | tr -A "[A-Z]" "[a-z]"`


Report Offensive Follow Up For Removal

Response Number 3
Name: matt daddy
Date: December 19, 2001 at 15:06:50 Pacific
Reply: (edit)

ls -l | tr A-Z a-z

if that doesn't work, here's a script that might...

for file in `ls -l`
do
tr A-Z a-z
done

or read about tr by typing man tr


Report Offensive Follow Up For Removal

Response Number 4
Name: Paul
Date: December 20, 2001 at 08:45:10 Pacific
Reply: (edit)

Thanks to you all, I will give them a try


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software