Skip to content

Instantly share code, notes, and snippets.

@painejake
Created February 22, 2011 11:33
Show Gist options
  • Save painejake/838544 to your computer and use it in GitHub Desktop.
Save painejake/838544 to your computer and use it in GitHub Desktop.
Rename files and folders to lowercase recursively
#!/bin/bash
# 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
@c4arl0s
Copy link

c4arl0s commented Aug 12, 2017

I use your code for rename all my directories and files into my macbook, according to my code the right way to appoint variable Y is y=echo "$x" | tr '[a-z]' '[A-Z]' this works for a bash shell in a macbook. Thanks.

@ryansmithevans
Copy link

Or, if you're a mad man and just wanna lowercase all files (not folders) in the current dir & sub dirs:

lower case all files in current dir & subdirs

for d in ./**/ ; do (cd "$d" && for x in ./*/ ; do (cd "$x" && for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "echo $f | tr "[:upper:]" "[:lower:]""; done); done); done

@sandeepksaini
Copy link

Try this one out much simple and easy one!

for f in **/*; do mv "$f" "${f:l}"; done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment