Perl recursive search



Hello,

I would need your help. I have a Perl code that makes a folder structure, it reads a txt and creates the folders.

Now I have an issue I have to go through this folder structure and insert a file delete_me txt is yhe lowest level, and I have many lowest levels.
How do I search?



use strict;
use warnings;
use Cwd;
 
use File::Find;
 
my $search_pattern=$ARGV[0];
my $file_pattern  =$ARGV[1];
 
find(\&d, cwd);
 
sub d {
 
  my $file = $File::Find::name;
 
  $file =~ s,/,\\,g;
 
  return unless -f $file;
  return unless $file =~ /$file_pattern/;
 
  open F, $file or print "couldn't open $file\n" && return;
 
  while (<F>) {
    if (my ($found) = m/($search_pattern)/o) {
      print "found $found in $file\n";
      last;
    }
  }
 
  close F;
}

It is called as follows

perl searhc.pl (?i)sorting jane

The first argument is the regular expression. I use (?i) to indicate that I want to search case insensitive. The second argument specifies that only files should be considered that contain jane in their name.

The following script having a subroutine that calls itself and that works.

#!/usr/bin/perl
#LowestDirs02.pl
use strict;
use warnings;
my $startdir = '/home/david/Programming';
print_dirs_without_subdirs($startdir);
 
sub print_dirs_without_subdirs {
    #This subroutine calls itself for each subdirectory it finds.
    #If it finds no directories, it prints the name of the directory in which
    # it is looking, $dir.
    my $dir = $_[0];
    opendir DH, $dir or die "Failed to open $dir: $!";
    my @d;
    while ($_ = readdir(DH)) {
        next if $_ eq "." or $_ eq "..";
        my $fn = $dir . '/' . $_;
        if (-d $fn) {
            push @d, $fn;
        }
    }
    if (scalar @d == 0) { #If no directories found, $dir is lowest dir in this branch
        print "$dir\n";
        return;
    }
    foreach (@d) {
        print_dirs_without_subdirs($_); #Look for directories in directory
    }
}