如何在 linux 上找到可读文件夹
How to find readable folders on linux
我正在尝试使用 shell 命令在 Linux 服务器上查找所有可读目录和子目录,
我试过这个命令行:
find /home -maxdepth 1 -type d -perm -o=r
但是这个命令行只显示了 (/
) 目录中的可读文件夹,而不是子目录。
我想使用 php 或命令行
谢谢
"but this command line show me just the readable folders in ( / )
directories and not subdirectories too"
当您设置 -maxdepth 1
时,您将 find 命令限制为仅 /home
,删除它 以允许 find 搜索 递归.
find /home -type d -perm -o=r
如果您需要原生 php
解决方案,您可以使用此 glob_recursive
函数和 is_writable
,即:
<?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";
}
}
我正在尝试使用 shell 命令在 Linux 服务器上查找所有可读目录和子目录, 我试过这个命令行:
find /home -maxdepth 1 -type d -perm -o=r
但是这个命令行只显示了 (/
) 目录中的可读文件夹,而不是子目录。
我想使用 php 或命令行
谢谢
"but this command line show me just the readable folders in ( / ) directories and not subdirectories too"
当您设置 -maxdepth 1
时,您将 find 命令限制为仅 /home
,删除它 以允许 find 搜索 递归.
find /home -type d -perm -o=r
如果您需要原生 php
解决方案,您可以使用此 glob_recursive
函数和 is_writable
,即:
<?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";
}
}