David Silva
David Silva

Reputation: 1091

How can I recursively change file names to lower case and update all references to them?

I need to rename all the files in the MathJax library to lower case and update all references to them in order to conform to some SVN restrictions. Does anyone have a bash script that will do this for me? Thanks.

Upvotes: 1

Views: 2880

Answers (2)

David Silva
David Silva

Reputation: 1091

Here's the solution I came up with. The script is based on another Stack Overflow answer: How to create a bash script that will lower case all files in the current folder, then search/replace in all files?. I just changed it to go through the whole directory tree. One thing that stumped me for a while was the need to provide an empty string after the -i switch for sed to get it to work with Mac OS X:

#!/bin/bash
find . -type f -name '*.js' -print0 | while IFS= read -r -d '' file; do
#Check to see if the filename contains any uppercase characters
    oldfilename=$(basename $file)
    iscap=`echo $oldfilename | awk '{if ($0 ~ /[[:upper:]]/) print }'`
    if [[ -n $iscap ]]
    then
#If the filename contains upper case characters convert them to lower case
        newname=`echo $oldfilename | tr '[A-Z]' '[a-z]'` #make lower case
#Perform various search/replaces on the file name to clean things up
        newname=$(echo "$newname" | sed 's/---/-/')
        newname=$(echo "$newname" | sed 's/--/-/')
        newname=$(echo "$newname" | sed 's/-\./\./')
        newname=$(echo "$newname" | sed 's/there-s-/theres-/')
#Rename file
        newpathandfile=$(dirname $file)/$newname
        echo "Moving $file\n To $newpathandfile\n\n"
        mv $file $newpathandfile
#Update all references to the new filename in all php files
        find . -type f -name '*.js' -print0 | while IFS= read -r -d '' thisfile; do
            sed -i '' -e "s/$oldfilename/$newname/g" $thisfile
            echo "$thisfile s/$oldfilename/$newname/g"
        done
    fi
done

I renamed directories partly by script and partly manually. There were other types of files (web fonts in particular) that I was able to rename using this script but by specifying the appropriate file extension.

The final steps for the MathJax customization involved transforming to lowercase the value that goes into config.root. I couldn't find anyone else who had had to convert the whole MathJax library to lowercase file and directory names, so that part of this question/answer may not have much value for others.

The script I came up with takes a while to run but that wasn't an issue for me; I don't think I'll ever have to run it again!

Upvotes: 0

LogicIO
LogicIO

Reputation: 623

In order to rename files from uppercase to lowercase, you can use the rename command. In this case you can enter the directory containing the library files and run following command to rename all the files to lower case:


find . -exec readlink -e '{}' \; | xargs rename  'y/A-Z/a-z/'

Let me know your feedback after trying this out.

Upvotes: 1

Related Questions