nix, shell, perl, php, mysql and mac os x tips and tricks

Monday, July 23, 2007

Shell script to re-name first part of filename to uppercase, and the extension to lowercase

For example ck100.Jpg -> CK100.jpg. Also removes spaces. Must be run in the directory the files are located in.
for file in *; do myfile=`echo $file | sed -e 's/ //g'`; first=`echo $myfile | sed 's/^\(.*\)\.\(.*\)/\1/'|tr '[a-z]' '[A-Z]'`; ext=`echo $myfile | sed 's/^\(.*\)\.\(.*\)/\2/'|tr '[A-Z]' '[a-z]'`;  mv "$file" `echo "$first.$ext"`; done

No comments:

Post a Comment