PHP, 180 bytes
- first argument: path must have a trailing slash (or backslash)
- second argument: level defaults to
NULL
and will be interpreted as 0
by str_repeat
; will throw a warning if not provided
function d($p,$e){$s=opendir($p);echo$b=str_repeat("\t",$e++),$e?basename($p)."/":$p,"
";while($f=readdir($s))echo preg_match("#^\.#",$f)?"":is_dir($p.$f)?d("$p$f/",$e):"$b\t$f
";}
- displays hidden files and directories, but does not recurse hidden directories
add parentheses around is_dir(...)?d(...):"..."
to remove hidden entries from output (+2)
replace "#^\.#"
with #^\.+$#
to display/recurse hidden entries but skip dot entries (+2)
- may throw errors when directories are nested too deep. Insert
closedir($s);
before the final }
to fix (+13)
- will fail if a directory contains an entry with no name, prepend
false!==
to the while condition to fix (+8)
with glob, 182 bytes (probably 163 in future php)
function g($p,$e){echo$b=str_repeat("\t",$e),$e++?basename($p)."/":$p,"
";foreach(glob(preg_replace("#[*?[]#","[$1]",$p)."*",2)as$f)echo is_dir($f)?g($f,$e):"$b\t".basename($f)."
";}
- does not display or recurse hidden files/directories
2
stands for GLOB_MARK
, will append a slash to all directory names, as does ls -F
- the
preg_replace
escapes glob special characters
I could have abused preg_quote
for this (-19); but that would fail on Windows systems, as the backslash is the directory separator there.
- php may soon include a function glob_quote, which will allow the same golfing as
preg_quote
and work on all systems.
with iterators, 183 bytes
(well, not purely iterators: I used implicit SplFileInfo::__toString()
to golf $f->getBaseName()
and $f->isDir()
to the old PHP 4 functions.)
function i($p){echo"$p
";foreach($i=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($p),1)as$f)echo str_repeat("\t",1+$i->getDepth()),basename($f),is_dir($f)?"/":"","
";}
- no trailing slash required
- displays and recurses hidden entries (
ls -a
)
- insert
,4096
or ,FilesystemIterator::SKIP_DOTS
before ),1
to skip dot entries (+5) (ls -A
)
- flag
1
stands for RecursiveIteratorIterator::SELF_FIRST
.
)? \$\endgroup\$