A robust, cross-platform shell function for quickly sharing a directory’s structure and previewing file contents.
Ideal for collaborating with others, sharing context with ChatGPT or other LLMs, or just auditing a codebase without manually sifting through files.
- Smart Exclusions
By default, skips common binary, dependency, build, and hidden files and directories (e.g.node_modules,.git,*.pyc), keeping output clean and focused. - Custom Excludes
Easily add your own exclusions (files or directories) via--exclude '*.md,*.env,sensitive_dir'—pattern syntax matches shell globs. - File Previews with Clipping
For each text or code file, prints up to the first 35 and last 5 lines (customizable). If you want the whole file, just add--show-all. - AI/LLM-Optimized Output
Makes it trivial to copy-paste directory trees and relevant code into ChatGPT or other AI assistants, without manual curation or exposing secrets/build artifacts. - Debug Mode
Use--debugto print exactly what would run—great for transparency or troubleshooting. - Cross-Platform
Works out-of-the-box on both macOS (zsh or bash) and Linux (bash, zsh, or most POSIX shells).
AI assistants are powerful, but don’t have access to your filesystem.
This tool lets you rapidly provide them with just enough context—not too much noise, not too little detail—for code review, bug diagnosis, or architecture advice.
Developers, security reviewers, educators, and anyone working with code in AI or human collaboration scenarios will find it invaluable.
- Shell:
Bash or Zsh (default shells on most Linux distros and macOS since Catalina). - Utilities:
Copy the function from detailed_tree.sh into your .bashrc, .zshrc, or any sourced shell file.
Or simply source the script directly:
source /path/to/detailed_tree.shThen restart your terminal, or source ~/.zshrc.
# Print tree with defaults (skips binary/dependency dirs)
detailed_tree
# Print tree for a specific directory
detailed_tree /path/to/dir
# Print tree, and preview ALL lines of each file
detailed_tree --show-all
# Add your own exclusions (comma-separated globs)
detailed_tree --exclude '*.md,*.json,private_dir'
# See exactly what will run (no file or tree output)
detailed_tree --debug --exclude '*.log'
# Combine options, order doesn't matter
detailed_tree /some/dir --exclude '*.txt' --show-all📂 Directory Tree:
.
├── app.js
├── package.json
└── src
└── index.js
📄 File Contents:
────────────────────────────────────────────
./app.js:
const express = require('express');
...
────────────────────────────────────────────
./src/index.js:
import something from './lib';
...
... (clipped, 50 lines total — showing first 35 and last 5)
💡 Use --show-all to view the full file: detailed_tree [directory] [--show-all]
- Uses
treefor pretty directory display and full-file listing (with exclusions). - Previews only text/code files (based on MIME type), not binaries.
- Excludes all common clutter by default; lets you add more as needed.
- Handles exclusions the way
treeexpects (multiple-Iarguments, not comma-separated). - Handles shell compatibility—works in both zsh (default macOS shell) and bash (default on most Linux systems).
Change these in the function if you want:
- clip_head: (default: 35) Number of lines from the start of each file to show.
- clip_tail: (default: 5) Number of lines from the end of each file to show.
- clip_total: (default: 40) If a file is ≤ this many lines, show the whole file.
MIT
Written and published by Benji Asperheim—adapted for shell, cross-platform, and AI/LLM workflows.