Tuesday, 25 August 2009

Perl One Liner: Lower case file names

If you want to make file names lower case this will make it so.

find . -type f -maxdepth 1| while read seq;do mv ${seq} $(echo ${seq} |perl -ne 'tr/A-Z/a-z/;print'); done

or with the easier ls

ls | while read seq;do mv ${seq} $(echo ${seq} |perl -ne 'tr/A-Z/a-z/;print'); done

You can add, for example, -name "*.txt" to the find part to only rename files that end in .txt. Also changing the type to -type d then it will work on directories.

You could also move the file within perl rather than in the shell, using File::Copy

ls | perl -ne 'use File::Copy;chomp;$old=$_;tr/A-Z/a-z/;move($old,$_)'
You could replace the ls with a find statement as well for more control.

No comments:

Post a Comment