LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-10-2009, 06:32 AM   #1
Asy
LQ Newbie
 
Registered: May 2008
Location: The Netherlands
Distribution: Ubuntu
Posts: 25

Rep: Reputation: 16
[perl] find2perl dynamic search for files or directories


Hi,

I'm new to Perl programming. I want to create a script searching for files or directories in a filesystem.

In the kornshell it is one find command, and is it possible to make the search dynamic with variables.

I want to create the same in Perl and used for the first step the find2perl to create the framework.
In this framework I placed some variable with are working fine only th e variable to search for filetype or directory type I didn't get solved.

Code till now:

Code:
my filename=$1
my saveday=$2
my filetype=$3

sub wanted {
my ($dev,$ino,$mode,$nlink,$uid,$gid);
   /$filename/s &&
   (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
   -f _ && # this line need dynamic!
   (int(-M _) < $saveday) &&
   print "$_\n";
}

File::Find::find({wanted => \&wanted}, '/');
Did any one have a suggestion?
 
Old 07-11-2009, 06:56 AM   #2
thinknix
Member
 
Registered: Nov 2008
Distribution: Lots!
Posts: 178

Rep: Reputation: 58
What do you mean by filetype - extension, content, or file vs. directory?
 
Old 07-12-2009, 02:15 PM   #3
Asy
LQ Newbie
 
Registered: May 2008
Location: The Netherlands
Distribution: Ubuntu
Posts: 25

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by slugmax View Post
What do you mean by filetype - extension, content, or file vs. directory?
Quote:
Originally Posted by asy
I want to create a script searching for files or directories in a filesystem.
I want to search for files or directories. In the $filename you can specify a search string with perl pattern matching.
 
Old 07-13-2009, 08:25 AM   #4
thinknix
Member
 
Registered: Nov 2008
Distribution: Lots!
Posts: 178

Rep: Reputation: 58
Your description doesn't say what values $filetype can hold, so I'll guess it will just be one value meaning 'files' and one meaning 'directories'. An example showing how your script will be run with some sample arguments would be useful if I still don't understand.

An easy way to implement this would be to search files by default, unless '-d' was specified. That keeps the conditional to one line. If not, you'll need a larger conditional block, which would probably be in a small subroutine that checks the value of filetest. It would need to have the value of the special filehandle '_' passed into it as a parameter.

Here is the easy way that assumes you want files if $filetype is not '-d':

Code:
...
(($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
   ( ( $filetype eq '-d' ) ? -d _ : -f _ ) && # Search for regular files by default
   (int(-M _) < $saveday) &&
...
You might also look into Getopt::Std (http://perldoc.perl.org/Getopt/Std.html) for argument processing (assuming this isn't part of some larger program).
 
Old 07-13-2009, 06:10 PM   #5
Asy
LQ Newbie
 
Registered: May 2008
Location: The Netherlands
Distribution: Ubuntu
Posts: 25

Original Poster
Rep: Reputation: 16
hi slugmax,

you are right, the filetype can be directory or file.
the hole script is suggest to run on in windows machine while our bosses has decide to kick out all our fine Unix servers.

Code:
( ( $filetype eq '-d' ) ? -d _ : -f _ )
Is for me very short notation for the if then else statement. But in this case it should do the trick.
I will test it tomorrow at work.
 
  


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
Search repo for files/directories by name with wget gnashley Programming 1 12-30-2007 05:13 AM
Search for files only in directories you have sufficient permissions tepez Linux - Software 1 04-08-2007 10:07 AM
Perl: Search and replace directories within text files Erhnam Programming 2 03-07-2006 04:07 AM
How to "Search & Replace" in html files using Perl? rebel Red Hat 8 04-09-2005 12:58 PM
Map Perl to directories and files yesyes125 Linux - Software 0 05-10-2002 03:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:03 AM.

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