Programming
bash macos command-line command-line-interface node-modules
Updated Thu, 25 Aug 2022 12:56:53 GMT

print the commands to delete node_modules folders recursively in a bash text file


I want this - https://stackoverflow.com/a/43561012/126833

find . -name 'node_modules' -type d -prune -exec rm -rf '{}'

except - instead of getting this done automatically, I want the all of the rm -rf /path/to/project/node_modules commands in a bash file like rm_node_modules.sh for me to review and then only I'll execute as bash rm_node_modules.sh

So basically my rm_node_modules.sh should be like :

rm -rf /path/to/project-1/node_modules
rm -rf /path/to/project-2/node_modules
rm -rf /path/to/project-3/node_modules



Solution

You are on macOS where find does not support the -printf flag which would make this a bit simpler. But something like this will do the job:

find . -name node_modules -type d -print0 | xargs -0 stat -f "rm -rf %N" > rm_node_modules.sh




Comments (3)

  • +0 – Thanks for the one-liner. I somehow dread the idea of an automatic rm -rf command. I'm afraid of a catastrophe. — Jul 14, 2022 at 12:09  
  • +0 – Yes, fair enough. You can make it interactive, so you have to confirm each one, by using rm -rfi. — Jul 14, 2022 at 12:11  
  • +0 – I ran your command in my workspace folder and the bash file has 116 rm commands. So that would be a lot of ENTERs. I thought I would manually glace through the parent folders and decide in case there are any I dont want deleted. — Jul 14, 2022 at 14:25