Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davehague/ed01845899cc3d89d8d2a965bb2a39e5 to your computer and use it in GitHub Desktop.
Save davehague/ed01845899cc3d89d8d2a965bb2a39e5 to your computer and use it in GitHub Desktop.

Print File Tree (PFT)

  1. Edit a file in /usr/local/bin called pft

  2. Mark it executable: sudo chmod +x /usr/local/bin

  3. Add these contents

#!/bin/bash

print_tree() {
    local path="$1"
    local indent="$2"
    local show_lines="$3"
    local excluded=("node_modules" ".nuxt" ".venv" "venv" ".idea" "__pycache__")
    local extensions=("py" "txt" "md" "ini" "ts" "json" "vue" "yaml" "env")

    for item in "$path"/*; do
        local name=$(basename "$item")
        if [[ -d "$item" ]]; then
            if [[ ! " ${excluded[@]} " =~ " $name " ]]; then
                echo "${indent}|-- $name"
                print_tree "$item" "${indent}|   " "$show_lines"
            fi
        elif [[ -f "$item" ]]; then
            local ext="${name##*.}"
            if [[ "$show_lines" == "true" ]]; then
                local lines=$(wc -l < "$item")
                printf "%s|-- %s (%d lines)\n" "$indent" "$name" "$lines"
            else
                echo "${indent}|-- $name"
            fi
        fi
    done
}

# Handle -l flag
if [[ "$1" == "-l" ]]; then
    print_tree "." "" "true"
else
    print_tree "." "" "false"
fi
  1. Run like this
pft
pft -l

Pass the -l flag to print with line counts.

Sample output

No line counts

davidhague@Davids-MacBook-Pro src % pft
|-- __init__.py
|-- data_parser.py
|-- parsers
|   |-- __init__.py
|   |-- address_parser.py
|   |-- address_validation.py
|   |-- data_cleaners.py
|   |-- data_combiners.py
|   |-- parsing_utils.py
|-- utils
|   |-- __init__.py
|   |-- config.py
|   |-- data_validator.py
|   |-- db_utils.py
|   |-- logging_config.py

With line counts

davidhague@Davids-MacBook-Pro src % pft -l
|-- __init__.py (0 lines)
|-- data_parser.py (16 lines)
|-- parsers
|   |-- __init__.py (79 lines)
|   |-- address_parser.py (316 lines)
|   |-- address_validation.py (76 lines)
|   |-- data_cleaners.py (102 lines)
|   |-- data_combiners.py (93 lines)
|   |-- parsing_utils.py (137 lines)
|-- utils
|   |-- __init__.py (0 lines)
|   |-- config.py (78 lines)
|   |-- data_validator.py (295 lines)
|   |-- db_utils.py (35 lines)
|   |-- logging_config.py (100 lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment