我正在使用一个我不熟悉的linux机器,所以我想从它的文件夹结构中获得一个txt打印。我记得写了一个脚本,在php中做了类似的事情,但找不到它。我正在寻找任何以下的,可以帮助我完成这个任务:使用bash或php获取文件夹层次结构
- bash脚本
- 现有的Linux命令行
- PHP脚本
我正在使用一个我不熟悉的linux机器,所以我想从它的文件夹结构中获得一个txt打印。我记得写了一个脚本,在php中做了类似的事情,但找不到它。我正在寻找任何以下的,可以帮助我完成这个任务:使用bash或php获取文件夹层次结构
find . -type d > dirstructure.txt
然而,在一个典型的Linux机器上,我宁愿不从目录根目录运行。如果你这样做,并得到一些权限错误,你可以发送错误到/dev/null
find . -type d > dirstructure.txt 2> /dev/null
如果你不想在目录结构中过深,添加-maxdepth#也可能很有用。要只显示两个级别,它将是:'find。 -maxdepth 2 -type d' – Brett
试试这个吗?
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
采取快速和肮脏的:
class Crawl_directory
{
public $exclude = array();
public $paths = array();
public $tree = FALSE;
public $tree_str = FALSE;
public function __construct($path, $exclude = array())
{
if (!$path || !is_dir($path))
return FALSE;
$this->exclude = array_merge(array(), $exclude);
$this->tree = $this->crawl($path);
$this->tree_str = $this->create_tree($this->tree);
}
public function crawl($path)
{
$arr = array();
$items = scandir($path);
$this->paths[] = $path;
foreach ($items as $k => $v) {
if (!in_array($v, $this->exclude) && $v != '.' && $v != '..') {
if (is_dir($path.'/'.$v)) {
$arr[$v] = $this->crawl($path.'/'.$v);
} else {
$arr[$v] = '';
}
}
}
return $arr;
}
function create_tree($arr)
{
$out = '<ul>'."\n";
foreach ($arr as $k => $v) {
$out .= '<li class="'.((is_array($v)) ? 'folder' : 'file').'">'.$k.'</li>'."\n";
if (is_array($v)) {
$out .= $this->create_tree($v);
}
}
$out .= '</ul>'."\n";
return $out;
}
function get_tree()
{
return $this->tree;
}
function print_tree()
{
echo $this->tree_str;
}
}
你尝试了'find'命令? – Mat