Skip to main content

Remove node_modules

·137 words·1 min
Linux Nodejs
Table of Contents

How to Find and Remove
#

find /home/user/code -used +7 -type d -name node_modules -prune -print0 | xargs -0 /bin/rm -rf

  1. to find inside /home/user/code
  2. directories (-type d)
  3. named node_modules (-name node_modules)
  4. that were used more than 7 days ago (-used +7)
  5. if found, stop looking inside -prune
  6. and output a full name -print0 (with 0 instead of newline)
  7. pass the found full name (xargs -0)
  8. to rm (/bin/rm -rf)

Save Script
#

/home/user/bin/cron/remove_node_modules:

#!bin/bash
find /home/user/code -used +7 -type d -name node_modules -prune -print0 | xargs -0 /bin/rm -rf

How to Automate
#

Add the script to crontab

  1. open a crontab file
    crontab -e (to change editor run select-editor)
  2. speicfy an interval and a path to the script file
    @weekly /home/user/bin/cron/remove_node_modules
  3. list user’s crontab:
    crontab -l

Sources
#