June 11, 2008

Perl Script to Organize Scripts

This is a perl script for renaming and arranging your scripts(perl,bash,sed,awk and sh currently included) into corresponding folders.

You just run it in a folder where you store all your scripts.This script examines the she-bang(#!) line and determines which folder to move the program to and what extension to provide.

#!/usr/bin/perl -w
%ftypes= qw (perl pl bash sh sed ed awk aw sh sh);
opendir dirhndl,"." or die "Cannot open directory:$!";

while ($file= readdir(dirhndl)) {
if(-f $file){
open fh,$file;
$line= ;
if($line=~/^\#!.*?(\w+)\s+/) {
$dir= $1;
$ext= $ftypes{$dir}; #Identify and extract extension from hash
}
if($file=~/(\w+\.)\w*/) {$new=$1.$ext;}
else {$new=$file.".".$ext;}
mkdir $dir unless( -e $dir); #Make directory by type name
$newfile="./".$dir."/".$new;
rename "$file", "$newfile";
close(fh);
}
}
closedir(dirhndl);

No comments: