1833151 Members
3131 Online
110051 Solutions
New Discussion

Re: Character set

 
peterchu
Super Advisor

Character set

If there are 500 files in a path, all file name are 8 characters and with block letter and lowercase eg. T3aXvb6i , T3deK3s3 , how to change all characters to lowercase ? thx
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: Character set

A script something like this (after cd'ing to desired directory):


#!/usr/bin/sh
ls | while read FNAME
do
NEW_FNAME=$(echo "${FNAME}" | tr "[A-Z]" "[a-z]")
if [[ -e "${NEW_FNAME}" ]]
then
echo "File \"${NEW_FNAME}\" already exists. Cannot convert." >&2
else
mv "${FNAME}" "${NEW_NAME}"
fi
done
If it ain't broke, I can fix that.
melvyn burnard
Honored Contributor

Re: Character set

use the tr command, for example:
tr "[:upper:]" "[:lower:]" file2

see the man page for tr
My house is the bank's, my money the wife's, But my opinions belong to me, not HP!
A. Clay Stephenson
Acclaimed Contributor

Re: Character set

Ooops,
mv "${FNAME}" "${NEW_NAME}"
should be:
mv "${FNAME}" "${NEW_FNAME}"
If it ain't broke, I can fix that.
Alan Meyer_4
Respected Contributor

Re: Character set

Here's the script

#! /bin/ksh
ls * |while read FILE ;do
LFILE=`echo $FILE |tr '[A-Z]' '[a-z]'`
mv $FILE $LFILE
done
" I may not be certified, but I am certifiable... "
A. Clay Stephenson
Acclaimed Contributor

Re: Character set

Actually, it's rather important to quote both the old and new filenames because your existing filenames might contain whitespace (though discouraged, those pesky spaces are perfectly legal in UNIX pathnames) and without quoting your script just exploded. Also, it's rather important to check for the existence of the new filename before renaming the old one or you just lost data.
If it ain't broke, I can fix that.
Alan Meyer_4
Respected Contributor

Re: Character set

I must defer to Mr Stephenson on this issue as well as the check to see if the new filename exists statement. Very good job.
" I may not be certified, but I am certifiable... "