Created
May 10, 2026 20:30
-
-
Save Keboo/44d487e700ad037a07ff06b7b6d516a7 to your computer and use it in GitHub Desktop.
Dir.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| root="${1:-.}" | |
| find "$root" -type d | while read -r dir; do | |
| # Strip the root prefix | |
| rel="${dir#$root}" | |
| # Count slashes to determine depth | |
| depth=$(grep -o "/" <<< "$rel" | wc -l) | |
| # Build indentation | |
| indent="" | |
| for ((i=1; i<depth; i++)); do | |
| indent+="| " | |
| done | |
| # Print line | |
| echo "${indent}|_ $(basename "$dir")" | |
| done |
Author
Author
#!/usr/bin/env bash
root="${1:-.}"
outfile="$HOME/Desktop/directory_tree.txt"
: > "$outfile" # create/empty the output file
Find all non-hidden directories
find "$root" -type d ! -path "/." | while read -r dir; do
rel="${dir#$root}"
depth=$(grep -o "/" <<< "$rel" | wc -l)
indent=""
for ((i=1; i<depth; i++)); do
indent+="| "
done
echo "${indent}|_ $(basename "$dir")" >> "$outfile"
done
macOS dialog
osascript -e 'display dialog "Directory tree has been written to your Desktop." buttons {"OK"} with icon note'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#!/usr/bin/env bash
root="${1:-.}"
outfile="$HOME/Desktop/directory_tree.txt"
Empty or create the output file
: > "$outfile"
find "$root" -type d | while read -r dir; do
rel="${dir#$root}"
depth=$(grep -o "/" <<< "$rel" | wc -l)
done
echo "Directory tree written to: $outfile"