Skip to content

Instantly share code, notes, and snippets.

@a-c-m
Created December 15, 2023 10:14
Show Gist options
  • Save a-c-m/ad14a7ac60732fe1fee663eec3b51507 to your computer and use it in GitHub Desktop.
Save a-c-m/ad14a7ac60732fe1fee663eec3b51507 to your computer and use it in GitHub Desktop.

Revisions

  1. a-c-m created this gist Dec 15, 2023.
    22 changes: 22 additions & 0 deletions measure-node_modules.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #!/bin/bash

    # Find all top-level node_modules directories and calculate their total size
    total_size=0
    for dir in $(find . -name "node_modules" -type d -not -path "*/node_modules/*"); do
    size=$(du -sk "$dir" | cut -f1)
    total_size=$(($total_size + $size))
    done

    # Convert total size to human-readable format
    total_size_human=$(awk "BEGIN { printf \"%.2f\", $total_size/1024/1024 }")

    # Print total size
    printf "Total node_modules size is %.2f GB, across %d directories.\n" $total_size_human $(find . -name "node_modules" -type d -not -path "*/node_modules/*" | wc -l)

    # Find all top-level node_modules directories and print their size and percentage of total
    for dir in $(find . -name "node_modules" -type d -not -path "*/node_modules/*"); do
    size=$(du -sk "$dir" | cut -f1)
    percentage=$(echo "scale=2; $size / $total_size * 100" | bc)
    size_human=$(awk "BEGIN { printf \"%.2f\", $size/1024/1024 }")
    printf "%0.0f%% - %s (%.2f GB)\n" $percentage $dir $size_human
    done