DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Convert Extensions Of Files To Lowercase Using Perl

06.21.2007
| 7541 views |
  • submit to reddit
        1. Searches for files having uppercase extensions (can be mixed with numbers and can be multipart eg: TAR.BZ2 ). 
   It does not detect files whose extensions contain both upper and lower cases.  This is intentional, as if you have part of extension lowercase, you probably intentionally left the other part upper case. Changing this behaviour is trivial. Replace that complex (?:[A-Z]*[0-9]*\.*) with a . (a dot). 

2. It then converts them to lowercase.

#!/usr/bin/perl
$files=`ls`;
@files=split(/\n/,$files);
foreach (@files) {
    if(/(.*)\.((?:[A-Z]*[0-9]*\.*)+)$/) {
        $name=$1."."."\L$2\E";
        system("mv $_ $name")
    }
}
Looks quite ugly, but this is what I could do with the Perl I know, and more importantly, it works (at least in my case)!