よく知らない Linux マシンを使用しているため、そのフォルダー構造から txt を出力したいと考えています。PHPで似たようなことをするスクリプトを書いたのを覚えていますが、見つかりません。このタスクに役立つ可能性のある次のいずれかを探しています。
- バッシュスクリプト
- 既存の Linux コマンドライン
- phpスクリプト
find . -type d > dirstructure.txt
ただし、一般的なLinuxボックスでは、これをディレクトリルートから実行したくありません。これを行っていくつかの許可エラーが発生した場合は、エラーを次の宛先に送信できます。/dev/null
find . -type d > dirstructure.txt 2> /dev/null
クイックアンドダーティ:
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;
}
}
これを試して?
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'