Operating System - HP-UX
1753722 Members
4714 Online
108799 Solutions
New Discussion юеВ

how to convert files names to upper case

 
SOLVED
Go to solution
martin clarke_1
Occasional Advisor

how to convert files names to upper case

I have a directory full of files that have lower case names. I need to convert the first part of the name to upper case leaving the suffix in lower case.
e.g. myfile.dat becomes MYFILE.dat

I remember the use of "foreach file" command under csh but cant get the file conversion right.
I have been trying the following but cant get it to work.

foreach file (*.dat)
mv $file tr `basename $file .dat`.dat
end

Is there a better way, am I on the right track.
4 REPLIES 4
Cheryl Griffin
Honored Contributor
Solution

Re: how to convert files names to upper case

These have been suggested previously -

for i in `ls *\.*`
do
echo $i | awk -F. '{if (NF == 2) print toupper($1)"."$2}'
done

or
ls -1 abc.dat | awk -F "." '{system("mv" ""$0 "" toupper($1) ".dat" }'
"Downtime is a Crime."
Sundar_7
Honored Contributor

Re: how to convert files names to upper case

for F in myfile.dat something.exe
do
FILE=$(echo "$F" | sed 's/\(.*\)\./\1/' | tr "[:lower:]" "[:upper:]")
EXT=$(echo "$F" | sed 's/.*\(\..*\)/\1/' )
mv $F ${FILE}${EXT}
done
Learn What to do ,How to do and more importantly When to do ?
Michael D'Aulerio
Regular Advisor

Re: how to convert files names to upper case

This works in ksh:

typeset -u newfile
for oldfile in *.dat
do
newfile=${oldfile%.dat}
mv ${oldfile} ${newfile}.dat
done
Email: michael.n.daulerio@lmco.com
Muthukumar_5
Honored Contributor

Re: how to convert files names to upper case

We can do this in your try as,
# for file in `ls .`; do
> cp $file `echo $file | cut -d "." -f 1 | tr '[a-z]' '[A-Z]'`.dat
> done

It will do.
Easy to suggest when don't know about the problem!