in reply to how to find the file from the subdirectories of a directory?
$ ./file-find-recursion-depth-subdirs-ext . 6 z.xml /home/zentara/1down/1/1/a/z.xml /home/zentara/1down/1/1/a
#!/usr/bin/perl # linux only for counting depth, you can adjust for Windows use warnings; use strict; use File::Find; use File::Spec; if (@ARGV < 2){print "Usage: $0 dir depth\n";exit} my ($path, $depth)= @ARGV; # scriptname . 6 would descend 6 layers into current dir's full path # so depth is often gretaer than you think :-) my $abs_path = File::Spec->rel2abs($path); #in case you enter . for di +r my $m = ($abs_path) =~ tr!/!!; #count slashes in top path my @files; my @dirs; find (\&found,$abs_path); print "@files\n"; print "@dirs\n"; exit; sub found{ my $n = ($File::Find::name) =~ tr!/!!; #count slashes in file return $File::Find::prune = 1 if $n > ($m + $depth); # do stuff here. if( -f && $File::Find::name=~/\.xml$/){ push @files, $_; #name only push @files, $File::Find::name; #name with full path push @dirs,$File::Find::dir; # dir full path } }
|
|---|