[SOLVED] Find Files Named with 'text.rpt' in All Directories and Subdirectories using Perl
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Below is the code that I tried but I don't think it is right because it set the directory to tmp. I don't want the script to specify which subdirectory it should look. I want the script to look into all directories and subdirectories because the summary files are tool generated.
Code:
#!/usr/bin/env perl
use warnings;
use strict;
use File::Find;
# for example let location be tmp
my $location="tmp";
sub find_txt {
my $F = $File::Find::name;
if ($F =~ /^text\.rpt/ ) {
print "$F\n";
}
}
find({ wanted => \&find_txt, no_chdir=>1}, $location);
This code will search for the file test.rpt from the /home directory to all subdirectory... Hope this is what you were looking for
Quote:
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use File::Path;
my $directory_list="/home";
my @inaccessible;
my $files_processed = 0; # counts the files processed by process_file()
# Supress Permission denied warnings from the File::find module
no warnings 'File::Find';
find( { wanted => \&process_file,
preprocess => \&preprocess, # ignore inaccessible directories
follow => 1,
follow_skip => 2, # to ignore any duplicate files and directories but to proceed normally otherwise
no_chdir => 1, # Does not chdir() to each directory as it recurses
}, $directory_list);
sub preprocess {
grep {
if ( -d $_ and !-r _ ) {
push @inaccessible, "$File::Find::dir/$_";
0; # don't pass on inaccessible dir
} else {
1;
}
} @_;
}
sub process_file {
my $filename = $_; # filename without directory
my $filename_full = $File::Find::name; # filename with directory
my $directory = $File::Find::dir; # directory only
return unless -f; # Must be a file
return unless text.rpt; # The filename
Good work, Hope you are not considering the cases i considered.
What if the directory is a symbolic link ? will your find will traverse in those directory ? If you will not encounter symbolic links then the solution seems Okay !
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,818
Rep:
Quote:
Originally Posted by newbi2014
It's okay.
I've figured out it already
If you were able to find the files at the shell prompt using "find", you could simply use that "find" command in an "open()" statement and read each returned filepath from a pipe. Then either push each returned line into an array or add it to a hash. (Seems simpler than using an external package but that's probably just me.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.