Bash Script To Rename File To Lowercase (web-safe Filename For Upload To WordPress)

Here’s a neat little bash script that converts all uppercase characters in a filename to lowercase and renames the files. As you know the filenames and URLs on the web are case sensitive. It’s always a good practice to replace spaces with a dash - and use all lowercase letters in filenames. Here’s the shell script. Two things you need to do:

  1. Save the following code in a file named preferably (web-safe-rename). dot sh means it’s a shell script.
  2. Give it execute permissions
  3. Try it somewhere safe
# lowerit
# convert all file names in the current directory to lower case
# only operates on plain files--does not change the name of directories
# will ask for verification before overwriting an existing file
# first argument should be the current file name
# change $1 to `ls` in the next line to rename all files without passing filename as the argument / will automatically take from ls
for x in "$1"
    if [ ! -f "$x" ]; then
        echo "$x" 'not found'
    lc=`echo "$x" | tr '[A-Z]' '[a-z]' | tr -s '[:space:]' | tr ' ' '-' | tr -cd 'A-Za-z0-9-.'`
    #echo "$x" | tr '[A-Z]' '[a-z]' | tr -s '[:space:]' | tr ' ' '-'
    if [ $lc != "$x" ]; then
        mv -i "$x" "$lc"
        echo Renamed: "$x" to "$lcn"

With some minimal changes in code you’ll be able to do a mass rename or just a one-off rename by passing the filename as the first parameter. Tada!

Hold on. Can you make this script better?

Divi WordPress Theme