Subscribe to
NSLog(); Header Image

Batch Lowercase on the Mac OS X CLI

In a bash prompt:

for filename in *                # Traverse all files in directory.
   fname=`basename $filename`
   n=`echo $fname | tr A-Z a-z`  # Change name to lowercase.
   if [ "$fname" != "$n" ]       # Rename only files not already lowercase.
     mv $fname $n

Thanks, Aaron.

5 Responses to "Batch Lowercase on the Mac OS X CLI"

  1. I found the answer in the advanced BASH scripting guide. They list two answers, the first one is the one I initially sent Erik. The second one was right below it and supports filenames with spaces/newlines in it.

    for filename in * # Not necessary to use basename,

    # since "*" won't return any file containing "/".

    do n=`echo "$filename/" | tr '[:upper:]' '[:lower:]'`

    # POSIX char set notation.

    # Slash added so that trailing newlines are not

    # removed by command substitution.

    # Variable substitution:

    n=${n%/} # Removes trailing slash, added above, from filename.

    [[ $filename == $n ]] || mv "$filename" "$n"

    # Checks if filename already lowercase.


  2. For those who are not terminal inclined, I would recommend the excellent R-Name which can do this and much, much more. (I am not in any way connected to the author, just a fan of his software.)

  3. Automater is pretty good for writing basic file rename scripts, and you can store them in the Finder's contextual menu. Not much use for the CLI, but otherwise pretty decent.

  4. I use a (*GASP*) perl script. It's not very robust, but it works for what I use it for...


    foreach (@ARGV) { rename $_, "\L$_\E"; }

  5. There's actually a bug in the original script. The basename call will have odd effects. A more flexible approach is to make it a function in the shell. You can just put this in your .profile:

    tolc () {

    for f in $*; do

    lc=$(echo "$f" | tr A-Z a-z)

    [ "$lc" == "$f" ] || mv "$f" "$lc"



    There are some advantages to this version. Namely it handles files with embedded spaces in the filename correctly and you can control what to run it on.

    usage: tolc files ...

    So things like:

    tolc A*.jpg


    tolc *.DAT

    Will all work as expected. (I didn't test this, so if it barfs, let me know and I'll tweak it)