ibpap has asked for the wisdom of the Perl Monks concerning the following question:

Monks, not quite a Newbie, but I could use some advice.

Working on an app to scan user directories for various things using File::Find::find (e.g. parse /etc/passwd, and pass the home directory to an appropriate &wanted subroutine).

My problem is that some user home directories in /etc/passwd are symlinks (which I want to follow), but these accounts also contain multiple symlinks that I want to ignore. I know that File::Find::find's default behavior is to not follow symlinks but I can set {wanted => \&wanted ,follow=>1} to follow symlinks.

I have code in place that tests the /home/username directory, and if a symlink, adds follow=>1 to the File::Find::find call.

The general behavior I'm looking for is to follow any symlink to the user's login directory, but then switch to the default of not following symlinks while scanning the contents of the account.

I've tried setting $File::Find::follow=0 in &wanted if $File::Find::dir doesn't match the user login directory, but no luck at this point (could be my code).

Any sage advice about how to approach this problem? Thanks for any thoughts you may have and for your time reading this.First post.

Replies are listed 'Best First'.
Re: symlinks when using File::Find::find (/.)
by tye (Sage) on Oct 28, 2015 at 02:56 UTC

    Even if /foo/bar is a symlink, /foo/bar/. would not be. Just pass "/home/$user/." in and you shouldn't need to worry about the 'follow' option.

    - tye        

Re: symlinks when using File::Find::find
by Anonymous Monk on Oct 28, 2015 at 01:14 UTC

    step 1) obtain list of user directories while following symlinks to said directories

    step 2) scan these directories while not following symlinks

    Its two calls

    my( @userdirs ) = GetUserDirs(); ScanUserDirs( \@userdirs );

    Its two seperate calls, say

    use File::Find::Rule qw/ find rule /; my @userdirs = rule( 'directory' , maxdepth => 1, )->extras({ follow => 1 }) ->in( 'startdir' ); ...