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
- sandip's blog
- Login or register to post comments
Comments
I think what this work too.
find * -depth -execdir rename 'y/A-Z/a-z/' {} \;
Daniel
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
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 uses
mkdir
+rmdir
This seems enough :
# Filename: mv_lcase.sh sp; &nb sp; from the current directory sp; &nb sp; Destination = y
# Description: Renames files and folders to lowercase recursively
# &nb
# Variables: Source = x
# &nb
#
# 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
This will not find folders within folders as it would have been moved already.
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.
Just wanted to thank you for that useful script. Saved me a lot of time.
Cheers!
@$@$ up your files and tree if there are spaces in file...thanks for the out dated 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 just renamed 103 files in less than 3 seconds. It's greatly appreciated.
It fails to convert files when they're spaced (like on "The House.jpg" it tries to stat The and not the hole file).
#!/bin/bash
sp; &nb sp; from the current directory sp; &nb sp; Destination = y
p; # Translate Caps to Small letters p; y=$(echo "$x" | tr '[A-Z ]' '[a-z_]');
p; # create directory if it does not exit p; if [ ! -d "$y" ]; then p; &nbs p; &nbs p; mkdir -p "$y"; p; fi
p; # check if the source and destination is the same p; if [ "$x" != "$y" ]; then
p; &nbs p; &nbs p; # move directory files before deleting p; &nbs p; &nbs p; ls -A "$x" | while read i p; &nbs p; &nbs p; do p; &nbs p; &nbs p; mv "$x"/"$i" "$y"; p; &nbs p; &nbs p; done p; &nbs p; &nbs p; rmdir "$x";
p; fi
p; # Translate Caps to Small letters p; y=$(echo "$x" | tr '[A-Z ]' '[a-z_]'); p; if [ "$x" != "$y" ]; then p; &nbs p; &nbs p; mv "$x" "$y"; p; fi
#
# Filename: rename.sh
# Description: Renames files and folders to lowercase recursively
# &nb
# Variables: Source = x
# &nb
#
# 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
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
done
#
# Rename all files
#
find * -type f | while read x ;
do
&nbs
&nbs
&nbs
&nbs
&nbs
done
exit 0
: command not found
: command not found
: command not found
rename.sh: line 53: syntax error: unexpected end of file
What do you get when you trace through the script as below?
sh -x rename.sh
I just used it on thousands of files in many directories. Worked perfectly!
Just wanted to say thank you!
works great!
Regards!
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 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:
To:
Hi,
I've tried several scripts to do this task, all of which failed. Yours worked a treat - 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 works!!
Hi,
Its working...Thanks Sandip.