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

I am trying to find all files and directories that I can not read or write on my Unix server. Please advise how I can do this? I need to know which directories and files (as a root user) that I can not write and/or read and was hoping the below was a start but need some help on it:
use strict; use File::Find; my $dir = '/webdir/pathhere; find(\&ReadPerm, $dir); sub ReadPerm { if ($_ ! -w) { print "$File::Find::name\n"; } }

Replies are listed 'Best First'.
Re: Checking permissions on server
by DrHyde (Prior) on Jul 30, 2003 at 12:53 UTC
    If you're root, then if we ignore platform-specific additions (like setting a file immutable, see chattr(1) on Linux for example) you can read or write any file.

    I would like to take the opportunity to plug my module File::Find::Rule::Permissions :-) It makes it pretty easy to find files or directories based on whether a given user (not necessarily the current user) can read or write them.

Re: Checking permissions on server
by wirrwarr (Monk) on Jul 30, 2003 at 13:49 UTC
    The line "if ($_ ! -w)" is wrong.
    It should be "if (! -w $_)".
    And you forgot to close the string 'webdir/pathhere
    The following script works on my system:
    use strict; 
    use File::Find; 
    my $dir = '/webdir/pathhere';
    find(\&ReadPerm, $dir); 
    sub ReadPerm { 
      if (! -w $_) { 
        print "$File::Find::name\n"; 
      }
    }
    
      Thanks, this will let me know if I cant write AND/OR read any file or directory?
      use strict; use File::Find; my $dir = '/webdir/pathhere'; find(\&ReadPerm, $dir); sub ReadPerm { if (! -w $_) { print "$File::Find::name\n"; } }

        If you want to find out both read and write, then you'll need to do 2 tests: one for read, one for write. Simply do a test with '-r' as well, such as the following (not tested, may/may not work as-is):

        #!/usr/bin/perl -w $|++; use strict; use File::Find; my $dir = '/path/to/dir'; find(\&wanted, '/path/to/dir'); sub wanted { if (not -w $_ && not -r $_) { print "Cannot read or write $File::Find::name.\n"; } elsif (not -w) { print "Cannot write $File::Find::name.\n"; } elsif (not -r) { print "Cannot read $File::Find::name.\n"; } }


        If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

Re: Checking permissions on server
by sgifford (Prior) on Jul 31, 2003 at 03:35 UTC

    As root you should be able to read and write all files. The exception is remote files shared over a protocol like NFS, but those will most likely appear to be readable/writable until you actually try.

    For example:

    # id -u
    0
    # ls -ld noperms
    ----------    1 sgifford sgifford        0 Jul 30 23:37 noperms
    # perl -e 'print STDOUT (-w "noperms" ? "" : "un") . "writable\n";'
    writable
    

    What sort of problem are you running into that requires you to do this?