Created
February 22, 2011 11:33
-
-
Save painejake/838544 to your computer and use it in GitHub Desktop.
Rename files and folders to lowercase recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Rename all directories. This will need to be done first. | |
# Process each directorys 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 |
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
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
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.