Here's a simple bash script that will help in renaming files and folders with uppper-case to lower-case letters:
#!/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












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
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]');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!!
Post new comment