Skip to content

Instantly share code, notes, and snippets.

@maxcnunes
Last active January 25, 2017 17:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxcnunes/6d1b0f02d4cd5784cfdd7cb08a46fe8f to your computer and use it in GitHub Desktop.
Save maxcnunes/6d1b0f02d4cd5784cfdd7cb08a46fe8f to your computer and use it in GitHub Desktop.
Remove all node_module folders recursively. Useful to free up disk space by removing the node_module from those projects you have not used for a while.
# Simple version using only "find"
find . -name "node_modules" -type d -prune -exec rm -rf '{}' +
# More helpful version with some log messages and with option of dry mode
# Usage:
# MODE: DRY
# sh remove-node_module-recursively-2.sh ~/my-path-with-many-projects
# MODE: REMOVE
# sh remove-node_module-recursively-2.sh ~/my-path-with-many-projects remove
REMOVE_PATH=$1
MODE=$2
echo "Removing all node_modules from $REMOVE_PATH"
if [ "$MODE" != "remove" ]; then
echo "-- DRY MODE --"
fi
for line in $(find $REMOVE_PATH -name "node_modules" -type d -prune); do
found=`echo "$line" | grep -o node_modules | wc -l`
if [ $found -eq 1 ]; then
echo "-> $line"
if [ "$MODE" = "remove" ]; then
echo "Removing..."
rm -rf "$line"
else
echo "Skip (dry mode enabled)"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment