Crawl computer for node_modules, add .nosync file

Per https://medium.com/@isaiah.taylor/how-to-maintain-node-projects-with-icloud-drive-4c6549f7c806, I can prevent node_modules folders from syncing via iCloud Drive.

Trouble is: there's often a lot of node_modules folders. A lot, a lot. And I often forget before I start copying.

Questions (1) Is there a way to crawl through my hard drive, find node_modules folders and add a .nosync file?

(2) Is there a way to keep this crawling going in the background? So that this crawling would happen for any new folders? Wonder if this is a Hammerspoon kind of thing maybe?


You can run the following to create a .nosync file in each node_modules folder

find ~ -type d -name node_modules -exec touch {}/.nosync \;

This will look through everything in your home directory. You can limit it to the Desktop folder by replacing ~ with ~/Desktop.

To have the command run regularly you can create a crontab entry:

  • Run EDITOR=nano crontab -e to start editing. If you haven't created another entry before the file will be empty
  • Add
    0 * * * * find $HOME -type d -name node_modules -exec touch {}/.nosync \; 
    
    to have it run every full hour, or
    0 */3 * * * find $HOME -type d -name node_modules -exec touch {}/.nosync \; 
    
    to run it every three hours
  • Type Ctrl-X and answer "Y" on the save prompt