Skip to content

Instantly share code, notes, and snippets.

@therealklanni
Last active October 17, 2020 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save therealklanni/8bc06ca1274af693be017660db7905d7 to your computer and use it in GitHub Desktop.
Save therealklanni/8bc06ca1274af693be017660db7905d7 to your computer and use it in GitHub Desktop.
Automatically remove stale modules (based on last-modified date)
# Remove stale modules directories, fast!
#
# Paste or source this file in your shell rc file (should work with Bash and ZSH)
#
# Optionally provide a path as first arg, and optionally a duration in days as second arg (default=30)
# e.g. clean_modules ~/github 60
# Remove the "-exec du" line for faster operation (i.e. do not print sizes, which can be slow)
PROJECT_DIR=~/Projects
# If `bfs` is installed, use that for faster searching
if type "bfs" > /dev/null; then
FINDER=bfs
else
FINDER=find
fi
function clean_modules() {
if [ $# -eq 0 ]; then
CLEANUP_SEARCH_PATH="$PROJECT_DIR"
else
CLEANUP_SEARCH_PATH="$1"
fi
$FINDER -maxdepth 4 -type d -mtime "+${2:-30}" "$CLEANUP_SEARCH_PATH" \
\(\
-not -path '*/node_modules/*' \
-a -not -path '*/bower_components/*' \
-a -not -path '*/.git/*' \
-a -not -path '*.git' \
-a -not -path '*/.yarn/*' \
\) \
\(\
-iname node_modules \
-o -iname bower_components \
\) \
-exec du -shc --apparent-size {} + \
-exec rm -rf {} +
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment