List files

2 snippets across 2 stacks — Bash & Linux, Python

SHBash & Linux

List Directory Contents

SH · Navigation & Files
syntax
ls [options] [directory]
example
ls -la /etc
ls -lhS ~/Downloads
ls -lt --color=auto
output
drwxr-xr-x  5 deploy staff  160 Mar 12 09:14 config
-rw-r--r--  1 deploy staff 2.4K Mar 11 18:30 app.js

Note -l for long format, -a includes hidden files (dotfiles), -h for human-readable sizes, -S sorts by size, -t sorts by modification time (newest first), -R lists recursively.

PYPython

Finding Files with Glob

PY · File I/O
syntax
Path(dir).glob(pattern)
Path(dir).rglob(pattern)
example
from pathlib import Path

# All Python files in current directory
for py_file in Path(".").glob("*.py"):
    print(py_file.name)

# Recursive search
all_json = list(Path(".").rglob("*.json"))
print(f"Found {len(all_json)} JSON files")

Note glob() searches one level. rglob() searches recursively through all subdirectories. Both return Path objects.