print tree3

!/usr/bin/env bash

Unicode tree for Linux, directories first, files second, human-readable sizes

print_tree() {
local dir=”$1″
local prefix=”${2:-}”
local maxdepth=”${3:-0}”
local depth=”${4:-1}”

[[ ! -d “$dir” ]…


This content originally appeared on DEV Community and was authored by Query Filter

!/usr/bin/env bash

Unicode tree for Linux, directories first, files second, human-readable sizes

print_tree() {
local dir="$1"
local prefix="${2:-}"
local maxdepth="${3:-0}"
local depth="${4:-1}"

[[ ! -d "$dir" ]] && return
[[ $maxdepth -ne 0 && $depth -gt $maxdepth ]] && return

echo "${prefix}${dir##*/}/"

# Directories first
local d
local dirs=()
shopt -s nullglob
for d in "$dir"/*; do
    [[ -d "$d" ]] && dirs+=("$d")
done
IFS=$'\n' dirs=($(printf "%s\n" "${dirs[@]}" | sort))
for i in "${!dirs[@]}"; do
    d="${dirs[i]}"
    local branch="├──"
    [[ $i -eq $(( ${#dirs[@]} - 1 )) ]] && branch="└──"
    local new_prefix="$prefix"
    [[ $i -eq $(( ${#dirs[@]} - 1 )) ]] && new_prefix+="    " || new_prefix+="│   "
    echo "${prefix}${branch} ${d##*/}/"
    print_tree "$d" "$new_prefix" "$maxdepth" $((depth+1))
done

# Files
local f
local files=()
for f in "$dir"/*; do
    [[ -f "$f" ]] && files+=("$f")
done
IFS=$'\n' files=($(printf "%s\n" "${files[@]}" | sort))
local file_count=0
for i in "${!files[@]}"; do
    f="${files[i]}"
    local branch="├──"
    [[ $i -eq $(( ${#files[@]} - 1 )) ]] && branch="└──"
    local size=$(stat -c "%s" "$f" 2>/dev/null || echo 0)
    local hr_size
    if [[ "$size" -lt 1024 ]]; then hr_size="${size}B"
    elif [[ "$size" -lt 1048576 ]]; then hr_size="$((size/1024))K"
    elif [[ "$size" -lt 1073741824 ]]; then hr_size="$((size/1024/1024))M"
    else hr_size="$((size/1024/1024/1024))G"; fi
    echo "${prefix}${branch} ${f##*/} [${hr_size}]"
    ((file_count++))
done

# Total files per directory
[[ $file_count -gt 0 ]] && echo "${prefix}└── Total files: $file_count"

shopt -u nullglob

}

Usage: print_tree

print_tree . "" 0


This content originally appeared on DEV Community and was authored by Query Filter


Print Share Comment Cite Upload Translate Updates
APA

Query Filter | Sciencx (2025-10-23T20:54:11+00:00) print tree3. Retrieved from https://www.scien.cx/2025/10/23/print-tree3/

MLA
" » print tree3." Query Filter | Sciencx - Thursday October 23, 2025, https://www.scien.cx/2025/10/23/print-tree3/
HARVARD
Query Filter | Sciencx Thursday October 23, 2025 » print tree3., viewed ,<https://www.scien.cx/2025/10/23/print-tree3/>
VANCOUVER
Query Filter | Sciencx - » print tree3. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/23/print-tree3/
CHICAGO
" » print tree3." Query Filter | Sciencx - Accessed . https://www.scien.cx/2025/10/23/print-tree3/
IEEE
" » print tree3." Query Filter | Sciencx [Online]. Available: https://www.scien.cx/2025/10/23/print-tree3/. [Accessed: ]
rf:citation
» print tree3 | Query Filter | Sciencx | https://www.scien.cx/2025/10/23/print-tree3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.