29
Oct

Change Filenames to Lowercase

I enjoyed this little tip from linuxjournal.com on how to convert filenames to lowercase recursively. Here’s an example of the command:

for x in `ls path/to/directory`; do  if [ ! -f $x ]; then continue; fi; lc=`echo $x | tr '[A-Z]' '[a-z]'`; if [ $lc != $x ]; then mv -i $x $lc; fi; done

In my case, I wanted to find all files with a particular string in them and then convert to lowercase. This is what I used:

for x in `find path/to/directory -name "*theString*"`; do  if [ ! -f $x ]; then continue; fi; lc=`echo $x | tr '[A-Z]' '[a-z]'`; if [ $lc != $x ]; then mv -i $x $lc; fi; done

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:


There's 1 Comment So Far

  • dj
    October 29th, 2010 at 12:17 pm

    Here’s another way to do it. It’ll handle file names with spaces. It takes advantage of Bash new attributes (upper case and lower case – see ‘help declare’). Manipulate the find to get the exact files you want.

    declare -u u; while read -d $” f; do u=”$f”; mv “$f” “$u”; done < <(find . -maxdepth 1 -type f -print0)

Share your thoughts, leave a comment!