Lowercase Filenames with Bash on Mac OS X

February 4th, 2010, filed under Apple, General

When saving files to my harddrive I’d like the filenames to be in lowercase. It just seems more clean that way. However, I do not like renaming all files manually. So here’s a little bash script that can do this automatically:

#!/bin/sh

if [ -n "$1" ]; then
  let count=0
  while [ "$1" ]; do
    f="$1"
    shift
    if [ -f "$f" ]; then
      lc=`echo $f | awk '{print(tolower($0));}'`
      if [ "$lc" != "$f" ]; then
        mv "$f" "$lc"
        ((count++))
      fi
    fi
  done
  echo "Result: $count files have been changed."
else
  echo "Usage: lowerit file(s)"
fi


An example showing the usage of the above script. See full image

Usage and examples

I’ve named this script lowerit and placed it in the /bin directory. You can read my previous post “Batch rename filename extension on a Mac” for more details about installing bash scripts on a Mac. Run this script by typing the following command:

~$: lowerit *

Using the asterisk (*) will lowercase all filenames in the current directory. It’s also possible to specify one single file, for example:

~$: lowerit Example.PHP

Combined with find you can also recursively change the filenames. The command to use should look something like this:

~$: find . -type f -exec lowerit {} \;

Off course this script can also be used to uppercase filenames as well. Just change awk print(tolower($0)) into awk print(toupper($0)) and your done!

Blimey, where's me comment!? Go on...write!

Have your say

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>