Do you guys have an idea on how to search or list down .exe files on the server I am currently using. (or maybe place it in an array)? I will use this command in my Perl program. Assuming that my program is also located on the said server. My OS is Linux - Ubuntu if that even matters, just in case. working in CLI here. =)
Perl to find every file under a specified directory that has a .exe
suffix:
#!/usr/bin/perl
use strict;
use File::Spec;
use IO::Handle;
die "Usage: $0 startdir\n"
unless scalar @ARGV == 1;
my $startdir = shift @ARGV;
my @stack;
sub process_file($) {
my $file = shift;
print $file
if $file =~ /\.exe$/io;
}
sub process_dir($) {
my $dir = shift;
my $dh = new IO::Handle;
opendir $dh, $dir or
die "Cannot open $dir: $!\n";
while(defined(my $cont = readdir($dh))) {
next
if $cont eq '.' || $cont eq '..';
my $fullpath = File::Spec->catfile($dir, $cont);
if(-d $fullpath) {
push @stack, $fullpath
if -r $fullpath;
} elsif(-f $fullpath) {
process_file($fullpath);
}
}
closedir($dh);
}
if(-f $startdir) {
process_file($startdir);
} elsif(-d $startdir) {
@stack = ($startdir);
while(scalar(@stack)) {
process_dir(shift(@stack));
}
} else {
die "$startdir is not a file or directory\n";
}
Have a look at File::Find.
Alternatively, if you can come up with a command line to the *nix file
command, you can use find2perl
to convert that command line to a Perl snippet.
As mentioned, It is not clear whether you want '*.exe' files, or executable files. You can use File::Find::Rule to find all executable files.
my @exe= File::Find::Rule->executable->in( '/'); # all executable files
my @exe= File::Find::Rule->name( '*.exe')->in( '/'); # all .exe files
If you are looking for executable files, you (the user running the script) need to be able to execute the file, so you probably need to run the script as root.
It might take a long time to run to.
If you are looking for .exe files, chances are that your disk is already indexed by locate
. So this would be much faster:
my @exe= `locate \.exe | grep '\.exe$'`
I'll probably be shot down for suggesting this, but you don't have to use modules for a simple task. For example:
#!/usr/bin/perl -w
@array = `find ~ -name '*.exe' -print`;
foreach (@array) {
print;
}
Of course, it will need to have some tweaking for your particular choice of starting directory (here, I used ~ for the home directory)
EDIT: Maybe I should have said until you get the modules installed