Published in: Bash
URL: http://www.linuxjournal.com/content/convert-filenames-lowercase
Line 6 starts a loop (which ends with line 15). The ls command returns a list of filenames which are sequentially assigned to the shell variable x. The if test (lines 8 through 10) checks to see if the current filename is that of a plain file. If not, the remainder of the statements in the current loop iteration are skipped.
If line 11 is to be executed we know that we have an ordinary file. Using tr we convert the filename to lowercase and assign the new name to the shell variable lc. Line 12 then checks to see if the lowercase version of the name differs from the original. If it does, line 13 is executed to change the name of the original file to the new lowercase name. The -i option causes the mv to prompt for confirmation if executing the command would overwrite an existing filename. __________________________
#!/bin/sh # lowerit # convert all file names in the current directory to lower case # only operates on plain files--does not change the name of directories # will ask for verification before overwriting an existing file for x in `ls` 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
Comments
Subscribe to comments
You need to login to post a comment.

Your script does not handle spaces and other stuff in filenames correctly.
lsis a really ugly novice mistake. Heres a much better version, it removes the directory check and interactive crap also.!/bin/bash -xe
convert all files in the current directory to lower case
for x in * do lc=$(echo "$x" | tr "[A-Z]" "[a-z]") if [[ $lc != $x ]]; then mv "$x" "$lc" fi done
ok, the automatic markup/down crap ruined my comment, and there is no obvious way to post code in a comment. This site = fail.