LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find Files Named with 'text.rpt' in All Directories and Subdirectories using Perl (https://www.linuxquestions.org/questions/linux-newbie-8/find-files-named-with-text-rpt-in-all-directories-and-subdirectories-using-perl-4175527286/)

newbi2014 12-04-2014 02:21 AM

Find Files Named with 'text.rpt' in All Directories and Subdirectories using Perl
 
Hi,

I want to write a script in Perl that can create an array of filename named 'text.rpt'. The files are in subdirectories.

Below are the files' path:

/localdisk/user/aaa/aa1/a11/text.rpt
/localdisk/user/bbb/ba1/b11/text.rpt
/localdisk/user/ccc/ca1/c11/text.rpt

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);


jags1984 12-04-2014 03:18 AM

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

print "process_file: $directory : $filename : $filename_full \n\n";
$files_processed++;

}

newbi2014 12-04-2014 06:57 PM

Hi,

Thanks for your reply.

But I don't think the script display the filename "text.rpt"

Instead, it prints all the pathname of directories, subdirectories and file.

newbi2014 12-04-2014 07:43 PM

Updated

I've found a simpler script that will work.

Code:

use strict;
use warnings;

use File::Find;

my $localdir = '/localdisk';

find(
sub { print $File::Find::name, "\n" if /^text/ },
$localdir);

How do I create an array that contain the files?

Thank you.

newbi2014 12-04-2014 09:27 PM

It's okay.

I've figured out it already :)

jags1984 12-04-2014 11:50 PM

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 !

rnturn 12-07-2014 02:37 PM

Quote:

Originally Posted by newbi2014 (Post 5279766)
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.)

How did you actually implement this?

--
Rick

newbi2014 12-08-2014 05:32 PM

Quote:

Originally Posted by rnturn (Post 5281003)
How did you actually implement this?

I did not use File::File

Below is my code

Code:

# find files named "summary.rpt"
my $localdir = '/localdisk/user';

# create an array for the files
my @text = `find $localdir -name "text.rpt" `;


rnturn 12-08-2014 06:13 PM

Quote:

Below is my code

Code:

# find files named "summary.rpt"
my $localdir = '/localdisk/user';

# create an array for the files
my @text = `find $localdir -name "text.rpt" `;


Another popular way to do that. As is usually the case when using Perl, there's more than one way to do it.

--
Rick

newbi2014 12-08-2014 10:22 PM

How do I open each file without listing the files' location manually?


All times are GMT -5. The time now is 07:46 PM.