Find command in your perl script

If you want to search a particular file in a complex directory structure we have Unix command ‘find’ . I have been using this quite a sometime. But, I wanted to incorporate this feature in my perl script – this is bit challenging and didn’t find on any of the sites, don’t know if I missed any important sites.! you can point me if so.

Well , I still could get basic information on this in perl documentation. I am adducing important sites I referred.

http://www.perlmonks.org/?node_id=217166

http://perldoc.perl.org/File/Find.html

Just read about wanted function in the document above and more importantly these variables used.

  • $File::Find::dir is the current directory name,
  • $_ is the current filename within that directory
  • File::Find::name is the complete pathname to the file.

Just tweaked these sample scripts as per my requirement. I am sharing my script here with the explanation inline.

################################

use File::Find;  ## perl module . please use appropriate version of perl software

my $dir=”.”;  ## you can choose your directory

### find command to find .v and .libs in the directory – any search pattern is possible to use ..CHARM..:)
find(\&test_name,$dir);

### Subroutine executed on your each of file/directory parsed by above find command

sub test_name
{
#print $File::Find::name if -f;
#print “$File::Find::name \n”
#if($File::Find::name =~ m/.*\.v$/ ){
if($File::Find::name =~ m/.*\.v$/ | $File::Find::name =~ m/.*\.lib$/)  {
print “$File::Find::name \n” ;  ##prints file name

## prints size of the file in bytes

print $size{$File::Find::name}=-s if -f;
print “\n”;
}
}
Hope it helped .. Enjoy scripting..:) Comment if you are trying out something like this, would love to check it. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s