Changing files to lower case with Bash and tr

I’m sure there are a number of ways to change a directory of files to lowercase including the mmv utility for ‘wildcard’ renaming and copying. However, this simple one-liner is likely to work on a wider range of systems without installing additional programs since it only requires bash and tr:

for file in `ls`; do mv -i $file `echo $file | tr '[A-Z]' '[a-z]'`; done;

This will match any lowercase character in a filename in the current directory and convert it to lowercase. We can convert the other way by changing the arguments to tr. Variants can be created by filtering the results from ls, or changing the regular expression strings for tr. For example, to change the first letter of every directory in the current directory to upper case do:

for file in `ls -l | grep ^d | awk '{print $8}'`;
do mv -i $file `echo ${file:0:1} | tr '[a-z]' '[A-Z]'`${file:1}; done;

Merry Christmas!

Related Entries

About

I work at Birmingham Conservatoire as senior researcher and software development manager for the Integra Project. I live with my wife and three beautiful children in Birmingham, UK.» More...

Tag Cloud

Projects

-->
Close