How to rename files to lowercase on Linux

Coming from Microsoft Windows, where the file system was basically case insensitive, I have had to change some of my development code bases to respect the case sensitive nature of the Linux ext file system.

To that end, I needed to rename some files to their lower case equivalent. More than that, it needed to rename recursively, diving into each folder one after another and renaming the files I wanted changed, which were all of the .cshtml files.

This is the script to do it:

#!/usr/bin/env bash

cd /home/brendan/development/

for SRC in `find . -depth -type f -name *.cshtml`
do
    DST=`dirname "${SRC}"`/`basename "${SRC}" | tr '[A-Z]' '[a-z]'`
    if [ "${SRC}" != "${DST}" ]
    then
    [ ! -e "${DST}" ] && mv -T "${SRC}" "${DST}" || echo "${SRC} was not renamed"
    fi
done

There are alternatives to be found online using the rename command, but I found that they didn’t work, as they only specified 2 arguments, where the current version of rename as of Ubuntu 16 requires at least 3 arguments.

 

Posted on Apr 29, 2016 in Articles & How To | Tags: TechnologyLinuxTerminal