Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save peiris/4bfe7edd94c76a98f578a228e8418529 to your computer and use it in GitHub Desktop.
Save peiris/4bfe7edd94c76a98f578a228e8418529 to your computer and use it in GitHub Desktop.
Clear all Node Modules Folders Recursively Mac/Linux
- Open your terminal.
- Browse to your master project directory or the top level of where you have a bunch of projects stored.
- Run the command: ``find . -name "node_modules" -type d -prune -exec rm -rf '{}' +``
####How it works
``find .`` Instructs the find program to search for files in the current directory ..
``-name "node_modules"`` Instructs the find program to search for files named “node_modules”.
``-type d`` Instructs the find program to only look for file directories named “node_modules”.
``-prune`` Instructs the find program to not descend into the current file/directory for it to exclude child “node_modules” directories. On Mac, since -d was specified, -prune has no effect on the find program’s lookup results (per the man pages). However, on Linux, -prune still instructs the find program to not descend into the current file/directory.
``-exec rm -rf '{}' +`` Instructs the find program to execute rm -rf on the matching results. The code '{}' + instructs the command line to be built by appending each selected file name at the end thus invoking the rm -rf command less times than the total number of “node_modules” directory matches. This helps with performance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment