Rename All files to lower case

From LinuxServerTech

Jump to: navigation, search


I found this truly excellent article at http://stackoverflow.com/questions/152514/how-to-rename-all-folders-and-files-to-lowercase-on-linux which gives one of the best list of answers to how to rapidly rename all files in a directory tree to lower case. I quote directly from there.

Pure bash form

A concise version using "rename" command.

find my_root_dir -depth -exec rename 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;

This avoids problems with directories being renamed before files and trying to move files into non-existing directories (e.g. "A/A" into "a/a").

Or, a more verbose version without using "rename".

for SRC in `find my_root_dir -depth`
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

Perl Script

For us perl lovers: Using Larry Wall's filename fixer

$op = shift or die $help;
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
    $was = $_;
    eval $op;
    die $@ if $@;
    rename($was,$_) unless $was eq $_;
}

it's as simple as

find | fix 'tr/A-Z/a-z/'

(where fix is of course the script above)

Read the article for other options, and visit the web site for other problems with solutions.

Other Solutions:

ls | ./fix.pl 'tr/\x80-\xFF//d' # remove control characters
ls | ./fix.pl 'tr/+&#()/_/d' # get rid of some really not nice filename characters
ls | ./fix.pl 's/_+/_/g' # replace duplicate underscores with one single underscore