walidz walidz - 6 months ago 11
PHP Question

How to find readable folders on linux

i m trying to find all readable directories and subdurectories on Linux server using shell command ,
i have tried this command line:

find /home -maxdepth 1 -type d -perm -o=r


but this command line show me just the readable folders in (
/
) directories and not subdirectories too.

I want to do that using php or command line

thank you

Answer

"but this command line show me just the readable folders in ( / ) directories and not subdirectories too"

When you set -maxdepth 1 you're restricting the find command to /home only, remove it to allow find to search recursively.

find /home -type d -perm -o=r

If you need a native php solution, you can use this glob_recursive function and is_writable, i.e.:

<?php
function rglob($pattern, $flags = 0) {
    $files = glob($pattern, $flags);
    foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
        $files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
    }
    return $files;
}

$dirs = rglob('/home/*', GLOB_ONLYDIR);
foreach( $dirs as $dir){
    if(is_writable($dir)){
        echo "$dir is writable.\n";
    }
}
Comments