Skip to main content
Use sandbox.files.list() to get the contents of a directory.

Basic Usage

from moru import Sandbox

sandbox = Sandbox.create()

# List directory contents
entries = sandbox.files.list("/home/user")

for entry in entries:
    print(f"{entry.name} ({entry.type}): {entry.size} bytes")

Entry Information

Each entry contains detailed metadata:
PropertyTypeDescription
namestringFile or directory name
pathstringFull path
type"file" or "dir"Entry type
sizenumberSize in bytes (0 for directories)
modenumberUnix permission mode
permissionsstringPermission string (e.g., -rw-r--r--)
ownerstringOwner username
groupstringGroup name
modified_timedatetimeLast modification time
symlink_targetstring?Target path if symlink

Recursive Listing

Use the depth parameter to list nested directories:
# List with depth 1 (default) - immediate children only
entries = sandbox.files.list("/home/user", depth=1)

# List with depth 2 - includes grandchildren
entries = sandbox.files.list("/home/user", depth=2)

# Deep recursive listing
entries = sandbox.files.list("/home/user", depth=10)

for entry in entries:
    print(entry.path)

Filter by Type

Filter entries by file type:
entries = sandbox.files.list("/home/user")

# Get only files
files = [e for e in entries if e.type == "file"]

# Get only directories
dirs = [e for e in entries if e.type == "dir"]

print(f"Files: {len(files)}, Directories: {len(dirs)}")

Tree-like Display

Build a tree structure from recursive listing:
def print_tree(entries, indent=0):
    # Group by parent directory
    by_parent = {}
    for entry in entries:
        parent = "/".join(entry.path.split("/")[:-1])
        by_parent.setdefault(parent, []).append(entry)

    def print_dir(path, level):
        children = by_parent.get(path, [])
        for child in sorted(children, key=lambda e: e.name):
            prefix = "  " * level
            icon = "📁" if child.type == "dir" else "📄"
            print(f"{prefix}{icon} {child.name}")
            if child.type == "dir":
                print_dir(child.path, level + 1)

    print_dir("/home/user", 0)

entries = sandbox.files.list("/home/user", depth=5)
print_tree(entries)

List as Different User

# List as root to see all files
entries = sandbox.files.list("/root", user="root")

# List as specific user
entries = sandbox.files.list("/home/alice", user="alice")

Find Files by Pattern

Combine with commands for pattern matching:
# Find all Python files
result = sandbox.commands.run("find /home/user -name '*.py' -type f")
python_files = result.stdout.strip().split("\n")

# Find files modified in last hour
result = sandbox.commands.run("find /home/user -mmin -60 -type f")
recent_files = result.stdout.strip().split("\n")

Error Handling

from moru.exceptions import NotFoundException

try:
    entries = sandbox.files.list("/nonexistent/path")
except NotFoundException:
    print("Directory not found")

Options Reference

OptionTypeDefaultDescription
depthnumber1Recursion depth
userstring"user"User context
request_timeout / requestTimeoutMsnumber-Operation timeout

Next Steps