Created
September 28, 2023 14:43
-
-
Save MaurerKrisztian/a93b51f196eac9d3f9245f485dbe2cb5 to your computer and use it in GitHub Desktop.
This script searches for top-level node_modules directories within a specified directory (or the entire filesystem by default). It calculates and displays the size of each found node_modules directory and provides a total size at the end.
This file contains 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
#!/bin/bash | |
# ANSI color codes | |
RED="\033[31m" | |
GREEN="\033[32m" | |
YELLOW="\033[33m" | |
RESET="\033[0m" | |
# Set the starting directory for the search. Default is root if no argument is provided. | |
START_DIR=${1:-/} | |
# Find top-level node_modules directories directly under a project within the specified directory | |
# and exclude those inside the .cache directory | |
directories=$(find "$START_DIR" -type d -name "node_modules" -not -path "*/node_modules/*/*" -not -path "*/.cache/*" 2>/dev/null) | |
total_size=0 | |
# Loop through each directory and calculate its size | |
for dir in $directories; do | |
# Check if the parent directory of node_modules is not another node_modules (to avoid nested ones) | |
parent_dir=$(dirname "$dir") | |
if [[ $(basename "$parent_dir") != "node_modules" ]]; then | |
size=$(du -sh "$dir" | cut -f1) | |
echo -e "${YELLOW}Size of $dir: ${GREEN}$size${RESET}" | |
# Convert size to bytes for summation | |
bytes=$(du -sb "$dir" | cut -f1) | |
total_size=$((total_size + bytes)) | |
fi | |
done | |
# Convert total size back to human-readable format | |
human_readable_total=$(numfmt --to=iec-i --suffix=B $total_size) | |
echo -e "${RED}Total size of all top-level node_modules within $START_DIR: ${GREEN}$human_readable_total${RESET}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment