March 15, 2021 • 2 min read
You want to do a backup of all your current files because you’re switching computers.
Some of your local repositories have unpublished branches so you want to copy them over to your new machine rather than checking out their remote versions.
However, the gazillion files inside node_modules make copying the repo take 2 hours. You don’t want to go on every directory and delete the folder manually.
What do you do?
We are going to use Unix's find to look for node_modules folders in our repositories directory, and then we are going to use xargs to pass these paths to rm.
cd my-repos
node_modules directories you are going to remove:find . -type d -name node_modules
# ./example-repo/node_modules
# ./example-repo/node_modules/node-libs-browser/node_modules
# ...
xargs to pass these paths to rm :find . -type d -name node_modules | xargs rm -rf
node_modules have been deleted:find . -type d -name node_modules
# Empty
Now copying over our repositories directory to our new machine takes just a second, and we can run npm install to generate a new node_modules directory.