Skip to main content
Moru provides methods for common file management operations: checking existence, creating directories, renaming, and deleting.

Check Existence

from moru import Sandbox

sandbox = Sandbox.create()

# Check if file exists
if sandbox.files.exists("/home/user/config.json"):
    print("Config found")
else:
    print("Config not found, using defaults")

# Check directory
if sandbox.files.exists("/home/user/data"):
    print("Data directory exists")

Get File Info

Get detailed metadata about a file or directory:
info = sandbox.files.get_info("/home/user/script.py")

print(f"Name: {info.name}")
print(f"Path: {info.path}")
print(f"Type: {info.type}")
print(f"Size: {info.size} bytes")
print(f"Permissions: {info.permissions}")
print(f"Owner: {info.owner}:{info.group}")
print(f"Modified: {info.modified_time}")

if info.symlink_target:
    print(f"Symlink to: {info.symlink_target}")

Create Directory

# Create a directory
created = sandbox.files.make_dir("/home/user/new_folder")
print(f"Created: {created}")  # True if created, False if already exists

# Nested directories are created automatically
sandbox.files.make_dir("/home/user/deep/nested/folder")

Rename / Move

Rename a file or move it to a new location:
# Rename a file
info = sandbox.files.rename("/home/user/old.txt", "/home/user/new.txt")
print(f"Renamed to: {info.path}")

# Move to different directory
sandbox.files.rename("/home/user/file.txt", "/tmp/file.txt")

# Rename a directory
sandbox.files.rename("/home/user/old_folder", "/home/user/new_folder")

Delete Files and Directories

# Delete a file
sandbox.files.remove("/home/user/unwanted.txt")

# Delete an empty directory
sandbox.files.remove("/home/user/empty_folder")

# Delete directory with contents (use command)
sandbox.commands.run("rm -rf /home/user/folder_with_contents")

Copy Files

Use commands for copying:
# Copy a file
sandbox.commands.run("cp /home/user/source.txt /home/user/copy.txt")

# Copy a directory recursively
sandbox.commands.run("cp -r /home/user/source_dir /home/user/copy_dir")

Change Permissions

Use commands to modify permissions:
# Make file executable
sandbox.commands.run("chmod +x /home/user/script.sh")

# Set specific permissions
sandbox.commands.run("chmod 644 /home/user/config.txt")

# Change owner (as root)
sandbox.commands.run("chown alice:alice /home/alice/file.txt", user="root")
# Create symlink using command
sandbox.commands.run("ln -s /home/user/actual_file.txt /home/user/link.txt")

# Verify symlink
info = sandbox.files.get_info("/home/user/link.txt")
if info.symlink_target:
    print(f"Links to: {info.symlink_target}")

Error Handling

from moru.exceptions import NotFoundException

try:
    sandbox.files.remove("/nonexistent/file.txt")
except NotFoundException:
    print("File doesn't exist")

try:
    info = sandbox.files.get_info("/nonexistent/file.txt")
except NotFoundException:
    print("Cannot get info for nonexistent file")

User Context

All operations can be performed as a different user:
# Create directory as root
sandbox.files.make_dir("/etc/myapp", user="root")

# Remove file as specific user
sandbox.files.remove("/home/alice/temp.txt", user="alice")

Next Steps