print tree2

!/usr/bin/env bash

Unicode-style tree with file sizes and total files per directory

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

# Exit if ma…


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

!/usr/bin/env bash

Unicode-style tree with file sizes and total files per directory

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

# Exit if maxdepth reached
if [[ $maxdepth -ne 0 && $depth -gt $maxdepth ]]; then
    return
fi

# Ensure directory exists
[[ ! -d "$dir" ]] && return

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

# Enable nullglob so empty dirs don't break loops
shopt -s nullglob

# Get directories first
local dirs=("$dir"/*/)
IFS=$'\n' dirs=($(sort <<<"${dirs[*]}"))

for i in "${!dirs[@]}"; do
    local 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

# Then files
local files=("$dir"/*)
IFS=$'\n' files=($(sort <<<"${files[*]}"))
local file_count=0
for f in "${files[@]}"; do
    [[ -f "$f" ]] || continue
    local branch="├──"
    [[ $file_count -eq $(( ${#files[@]} - 1 )) ]] && branch="└──"
    # Human-readable size
    local size=$(stat -c "%s" "$f" 2>/dev/null || stat -f "%z" "$f")
    local hr_size
    if [[ "$size" =~ ^[0-9]+$ ]]; then
        if [ "$size" -lt 1024 ]; then
            hr_size="${size}B"
        elif [ "$size" -lt $((1024*1024)) ]; then
            hr_size="$((size/1024))K"
        elif [ "$size" -lt $((1024*1024*1024)) ]; then
            hr_size="$((size/1024/1024))M"
        else
            hr_size="$((size/1024/1024/1024))G"
        fi
    else
        hr_size="?"
    fi
    echo "${prefix}${branch} ${f##*/} [${hr_size}]"
    ((file_count++))
done

# Total files in directory
if [ $file_count -gt 0 ]; then
    echo "${prefix}└── Total files: $file_count"
fi

shopt -u nullglob

}

Usage: print_tree

Example: print_tree . "" 0 # unlimited depth

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:51:32+00:00) print tree2. Retrieved from https://www.scien.cx/2025/10/23/print-tree2/

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

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.