Skip to content

Instantly share code, notes, and snippets.

@robinkaty
Last active August 7, 2023 04:20
Show Gist options
  • Save robinkaty/c765ddd2eeb9e9db2d207f382b311c9f to your computer and use it in GitHub Desktop.
Save robinkaty/c765ddd2eeb9e9db2d207f382b311c9f to your computer and use it in GitHub Desktop.
SearchAllPath.pl
#!/usr/bin/perl
use strict;
use warnings;
sub find_file_in_path {
my ($filename) = @_;
my $count=0;
# Split the PATH environment variable into individual directories
my @dirs = split(':', $ENV{'PATH'});
# Iterate over each directory and check if the file exists
foreach my $dir (@dirs) {
my $full_path = "$dir/$filename";
if (-e $full_path) {
print "Found the file at: $full_path\n";
$count++;
#return;
}
}
# If the file is not found in any directory, print a message
if ($count == 0 ) {
print "File '$filename' not found in any directory in the PATH.\n";
}
}
# Check if the filename is provided as a command-line argument
if (@ARGV != 1) {
die "Usage: $0 <filename>\n";
}
my $filename_to_search = $ARGV[0];
find_file_in_path($filename_to_search);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment