Rename files and folders to lower case letters

Here's a simple bash script that will help in renaming files and folders with uppper-case to lower-case letters.

Make sure you have a backup and test it with a small batch. Also read the whole comments section below if you have spaces in your file names.

#!/bin/bash

#
# Filename: rename.sh
# Description: Renames files and folders to lowercase recursively
#              from the current directory
# Variables: Source = x
#            Destination = y

#
# Rename all directories. This will need to be done first.
#

# Process each directory’s contents before the directory  itself
for x in `find * -depth -type d`;
do

  # Translate Caps to Small letters
  y=$(echo $x | tr '[A-Z]' '[a-z]');

  # check if directory exits
  if [ ! -d $y ]; then
    mkdir -p $y;
  fi

  # check if the source and destination is the same
  if [ "$x" != "$y" ]; then

    # check if there are files in the directory
    # before moving it
    if [ $(ls "$x") ]; then
      mv $x/* $y;
    fi
    rmdir $x;

  fi

done

#
# Rename all files
#
for x in `find * -type f`;
do
  # Translate Caps to Small letters
  y=$(echo $x | tr '[A-Z]' '[a-z]');
  if [ "$x" != "$y" ]; then
    mv $x $y;
  fi
done

exit 0

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

and this command?

I think what this work too.

find * -depth -execdir rename 'y/A-Z/a-z/' {} \;

Daniel

Mod for samba mounts to windows

It is a great script, but if you are trying to run it on a samba mount to a windows server... it cannot rename This to this. This mod does that in two steps by renaming This to this! and this! to this.

# Process each directory.s contents before the directory itself
find * -depth -type d | while read x
do

# Translate Caps to Small letters
y=$(echo "$x" | tr '[A-Z]' '[a-z]');
yprime="${y}!";

# create directory if it does not exit
if [ ! -d "$yprime" ]; then
mkdir -p "$yprime";
fi

# check if the source and destination is the same
if [ "$x" != "$yprime" ]; then

# move directory files before deleting
ls -A "$x" | while read i
do
ilower=$(echo "$i" | tr '[A-Z]' '[a-z]');
mv "$x"/"$i" "$yprime"/"$ilower";
done
rmdir "$x";

fi

# create directory if it does not exit
if [ ! -d "$y" ]; then
mkdir -p "$y";
fi

# move directory files before deleting
ls -A "$yprime" | while read i
do
mv "$yprime"/"$i" "$y";
done
rmdir "$yprime";
done

possible fix for spaces

Try quoting the arguments to mv..

mv "$x" "$y"

I've not checked to see if the rest of the script will handle the spaces ok though.

thank for that code. But why

thank for that code.
But why uses mkdir + rmdir

This seems enough :

# Filename: mv_lcase.sh
# Description: Renames files and folders to lowercase recursively
#              from the current directory
# Variables: Source = x
#            Destination = y

#
# Rename either all files or all directories
#
function Rename {
for x in `find * -type $1`;
do
  # Translate Caps to Small letters
  y=$(echo $x | tr '[A-Z]' '[a-z]');
  if [ "$x" != "$y" ]; then
    mv $x $y;
  fi
done
}

#
# Rename all directories. This will need to be done first.
#
Rename d
#
# Rename all files
#
Rename f

exit 0

will not work with folder tree

This will not find folders within folders as it would have been moved already.

This script works PERFECTLY!!

I used this in my music directory, and it took about 30 seconds to finish, but that is probably because I have a ton a music.

Thank you :-)

Just wanted to thank you for that useful script. Saved me a lot of time.

Cheers!

whatever

@$@$ up your files and tree if there are spaces in file...thanks for the out dated script

There is also the 'rename' (perl) script

Nice script - thank you for that!

This got me thinking that another way to do this would be to use the 'rename' command (this requires a standard 'perl' installation though, not just bash like your script).

Something like this *should* work (needs testing):
find . -type d |xargs rename 'y/A-Z/a-z/' && find . -type f |xargs rename 'y/A-Z/a-z/'

Have fun...

I would also like to say thanks

I just renamed 103 files in less than 3 seconds. It's greatly appreciated.

Nice but uneffective

It fails to convert files when they're spaced (like on "The House.jpg" it tries to stat The and not the hole file).

updated script that take into account any spaces in filenames

#!/bin/bash

#
# Filename: rename.sh
# Description: Renames files and folders to lowercase recursively
#              from the current directory
# Variables: Source = x
#            Destination = y

#
# Rename all directories. This will need to be done first.
#

# Process each directory’s contents before the directory  itself
find * -depth -type d | while read x
do

        # Translate Caps to Small letters
        y=$(echo "$x" | tr '[A-Z ]' '[a-z_]');

        # create directory if it does not exit
        if [ ! -d "$y" ]; then
                mkdir -p "$y";
        fi

        # check if the source and destination is the same
        if [ "$x" != "$y" ]; then

                # move directory files before deleting
                ls -A "$x" | while read i
                do
                  mv "$x"/"$i" "$y";
                done
                rmdir "$x";

        fi

done

#
# Rename all files
#
find * -type f | while read x ;
do
        # Translate Caps to Small letters
        y=$(echo "$x" | tr '[A-Z ]' '[a-z_]');
        if [ "$x" != "$y" ]; then
                mv "$x" "$y";
        fi
done

exit 0

me sale lo siguientes errores

: command not found
: command not found
: command not found
rename.sh: line 53: syntax error: unexpected end of file

debug with xtrace

What do you get when you trace through the script as below?

sh -x rename.sh

Excellent!

I just used it on thousands of files in many directories. Worked perfectly!

Thanks a lot

Just wanted to say thank you!

works great!

Regards!

Very Good, but

Thank you for the code but it seems to have a slight problem:

If a file (folder) contains a space, For example: "Good day", it will rename it to "good_day", instead of "good day"

Replacing space to underscore is a feature

Replacing spaces to underscore is a feature. However, if you need to keep the spaces, you can replace the translate code as below for all instances in the script:

From:

        y=$(echo "$x" | tr '[A-Z ]' '[a-z_]');

To:

        y=$(echo "$x" | tr '[A-Z]' '[a-z]');

Thank you

Hi,

I've tried several scripts to do this task, all of which failed. Yours worked a treat - thanks!

05/27/2008 lower case script is useful, thanks

I just used the 05/27/2008 script on a 40 GB of files which were all in upper case. It did take several hours to complete but every file and folder recursively were changed to lower case. Thank you sandip for the effort to patch and provide the script for everyone's use.

Thanks, and good to know it

Thanks, and good to know it works!!

Very Good Script

Hi,

Its working...Thanks Sandip.

Comment