Skip to content

Instantly share code, notes, and snippets.

@qutek
Last active March 11, 2021 09:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qutek/868c30f5d1e7ec03c7459e067444bd67 to your computer and use it in GitHub Desktop.
Save qutek/868c30f5d1e7ec03c7459e067444bd67 to your computer and use it in GitHub Desktop.
[Clear Node Modules] Delete all node_modules directory #bash
#!/bin/bash
#
# This script will remove all node modules
#
# Author: Lafif Astahdziq
# https://lafif.me
#
FOLDER_ROOT=${1:-.} # default to current directory
echo "---- Check Folders -----";
# Check and count node modules directories
if [[ $(find ${FOLDER_ROOT} -name "node_modules" -type d -prune | wc -l) -eq 0 ]]; then
echo "No node_modules found!"
else
find ${FOLDER_ROOT} -name "node_modules" -type d -prune -print | xargs du -chs
# Let user confirm delete
read -p "Delete all folders (y/n)? " -n 1 -r CONFIRM
echo # move to a new line
if [[ $CONFIRM =~ ^[Yy]$ ]]
then
echo "Removing..";
find ${FOLDER_ROOT} -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
fi
fi

Use

find . -name "node_modules" -type d -prune -print | xargs du -chs

or

find . -type d -name node_modules | grep -v node_modules.*node_modules | tr '\n' '\0' | xargs -0 du -sch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment