1829586 Members
2603 Online
109992 Solutions
New Discussion

File::find question

 
Ridzuan Zakaria
Frequent Advisor

File::find question

Hi,

I would like to use File::find to traverse a directory tree find files but would like to skip some of suddirectories under that directory.

How do I do this using File::find?

Thanks.
quest for perfections
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

Re: File::find question

Hi:

A Perl question...

First, consider:

If you wanted to find all files in '/var' whose name ended in ".log" but you wanted to *skip* any within '/var/adm' you could do:

# find /var -type f ! -path "/var/adm/*" -prune -name "*.log" -print

NOW, using the above command, with *one modification*, do:

# find2perl /var -type f ! -name "/var/adm/*" -prune -name "*.log" -print

NOTE that the '-path' has been changed to '-name' and then the whole argument passed to 'find2perl'.

The generated 'wanted' subroutine should be what you seek.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: File::find question

Hi (again):

My apologies, the generated code doesn't quite do what we want. Given my original shell command:

# find /var -type f ! -path "/var/adm/*" -prune -name "*.log" -print

...a 'wanted' subroutine like this should match:

sub wanted {
my ($dev,$ino,$mode,$nlink,$uid,$gid);

(($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
-f _ &&
$File::Find::name !~ /^\/var\/adm\//s &&
/^.*\.log\z/s &&
print("$name\n");
}

Regards!

...JRF...

H.Merijn Brand (procura
Honored Contributor

Re: File::find question

Check on $File::Find::dir
If you want to check on the target of symbolic links too, use Cwd's realpath () or perl's readlink ().

# perl -MFile::Find -le'find(sub{$File::Find::dir=~m{pattern-for-dirs-to-skip} and return;print$File::Find::name},@ARGV)' /var /tmp

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ridzuan Zakaria
Frequent Advisor

Re: File::find question

Thanks everyone for your help.
quest for perfections