LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-04-2014, 02:21 AM   #1
newbi2014
LQ Newbie
 
Registered: Nov 2014
Posts: 15

Rep: Reputation: Disabled
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);

Last edited by newbi2014; 12-04-2014 at 02:24 AM.
 
Old 12-04-2014, 03:18 AM   #2
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
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++;

}

Last edited by jags1984; 12-04-2014 at 03:25 AM.
 
1 members found this post helpful.
Old 12-04-2014, 06:57 PM   #3
newbi2014
LQ Newbie
 
Registered: Nov 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-04-2014, 07:43 PM   #4
newbi2014
LQ Newbie
 
Registered: Nov 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
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.

Last edited by newbi2014; 12-04-2014 at 08:04 PM.
 
Old 12-04-2014, 09:27 PM   #5
newbi2014
LQ Newbie
 
Registered: Nov 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
It's okay.

I've figured out it already
 
Old 12-04-2014, 11:50 PM   #6
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
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 !

Last edited by jags1984; 12-04-2014 at 11:51 PM.
 
Old 12-07-2014, 02:37 PM   #7
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by newbi2014 View Post
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
 
Old 12-08-2014, 05:32 PM   #8
newbi2014
LQ Newbie
 
Registered: Nov 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rnturn View Post
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" `;
 
Old 12-08-2014, 06:13 PM   #9
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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
 
Old 12-08-2014, 10:22 PM   #10
newbi2014
LQ Newbie
 
Registered: Nov 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
How do I open each file without listing the files' location manually?

Last edited by newbi2014; 12-08-2014 at 10:25 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find directories that have two subdirectories in them-- PLEASE help pierceogden Programming 6 04-06-2012 08:47 PM
How can I find all directories with subdirectories with find? Ujjain Linux - Software 1 06-11-2011 05:57 PM
Sort Many Files in Subdirectories into Directories by File Type garyozzy Programming 5 02-15-2011 03:18 PM
Copying files and sub-directories of a directory except the directories named ".abc" sri1025 Linux - General 2 08-24-2010 08:53 AM
[SOLVED] command to find a specific word in directories and subdirectories? siranjeevi Linux - Newbie 4 06-09-2010 09:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration